fmod Compute remainder of division Returns the floating-point remainder of numer/denom (rounded towards zero):
fmod = numer - tquot * denom
Where tquot is the truncated (i.e., rounded towards zero) result of: numer/denom.
import "math"
func Mod(x, y float64) float64
Mod returns the floating-point remainder of x/y. The magnitude of the result is less than y and its sign agrees with that of x.
For example,
package main
/*
#cgo LDFLAGS: -lm
#include <math.h>
#include <stdio.h>
double fmodC(double x, double y) {
double mod = fmod(x, y);
printf("%f
", mod);
return mod;
}
*/
import "C"
import (
"fmt"
"math"
)
func fmodGo(x, y float64) float64 {
mod := math.Mod(x, y)
fmt.Printf("%f
", mod)
return mod
}
func main() {
x, y := 42.0*3.14159, -2.718
fmt.Println(x, y)
modC := C.fmodC(C.double(x), C.double(y))
modGo := fmodGo(x, y)
fmt.Println(float64(modC) == modGo)
// fmod = numer - tquot * denom
numer := x
denom := y
tquot := float64(int64(x / y))
fmod := math.Mod(numer, denom)
fmt.Println(fmod == numer-tquot*denom)
}
Output:
131.94678 -2.718
1.482780
1.482780
true
true