Go Mod() function is “used to find the modulus or remainder from the floating-point division of the two arguments (x/y)”.
Syntax
func Mod(x, y float64) float64
Parameters
The Mod() function takes two arguments of type float64:
- x: It represents the numerator in the division that will take place to find the remainder.
- y: It represents the denominator in the division that will take place to find the remainder.
Return value
The math.Mod() function 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.
There are some of the following cases.
- If the argument has a +Inf value, the return value will be 0.
- If the value of
x
is either (±)Inf orNAN
, the return value will beNAN
. - The return value is
NAN
if the value of the second argument is either 0 orNAN
. - If (±)Inf is passed as the second argument, the return value is
x
.
Example 1: Golang program to calculate the modulo
package main
import (
"fmt"
"math"
)
func main() {
var x float64 = 21.19
var y float64 = 19.21
result := math.Mod(x, y)
fmt.Println("The Remainder of ", x, "/", y, " is:", result)
}
Output
The Remainder of 21.19 / 19.21 is: 1.9800000000000004
Example 2: Handling NaN values in Mod() Function
If you pass the NaN value to the math.Mod() function returns the NaN value.
To get the NaN value in Golang, use math.NaN() function.
package main
import (
"fmt"
"math"
)
func main() {
var x float64 = 21.19
var y float64 = math.NaN()
result := math.Mod(x, y)
fmt.Println("The Remainder of ", x, "/", y, " is:", result)
}
Output
The Remainder of 21.19 / NaN is: NaN
That’s it!

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.