Home » How to Find x%y Using Golang Modulus Operator

How to Find x%y Using Golang Modulus Operator

The Golang modulus operator (%) returns the remainder after integer division. The modulo operator returns the remainder left after the division of two integers.

Golang math package has a Mod function that can be used to get the remainder after the division of two float numbers.

In this article, we will discuss how to find the mod of a given number in golang using the modulus operator, mod function, and remainder function in go.

How to Get Remainder of the Divison Using Golang Modulus Operator %

To get the remainder of the division of two integer numbers in golang:

  1. Use Modulus operator %
  2. x % y, where x and y are integers
  3. Returns the remainder.

Let’s understand the Golang Modulus operator with an example.

package main

import (
	"fmt"
)

func main() {
	ans := 1 % 1
	fmt.Println(ans)

	ans = 7 % 2
	fmt.Println(ans)

	ans = 16 % 4
	fmt.Println(ans)

	ans = 5 % 7
	fmt.Println(ans)
        
        // Remainder with negative dividen
	ans = -15 % 2
	fmt.Println(ans)
}

Output

0
1
0
5
-1

In the above Golang program, the modulus operator % returns the remainder after the division of the dividend by the divisor.

If a dividend is a negative number, the final remainder takes the sign of the dividend.

For example, for the operation, -15 % 2 where the dividend is -15 and divisor is 2, % returns the remainder as -1

How to Get Remainder for Float Using Golang Mod Function

To get the remainder of the division of the float number, use the Mod function. the Golang math package has a Mod function that takes two float numbers as input parameters and returns the remainder of the float type.

Let’s understand the Golang Mod function with an example.

package main

import (
	"fmt"
	"math"
)

func main() {
	ans := math.Mod(20, 4)
	fmt.Println(ans)

	ans = math.Mod(8.4, 2)
	fmt.Println(ans)

	ans = math.Mod(15, 4)
	fmt.Println(ans)

	ans = math.Mod(-25, 2)
	fmt.Println(ans)
}

Output

0
0.40000000000000036
3
-1

In the above golang program, the Mod function returns the remainder after the division of the dividend and divisor.

If the dividend has a negative sign, the output remainder will take the negative sign of a dividend.

Cool Tip: How to find the floor of a number in golang!

How to Get IEEE 754 floating-point remainder

Golang math package has a Remainder function that takes two float numbers as input parameters and returns the IEEE 754 floating-point remainder.

Let’s understand the Golang Remainder() function with an example.

package main

import (
	"fmt"
	"math"
)

func main() {

	ans := math.Remainder(10, 4)
	fmt.Println(ans)

	ans = math.Remainder(8.5, 2)
	fmt.Println(ans)

	ans = math.Remainder(13, 4)
	fmt.Println(ans)

	ans = math.Remainder(-5, 2)
	fmt.Println(ans)

	ans = math.Remainder(3.5, 1)
	fmt.Println(ans)
}

Output

In the above golang program, the Remainder function in golang returns the IEEE 754 floating-point remainder after the division of two numbers.

Cool Tip: How to calculate the power of a number in Golang!

Conclusion

I hope the above article helped you to understand the Golang Modulus operator and math package functions like Mod and Remainder to find the remainder.

You can find more topics about the Golang tutorials on the GolangSpot Home page.

Leave a Comment