Horje
Ruby Float rationalize() method with example

Float rationalize() is a float class method which return the simple rational form (p/q) of a float value.

Syntax: float.rationalize()

Parameter: float value as argument

Return: Simple approximation value

Example #1:




# Ruby program for rationalize() method
  
# Initialize value
a = 0.767
b = 2999.011
  
# Printing result
puts "Rationalizing a : #{a.rationalize}\n\n"
puts "Rationalizing b : #{b.rationalize}\n\n"

Output :

Rationalizing a : 767/1000

Rationalizing b : 2999011/1000

Example #2:




# Ruby code for rationalize() method
  
# Initialize value
a = 0.767
b = 2999.011
c = 2.0000
  
# Printing result
puts "Rationalizing a : #{a.rationalize(0.01)}\n\n"
puts "Rationalizing b : #{b.rationalize(0.00001)}\n\n"
puts "Rationalizing c : #{c.rationalize(0.1)}\n\n"

Output :

Rationalizing a : 10/13

Rationalizing b : 1634461/545

Rationalizing c : 2/1



Reffered: https://www.geeksforgeeks.org


Ruby

Related
Ruby Float zero?() method with example Ruby Float zero?() method with example
Ruby Integer next function with example Ruby Integer next function with example
Ruby Integer modulo() function with example Ruby Integer modulo() function with example
Ruby Integer magnitude function with example Ruby Integer magnitude function with example
Ruby Integer lcm() function with example Ruby Integer lcm() function with example

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
9