39 lines
636 B
Go
39 lines
636 B
Go
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)
|
||
}
|