Golang math.IsNaN() Function with Examples

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

math.IsNaN()

The IsNaN() function is an inbuilt function of the math package which is used to check whether the given parameter is an IEEE 754 "not-a-number" value.

It accepts a parameter (f), and checks whether f is an IEEE 754 "not-a-number" value.

Syntax:

func IsNaN(f float64) (is bool)

Parameter(s):

  • f : The value to be checked.

Return Value:

The return type of IsNaN() function is a bool, it checks whether the given parameter is an IEEE 754 "not-a-number" value.

Example 1:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.IsNaN(math.Sqrt(-3)))
	fmt.Println(math.IsNaN(math.NaN()))

	fmt.Println(math.IsNaN(math.Inf(-3)))
	fmt.Println(math.IsNaN(10))
}

Output:

true
true
false
false

Example 2:

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

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var IsNaNX bool

	x = 1
	IsNaNX = math.IsNaN(x)
	fmt.Println("IsNaN(", x, ") = ", IsNaNX)

	x = 10.5
	IsNaNX = math.IsNaN(x)
	fmt.Println("IsNaN(", x, ") = ", IsNaNX)

	x = 0
	IsNaNX = math.IsNaN(x)
	fmt.Println("IsNaN(", x, ") = ", IsNaNX)

	x = math.Sqrt(-1)
	IsNaNX = math.IsNaN(x)
	fmt.Println("IsNaN(", x, ") = ", IsNaNX)

	x = math.Inf(1)
	IsNaNX = math.IsNaN(x)
	fmt.Println("IsNaN(", x, ") = ", IsNaNX)

	x = math.NaN()
	IsNaNX = math.IsNaN(x)
	fmt.Println("IsNaN(", x, ") = ", IsNaNX)
}

Output:

IsNaN( 1 ) =  false
IsNaN( 10.5 ) =  false
IsNaN( 0 ) =  false
IsNaN( NaN ) =  true
IsNaN( +Inf ) =  false
IsNaN( NaN ) =  true

Golang math Package Constants and Functions »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.