ADVERTISEMENT
ADVERTISEMENT

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)

Parameter(s):

  • 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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.