Source file src/pkg/math/fmod.go
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 // Fmod 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 // if x is not finite, Fmod returns NaN 17 // if y is 0 or NaN, Fmod returns NaN 18 func Fmod(x, y float64) float64 { 19 // TODO(rsc): Remove manual inlining of IsNaN, IsInf 20 // when compiler does it for us. 21 if y == 0 || x > MaxFloat64 || x < -MaxFloat64 || x != x || y != y { // y == 0 || IsInf(x, 0) || IsNaN(x) || IsNan(y) 22 return NaN() 23 } 24 if y < 0 { 25 y = -y 26 } 27 28 yfr, yexp := Frexp(y) 29 sign := false 30 r := x 31 if x < 0 { 32 r = -x 33 sign = true 34 } 35 36 for r >= y { 37 rfr, rexp := Frexp(r) 38 if rfr < yfr { 39 rexp = rexp - 1 40 } 41 r = r - Ldexp(y, rexp-yexp) 42 } 43 if sign { 44 r = -r 45 } 46 return r 47 }