Mod Versus Remainder Float Semantics
Compare math.Mod and math.Remainder on the same operands: truncated division keeps the dividend sign, IEEE 754 remainder rounds to the nearest quotient.
Code
package main
import (
"fmt"
"math"
)
func main() {
// Mod:截断除法的余数,符号跟随被除数
fmt.Println("mod(5,3):", math.Mod(5, 3), " mod(-5,3):", math.Mod(-5, 3))
// Remainder:IEEE 754 就近舍入求余,|结果| <= 除数的一半
fmt.Println("rem(5,3):", math.Remainder(5, 3), " rem(-5,3):", math.Remainder(-5, 3))
// 周期回绕:把任意角度规范回 (-π, π] 区间
angle := 9.0
wrapped := math.Remainder(angle, 2*math.Pi)
fmt.Println("9 rad wrapped:", wrapped/math.Pi, "π")
// Mod(-6,3) 的结果是 -0:与 0 相等但符号位为负
fmt.Println("mod(-6,3)==0:", math.Mod(-6, 3) == 0, " signbit:", math.Signbit(math.Mod(-6, 3)))
}How it works
Mod Versus Remainder Float Semantics comes down to the function main.
What to watch for
- No external packages — everything relies on the language’s built-in APIs.
Using it
Signature: func main()
Running the file prints labeled results such as mod(5,3):, rem(5,3):, 9 rad wrapped: — each operation’s effect is visible in the output.
Key statements
wrapped := math.Remainder(angle, 2*math.Pi)
Interactive Tools for This Topic
cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.GCD & LCM CalculatorFind the greatest common divisor and least common multiple of two or more integers, computed exactly with BigInt.IEEE 754 ConverterConvert decimal floats to and from IEEE 754 single- and double-precision bit patterns in hex and binary.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- What does Mod Versus Remainder Float Semantics print when run?
- Execute the whole file; each printed line is prefixed with a label like `mod(5,3):`, `rem(5,3):`, `9 rad wrapped:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.