ADVERTISEMENT
ADVERTISEMENT

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 »


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.