github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libgo/go/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  
    22  //extern fmod
    23  func libc_fmod(float64, float64) float64
    24  
    25  func Mod(x, y float64) float64 {
    26  	return libc_fmod(x, y)
    27  }
    28  
    29  func mod(x, y float64) float64 {
    30  	if y == 0 || IsInf(x, 0) || IsNaN(x) || IsNaN(y) {
    31  		return NaN()
    32  	}
    33  	if y < 0 {
    34  		y = -y
    35  	}
    36  
    37  	yfr, yexp := Frexp(y)
    38  	sign := false
    39  	r := x
    40  	if x < 0 {
    41  		r = -x
    42  		sign = true
    43  	}
    44  
    45  	for r >= y {
    46  		rfr, rexp := Frexp(r)
    47  		if rfr < yfr {
    48  			rexp = rexp - 1
    49  		}
    50  		r = r - Ldexp(y, rexp-yexp)
    51  	}
    52  	if sign {
    53  		r = -r
    54  	}
    55  	return r
    56  }