This commit is contained in:
2026-08-24 20:53:21 +08:00
commit 6e78bba16a
287 changed files with 32443 additions and 0 deletions

38
com/cross.go Normal file
View File

@@ -0,0 +1,38 @@
package com
import "math"
func NewCrossLog() *CrossLog {
return &CrossLog{}
}
type CrossLog struct {
PrevVal float64
Sign int // 最近一次交叉符号:1上穿,-1下穿
XAt int // 最近一次交叉序号
Num int // 当前序号
}
func (l *CrossLog) Update(sub float64) {
l.Num += 1
if sub == 0 {
return
}
if math.IsNaN(l.PrevVal) || l.PrevVal == 0 {
l.PrevVal = sub
} else {
if factor := l.PrevVal * sub; factor < 0 {
l.PrevVal = sub
l.XAt = l.Num
if sub > 0 {
l.Sign = 1
} else {
l.Sign = -1
}
}
}
}
func (l *CrossLog) Get() int {
return l.Sign * (l.Num - l.XAt + 1)
}