Golang math.Remainder() Function with Examples

Golang | math.Remainder() Function: Here, we are going to learn about the Remainder() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 03, 2021

math.Remainder()

The Remainder() function is an inbuilt function of the math package which is used to get the IEEE 754 floating-point remainder of x/y. Where x is the dividend and y is the divisor.

It accepts two parameters (x, y) and returns the IEEE 754 floating-point remainder of x/y.

Syntax:

func Remainder(x, y float64) float64

Parameter(s):

  • x, y : The values to be used to find the floating-point remainder of x/y.

Return Value:

The return type of Remainder() function is a float64, it returns the IEEE 754 floating-point remainder of x/y.

Special cases:

  • Remainder(±Inf, y) = NaN
    If the dividend is ±Inf, the function returns NaN.
  • Remainder(NaN, y) = NaN
    If the dividend is NaN, the function returns NaN.
  • Remainder(x, 0) = NaN
    If the divisor is 0, the function returns NaN.
  • Remainder(x, ±Inf) = x
    If the divisor is ±Inf, the function returns the value of dividend (x).
  • Remainder(x, NaN) = NaN
    If the divisor is NaN, the function returns NaN.

Example 1:

// Golang program to demonstrate the
// example of math.Remainder() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Remainder(2, 6))
	fmt.Println(math.Remainder(2.5, 1.2))
	fmt.Println(math.Remainder(100, 33))
	fmt.Println(math.Remainder(10, 5))
	fmt.Println(math.Remainder(1.49, -2))

	fmt.Println(math.Remainder(5.0, 0))
	fmt.Println(math.Remainder(1, 10))
	fmt.Println(math.Remainder(math.NaN(), 10))
	fmt.Println(math.Remainder(math.Inf(1), 0))
}

Output:

2
0.10000000000000009
1
0
-0.51
NaN
1
NaN
NaN

Example 2:

// Golang program to demonstrate the
// example of math.Remainder() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var y float64
	var RemainderXY float64

	x = 2
	y = 8
	RemainderXY = math.Remainder(x, y)
	fmt.Println("Remainder of", x, "/", y, "is", RemainderXY)

	x = 5
	y = 4
	RemainderXY = math.Remainder(x, y)
	fmt.Println("Remainder of", x, "/", y, "is", RemainderXY)

	x = 0.5
	y = 15
	RemainderXY = math.Remainder(x, y)
	fmt.Println("Remainder of", x, "/", y, "is", RemainderXY)

	x = -5
	y = 0
	RemainderXY = math.Remainder(x, y)
	fmt.Println("Remainder of", x, "/", y, "is", RemainderXY)

	x = 1.23
	y = 10
	RemainderXY = math.Remainder(x, y)
	fmt.Println("Remainder of", x, "/", y, "is", RemainderXY)
}

Output:

Remainder of 2 / 8 is 2
Remainder of 5 / 4 is 1
Remainder of 0.5 / 15 is 0.5
Remainder of -5 / 0 is NaN
Remainder of 1.23 / 10 is 1.23

Golang math Package Constants and Functions »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.