Home »
Golang »
Golang Reference
Golang math.Nextafter() Function with Examples
Golang | math.Nextafter() Function: Here, we are going to learn about the Nextafter() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 02, 2021
math.Nextafter()
The Nextafter() function is an inbuilt function of the math package which is used to get the next representable float64 (floating-point) value after the first parameter (x) towards the second parameter (y).
It accepts two parameters (x, y) and returns the next representable float64 value after x towards y.
Syntax
func Nextafter(x, y float64) (r float64)
Parameters
- x, y : The values to be used to find the next representable float64 value after x towards y.
Return Value
The return type of Nextafter() function is a float64, it returns the next representable float64 value after x towards y.
Special Cases
- Nextafter(x, x) = x
If both the parameters are same, the function returns the same value.
- Nextafter(NaN, y) = NaN
If the first parameter is NaN, the function returns NaN.
- Nextafter(x, NaN) = NaN
If the second parameter is NaN, the function returns NaN.
Example 1
// Golang program to demonstrate the
// example of math.Nextafter() Function
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(math.Nextafter(1.0, 2.0))
fmt.Println(math.Nextafter(1.0, 1.5))
fmt.Println(math.Nextafter(-10.23, -20))
fmt.Println(math.Nextafter(10, 5))
fmt.Println(math.Nextafter(1.49, -2))
fmt.Println(math.Nextafter(1.23, 1.23))
fmt.Println(math.Nextafter(1.23, math.NaN()))
fmt.Println(math.Nextafter(math.NaN(), 1.23))
}
Output:
1.0000000000000002
1.0000000000000002
-10.230000000000002
9.999999999999998
1.4899999999999998
1.23
NaN
NaN
Example 2
// Golang program to demonstrate the
// example of math.Nextafter() Function
package main
import (
"fmt"
"math"
)
func main() {
var x float64
var y float64
var NextafterXY float64
x = 1.23
y = 1.23
NextafterXY = math.Nextafter(x, y)
fmt.Println("Next representable float64 value after", x, "towards", y, "is", NextafterXY)
x = -1.23
y = 1.23
NextafterXY = math.Nextafter(x, y)
fmt.Println("Next representable float64 value after", x, "towards", y, "is", NextafterXY)
x = 0.5
y = 1
NextafterXY = math.Nextafter(x, y)
fmt.Println("Next representable float64 value after", x, "towards", y, "is", NextafterXY)
x = math.NaN()
y = 1.23
NextafterXY = math.Nextafter(x, y)
fmt.Println("Next representable float64 value after", x, "towards", y, "is", NextafterXY)
x = 1.23
y = math.NaN()
NextafterXY = math.Nextafter(x, y)
fmt.Println("Next representable float64 value after", x, "towards", y, "is", NextafterXY)
}
Output:
Next representable float64 value after 1.23 towards 1.23 is 1.23
Next representable float64 value after -1.23 towards 1.23 is -1.2299999999999998
Next representable float64 value after 0.5 towards 1 is 0.5000000000000001
Next representable float64 value after NaN towards 1.23 is NaN
Next representable float64 value after 1.23 towards NaN is NaN
Golang math Package Constants and Functions »