]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgo/go/math/fmod.go
Add Go frontend, libgo library, and Go testsuite.
[thirdparty/gcc.git] / libgo / go / 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 /*
9 Floating-point mod function.
10 */
11
12 // Fmod returns the floating-point remainder of x/y.
13 // The magnitude of the result is less than y and its
14 // sign agrees with that of x.
15 //
16 // Special cases are:
17 // if x is not finite, Fmod returns NaN
18 // if y is 0 or NaN, Fmod returns NaN
19 func Fmod(x, y float64) float64 {
20 // TODO(rsc): Remove manual inlining of IsNaN, IsInf
21 // when compiler does it for us.
22 if y == 0 || x > MaxFloat64 || x < -MaxFloat64 || x != x || y != y { // y == 0 || IsInf(x, 0) || IsNaN(x) || IsNan(y)
23 return NaN()
24 }
25 if y < 0 {
26 y = -y
27 }
28
29 yfr, yexp := Frexp(y)
30 sign := false
31 r := x
32 if x < 0 {
33 r = -x
34 sign = true
35 }
36
37 for r >= y {
38 rfr, rexp := Frexp(r)
39 if rfr < yfr {
40 rexp = rexp - 1
41 }
42 r = r - Ldexp(y, rexp-yexp)
43 }
44 if sign {
45 r = -r
46 }
47 return r
48 }