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