Files
banstagy/com/cross.go
2026-08-24 20:53:21 +08:00

39 lines
636 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
}