ADVERTISEMENT
ADVERTISEMENT

Golang math.NaN() Function with Examples

Golang | math.NaN() Function: Here, we are going to learn about the NaN() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 02, 2021

math.NaN()

The NaN() function is an inbuilt function of the math package which is used to get an IEEE 754 "not-a-number" value.

It does not accept any parameter and returns an IEEE 754 "not-a-number" value.

Syntax:

func NaN() float64

Parameter(s):

  • None

Return Value:

The return type of NaN() function is a float64, it returns an IEEE 754 "not-a-number" value.

Example 1:

// Golang program to demonstrate the
// example of math.NaN() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	// printing the NaN
	fmt.Println(math.NaN())

	// printing the type of NaN
	fmt.Printf("Type of NaN is:%T\n", math.NaN())

	// storing NaN value in a variable
	// and printing it on the screen
	result := math.NaN()
	fmt.Println("result:", result)
}

Output:

NaN
Type of NaN is:float64
result: NaN

Example 2:

// Golang program to demonstrate the
// example of math.NaN() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64

	// getting & assigning the value of NaN in x
	x = math.NaN()
	// printing the value of x
	fmt.Println("x:", x)

	// comparing NaN using math.NaN()
	// Note:
	// NaN is not considered equal to any number, including itself.
	// That's because it represnts a number outside
	// the range of representation.
	// Thus, the result will be false
	if x == math.NaN() {
		fmt.Println("The value of x is NaN")
	} else {
		fmt.Println("The value of x is not NaN")
	}

	// comparing NaN using math.IsNaN()
	if math.IsNaN(x) {
		fmt.Println("The value of x is NaN")
	} else {
		fmt.Println("The value of x is not NaN")
	}
}

Output:

x: NaN
The value of x is not NaN
The value of x 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.