math.IsNaN() Function in Golang with Examples

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)    
}

chevron_right


Output:

Result 1: false
Result 2: true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)")    
}

chevron_right


Output:

a is not-a-number



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.