Mod()
Published Aug 6, 2023
Contribute to Docs
The Mod()
function returns the floating-point remainder of dividing x by y. The result has the same sign as x and a magnitude less than the magnitude of y.
The Mod()
function operates exclusively with floating-point numbers.
Note: If the numbers involved in the operation are of integer type, the
%
modulus operator should be used instead. The%
operator works with integers, whilemath.Mod()
operates with floating-point numbers.
Syntax
math.Mod(x,y)
The Mod()
function takes two arguments of type ‘float64’.
Please be aware of the following special cases:
- If the argument has a
+Inf
value, the return value will be0
: - 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 either0
orNAN
. - If
(±)Inf
is passed as the second argument, the return value isx (first argument)
.
Example
The following example demonstrates how to use the Mod()
function and print the result:
package mainimport("fmt""math")func main() {a:= 23.50b:= 5.20modResult := math.Mod(a, b)fmt.Printf("The modulus of %.2f and %.2f = %.2f", a, b, modResult)}
The output will be:
The modulus of 23.50 and 5.20 = 2.70
Codebyte Example
The provided example is executable and demonstrates special cases in the use of the Mod()
function:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Go on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Go
Learn how to use Go (Golang), an open-source programming language supported by Google!Beginner Friendly6 hours