Go Modf()
Anonymous contributor
Published Aug 1, 2023
Contribute to Docs
The Modf() function in Go is used to find the integer and fractional part of a floating-point number. This function is part of the math package, and the package must be imported to use the function in a Go program.
Syntax
int, frac := Modf(f)
Where:
f: is the floating-point number to extract the integer and fractional parts from.int: is the integer part of the passed parameter.frac: is the fractional part of the passed parameter.
Some special return cases:
- If (±)Inf is passed as an argument, then
Modfreturns (±)Inf andNAN. - If the argument passed is
NAN, then the return will also beNAN.
Example
Below is a basic implementation of the Modf() function.
package mainimport("fmt""math")func main() {var x float64 = 3.3456var w, z float64 = math.Modf(x)fmt.Print("The value ", x, " is composed of the integer: ", w, " and the fractional part: ", z)}
The output will be:
The value 3.3456 is composed of the integer: 3 and the fractional part: 0.34560000000000013
Codebyte Example
The following example shows how the Modf() function can be used in a Go program:
All contributors
- Anonymous contributor
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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use Go (Golang), an open-source programming language supported by Google!
- Beginner Friendly.6 hours