
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a Given Number is Finite in Golang
In this article we will write a go language program to check if a given number is finite or not. The program uses IsInf() and IsNaN() library functions in order to check whether numbers are finite or not.
Syntax
func IsInf(f float64, sign int) bool
IsInf() function is present in the math package. This function takes two arguments one is the floating point number that we wish to check and the other is the sign. The sign is an integer which can be greater than, lesser than, or equal to zero. If the sign is greater than zero then this function checks if the floating point number is positive infinity and checks negative infinity if the sign is lesser than zero. Further if sign is equal to zero then the function checks if the given number is finite or not and returns a Boolean variable based on the result.
func IsNaN(x float64) (is bool)
IsNaN() function is present in math package. This function takes a floating point number as an argument and checks if the number is a number or Not-A-Number. if the given number is a number then it returns true. On the contrary, it returns false if the provided number is NaN.
Algorithm
Step 1 ? First, we need to import the fmt and math package.
Step 2 ? Then, create a function called isFinite() that takes a floating point number as an argument and returns a Boolean value.
Step 3 ? This function uses IsInf() and IsNaN() function to check whether the given number is finite or not.
Step 4 ? Now, start the main() function. Initialize a variable called num and result to hold the number to be checked and a Boolean variable to carry the result. We are using four examples in the first one initialize num with 1.23 and print it on the screen.
Step 5 ? Now, to check whether the number is finite or not pass the number along with the sign as argument to the function isFinite() and store the result in result variable.
Step 6 ? If the value obtained by the function is true then print that the given number is finite otherwise print that the number is not finite.
Step 7 ? Repeat the process with some more examples. Also check the result with NaN and negative real numbers.
Example
In this example we will use an internal function in go programing language to check if the provided number is finite or not. We will create a function that will accept the number to be checked as argument and returns the appropriate result after doing the respective calculation.
package main import ( "fmt" "math" ) func isFinite(num float64) bool { return !math.IsInf(num, 0) && !math.IsNaN(num) } func main() { var num float64 var result bool // example 1 num = 1.23 fmt.Println("Example 1") fmt.Println("The number to be checked is:", num) result = isFinite(num) if result { fmt.Println("The given number is finite") } else { fmt.Println("The number is not finite") } fmt.Println() // example 2 num = 1 fmt.Println("Example 2") fmt.Println("The number to be checked is:", num) result = isFinite(num) if result { fmt.Println("The given number is finite") } else { fmt.Println("The number is not finite") } fmt.Println() // example 3 fmt.Println("Example 3") fmt.Println("The number to be checked is: NaN") result = isFinite(math.NaN()) if result { fmt.Println("The given number is finite") } else { fmt.Println("The number is not finite") } fmt.Println() // example 4 num = -2.34 fmt.Println("Example 4") fmt.Println("The number to be checked is:", num) result = isFinite(num) if result { fmt.Println("The given number is finite") } else { fmt.Println("The number is not finite") } }
Output
Example 1 The number to be checked is: 1.23 The given number is finite Example 2 The number to be checked is: 1 The given number is finite Example 3 The number to be checked is: NaN The number is not finite Example 4 The number to be checked is: -2.34 The given number is finite
Conclusion
We have successfully compiled and executed a go language program to check whether a given number is finite or not. We have implemented four different kinds of examples in this program. The program is using a separate function that accepts the variable to be checked as an argument to the function and returns the Boolean result which we can use to print the output accordingly.