gitee.com/quant1x/num@v0.3.2/math32/mod.go (about) 1 package math32 2 3 // Mod returns the floating-point remainder of x/y. 4 // The magnitude of the result is less than y and its 5 // sign agrees with that of x. 6 // 7 // Special cases are: 8 // 9 // Mod(±Inf, y) = NaN 10 // Mod(NaN, y) = NaN 11 // Mod(x, 0) = NaN 12 // Mod(x, ±Inf) = x 13 // Mod(x, NaN) = NaN 14 func Mod(x, y float32) float32 { 15 return mod(x, y) 16 } 17 18 func mod(x, y float32) float32 { 19 if y == 0 || IsInf(x, 0) || IsNaN(x) || IsNaN(y) { 20 return NaN() 21 } 22 if y < 0 { 23 y = -y 24 } 25 26 yfr, yexp := Frexp(y) 27 sign := false 28 r := x 29 if x < 0 { 30 r = -x 31 sign = true 32 } 33 34 for r >= y { 35 rfr, rexp := Frexp(r) 36 if rfr < yfr { 37 rexp = rexp - 1 38 } 39 r = r - Ldexp(y, rexp-yexp) 40 } 41 if sign { 42 r = -r 43 } 44 return r 45 }