ADVERTISEMENT
ADVERTISEMENT

Golang math.Mod() Function with Examples

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

math.Mod()

The Mod() function is an inbuilt function of the math package which is used to get the floating-point remainder of the given parameters (x/y). The magnitude of the result is less than the second parameter (y) and its sign agrees with that of the first parameter (x).

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

Syntax:

func Mod(x, y float64) float64

Parameter(s):

  • x, y : The values of dividend and divisor to get the floating-point remainder.

Return Value:

The return type of Mod() function is a float64, it returns the floating-point remainder of the given parameters.

Special cases:

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

Example 1:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Mod(10, 3))
	fmt.Println(math.Mod(20, 13))
	fmt.Println(math.Mod(10, 10))
	fmt.Println(math.Mod(-10, 2))
	fmt.Println(math.Mod(10, -2))
	fmt.Println(math.Mod(-10, -3))
	fmt.Println(math.Mod(12.50, -0.65))

	fmt.Println(math.Mod(math.Inf(1), 1))
	fmt.Println(math.Mod(math.Inf(-1), 1))
	fmt.Println(math.Mod(math.NaN(), 3))
	fmt.Println(math.Mod(10, 0))
	fmt.Println(math.Mod(10, math.Inf(1)))
	fmt.Println(math.Mod(10, math.Inf(-1)))
	fmt.Println(math.Mod(10, math.NaN()))
}

Output:

1
7
0
-0
0
-1
0.14999999999999958
NaN
NaN
NaN
NaN
10
10
NaN

Example 2:

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

package main

import (
	"fmt"
	"math"
)

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

	x = 10
	y = 20
	ModXY = math.Mod(x, y)
	fmt.Println("floating-point remainder of", x, "/", y, "is", ModXY)

	x = 20
	y = 10
	ModXY = math.Mod(x, y)
	fmt.Println("floating-point remainder of", x, "/", y, "is", ModXY)

	x = -10
	y = -20
	ModXY = math.Mod(x, y)
	fmt.Println("floating-point remainder of", x, "/", y, "is", ModXY)

	x = -1.289
	y = -2.120
	ModXY = math.Mod(x, y)
	fmt.Println("floating-point remainder of", x, "/", y, "is", ModXY)

	x = 10
	y = math.Inf(-1)
	ModXY = math.Mod(x, y)
	fmt.Println("floating-point remainder of", x, "/", y, "is", ModXY)

	x = 10
	y = math.Inf(1)
	ModXY = math.Mod(x, y)
	fmt.Println("floating-point remainder of", x, "/", y, "is", ModXY)

	x = math.NaN()
	y = math.NaN()
	ModXY = math.Mod(x, y)
	fmt.Println("floating-point remainder of", x, "/", y, "is", ModXY)
}

Output:

floating-point remainder of 10 / 20 is 10
floating-point remainder of 20 / 10 is 0
floating-point remainder of -10 / -20 is -10
floating-point remainder of -1.289 / -2.12 is -1.289
floating-point remainder of 10 / -Inf is 10
floating-point remainder of 10 / +Inf is 10
floating-point remainder of NaN / NaN is NaN

Golang math Package Constants and Functions »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.