Open In App

math.IsNaN() Function in Golang with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

Go language provides inbuilt support for basic constants and mathematical functions to perform operations on the numbers with the help of the math package. This package provides IsNaN() function which is used to check whether x is an IEEE 754 “not-a-number” value or not. This function return true if x is an IEEE 754 “not-a-number” value. Otherwise, this function will returns false. So, you need to add a math package in your program with the help of the import keyword to access the IsNaN() function.

Syntax:

func IsNaN(x float64) (is bool)

Example 1:




// Golang program to illustrate 
// math.IsNaN() Function
  
package main
    
import (
    "fmt"
    "math"
)
    
// Main function
func main() {
     
// Checking the specified value
// is not-a-number or not
// Using IsNaN() function
a1 := 4.4
res1 := math.IsNaN(a1)
fmt.Println("Result 1:", res1)
  
a2 := math.NaN()
res2 := math.IsNaN(a2)
fmt.Println("Result 2:", res2)    
}


Output:

Result 1: false
Result 2: true

Example 2:




// Golang program to illustrate 
// math.IsNaN() Function
package main
    
import (
    "fmt"
    "math"
)
    
// Main function
func main() {
     
// Checking the specified value 
// is not-a-number or not
// Using IsNaN() function
a := math.NaN()
res := math.IsNaN(a)
if (res == true){
    fmt.Println("a is not-a-number")
}else{
fmt.Println("a is not a NaN(not-a-number)")    
}


Output:

a is not-a-number


Last Updated : 28 Apr, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads