Open In App

Ruby | Numeric infinite? function

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The infinite?() is an inbuilt method in Ruby returns nil if the number is finite. It returns -1 and +1 if the number is -infinity or +infinity.

Syntax: num.infinite?()

Parameters: The function needs a number which is to be checked.

Return Value: It returns nil, -1 or +1.

Example 1:




# Ruby program for infinite?
# method in Numeric
  
# Initialize a number 
num1 = 12
  
# Prints Imaginary number
puts num1.infinite?()


Output:


Example 2:




# Ruby program for infinite?
# method in Numeric
  
# Initialize a number 
num1 = 12/0.0
num2 = -12/0.0
  
# Prints Imaginary number
puts num1.infinite?()
puts num2.infinite?()


Output:

1
-1

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads