Golang : Test floating point numbers not-a-number and infinite example
A simple example to demonstrate how to create not-a-number
and infinite
floating-point values in Golang. Will show some example tests on the values and things to watch out for in order to get the most accurate result.
Here you go!
package main
import (
"fmt"
"math"
)
func main() {
fnum := float64(12.2)
fmt.Println(fnum)
fmt.Println("Is not-a-number ? : ", math.IsNaN(fnum))
fNaN := math.NaN()
fmt.Println(fNaN)
fmt.Println("Is not-a-number ? : ", math.IsNaN(fNaN))
// test if floating-point value is infinity
fmt.Println("Is positive infinity? : ", math.IsInf(fNaN, 1))
fmt.Println("Is negative infinity? : ", math.IsInf(fNaN, -1))
fmt.Println("Is either infinity? : ", math.IsInf(fNaN, 0))
// reference https://golang.org/pkg/math/#IsInf
fmt.Println("---------------------------------------------")
fmt.Println("Test positive infinite value")
fmt.Println("---------------------------------------------")
posInf := math.Inf(1)
posInf = posInf + 12.2 // infinite value will still propagate after add operation
fmt.Println("Symbol : ", posInf)
fmt.Println("Is positive infinity? : ", math.IsInf(posInf, 1))
fmt.Println("Is negative infinity? : ", math.IsInf(posInf, -1))
fmt.Println("Is either infinity? : ", math.IsInf(posInf, 0))
fmt.Println("---------------------------------------------")
fmt.Println("Watch out for operation such as 10 / posInf !!")
fmt.Println("---------------------------------------------")
posInf = 10 / posInf // HOWEVER! this will screw up the infinite
fmt.Println("Symbol : ", posInf)
fmt.Println("Is positive infinity? : ", math.IsInf(posInf, 1))
fmt.Println("Is negative infinity? : ", math.IsInf(posInf, -1))
fmt.Println("Is either infinity? : ", math.IsInf(posInf, 0))
fmt.Println("---------------------------------------------")
fmt.Println("Test negative infinite value")
fmt.Println("---------------------------------------------")
negInf := math.Inf(-1)
fmt.Println("Symbol : ", negInf)
fmt.Println("Is postive infinity? : ", math.IsInf(negInf, 1))
fmt.Println("Is negative infinity? : ", math.IsInf(negInf, -1))
fmt.Println("Is either infinity? : ", math.IsInf(negInf, 0))
fmt.Println("---------------------------------------------")
fmt.Println("Watch out for operation such as infinite / infinite !!")
fmt.Println("---------------------------------------------")
negInf = negInf / negInf // --- WILL turn infinite into NaN
fmt.Println("Symbol : ", negInf)
fmt.Println("Is postive infinity? : ", math.IsInf(negInf, 1))
fmt.Println("Is negative infinity? : ", math.IsInf(negInf, -1))
fmt.Println("Is either infinity? : ", math.IsInf(negInf, 0))
}
Output:
12.2
Is not-a-number ? : false
NaN
Is not-a-number ? : true
Is positive infinity? : false
Is negative infinity? : false
Is either infinity? : false
---------------------------------------------
Test positive infinite value
---------------------------------------------
Symbol : +Inf
Is positive infinity? : true
Is negative infinity? : false
Is either infinity? : true
---------------------------------------------
Watch out for operation such as 10 / posInf !!
---------------------------------------------
Symbol : 0
Is positive infinity? : false
Is negative infinity? : false
Is either infinity? : false
---------------------------------------------
Test negative infinite value
---------------------------------------------
Symbol : -Inf
Is postive infinity? : false
Is negative infinity? : true
Is either infinity? : true
---------------------------------------------
Watch out for operation such as infinite / infinite !!
---------------------------------------------
Symbol : NaN
Is postive infinity? : false
Is negative infinity? : false
Is either infinity? : false
Happy coding!
Reference:
See also : Golang : Accurate and reliable decimal calculations
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8k Golang : Check if integer is power of four example
+32.5k Golang : How to check if a date is within certain range?
+6.9k Golang : Use modern ciphers only in secure connection
+5.3k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+16.4k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+7.1k Golang : alternative to os.Exit() function
+4.9k PHP : See installed compiled-in-modules
+10.3k Golang : Get local time and equivalent time in different time zone
+13.9k Golang : How to convert a number to words
+5.8k Golang : Dealing with backquote
+40.7k Golang : How to count duplicate items in slice/array?
+15.8k Golang : How to check if input from os.Args is integer?