Home »
Golang »
Golang Reference
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
Parameters
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 »