Home »
Golang »
Golang Reference
Golang math.Modf() Function with Examples
Golang | math.Modf() Function: Here, we are going to learn about the Modf() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 02, 2021
math.Modf()
The Modf() function is an inbuilt function of the math package which is used to get the integer and fractional floating-point numbers that sum to the given value (f). Both values have the same sign as f.
It accepts a parameter (f) and returns the integer and fractional floating-point numbers that sum to f.
Syntax
func Modf(f float64) (int float64, frac float64)
Parameters
- x : The value whose integer and fractional floating-point numbers are to be found.
Return Value
The return type of Modf() function is (int float64, frac float64), it returns two values an integer and a fractional floating-point numbers that sum to the given value.
Special Cases
- Modf(±Inf) = ±Inf, NaN
If the parameter is ±Inf, the function returns ±Inf as integer part and NaN as fractional part.
- Modf(NaN) = NaN, NaN
If the parameter is NaN, the function returns NaN as integer part and NaN as fractional part.
Example 1
// Golang program to demonstrate the
// example of math.Modf() Function
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Modf(1.25))
fmt.Println(math.Modf(11.123))
fmt.Println(math.Modf(10.0))
fmt.Println(math.Modf(50))
fmt.Println(math.Modf(-1.25))
fmt.Println(math.Modf(-11.123))
fmt.Println(math.Modf(-10.0))
fmt.Println(math.Modf(-50))
fmt.Println(math.Modf(math.Inf(1)))
fmt.Println(math.Modf(math.Inf(-1)))
fmt.Println(math.Modf(math.NaN()))
}
Output:
1 0.25
11 0.12299999999999933
10 0
50 0
-1 -0.25
-11 -0.12299999999999933
-10 -0
-50 -0
+Inf NaN
-Inf NaN
NaN NaN
Example 2
// Golang program to demonstrate the
// example of math.Modf() Function
package main
import (
"fmt"
"math"
)
func main() {
var x float64
x = 0
integer, fractional := math.Modf(x)
fmt.Println("Modf(", x, ") = ", integer, fractional)
x = 10.25
integer, fractional = math.Modf(x)
fmt.Println("Modf(", x, ") = ", integer, fractional)
x = -2.12
integer, fractional = math.Modf(x)
fmt.Println("Modf(", x, ") = ", integer, fractional)
x = math.Inf(1)
integer, fractional = math.Modf(x)
fmt.Println("Modf(", x, ") = ", integer, fractional)
x = math.Inf(-1)
integer, fractional = math.Modf(x)
fmt.Println("Modf(", x, ") = ", integer, fractional)
x = math.NaN()
integer, fractional = math.Modf(x)
fmt.Println("Modf(", x, ") = ", integer, fractional)
}
Output:
Modf( 0 ) = 0 0
Modf( 10.25 ) = 10 0.25
Modf( -2.12 ) = -2 -0.1200000000000001
Modf( +Inf ) = +Inf NaN
Modf( -Inf ) = -Inf NaN
Modf( NaN ) = NaN NaN
Golang math Package Constants and Functions »