github.com/c12o16h1/go/src@v0.0.0-20200114212001-5a151c0f00ed/math/mod.go (about) 1 // Copyright 2009-2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package math 6 7 /* 8 Floating-point mod function. 9 */ 10 11 // Mod returns the floating-point remainder of x/y. 12 // The magnitude of the result is less than y and its 13 // sign agrees with that of x. 14 // 15 // Special cases are: 16 // Mod(±Inf, y) = NaN 17 // Mod(NaN, y) = NaN 18 // Mod(x, 0) = NaN 19 // Mod(x, ±Inf) = x 20 // Mod(x, NaN) = NaN 21 func Mod(x, y float64) float64 22 23 func mod(x, y float64) float64 { 24 if y == 0 || IsInf(x, 0) || IsNaN(x) || IsNaN(y) { 25 return NaN() 26 } 27 y = Abs(y) 28 29 yfr, yexp := Frexp(y) 30 r := x 31 if x < 0 { 32 r = -x 33 } 34 35 for r >= y { 36 rfr, rexp := Frexp(r) 37 if rfr < yfr { 38 rexp = rexp - 1 39 } 40 r = r - Ldexp(y, rexp-yexp) 41 } 42 if x < 0 { 43 r = -r 44 } 45 return r 46 }