Ruby | Rational rationalize() function
Last Updated :
19 Mar, 2024
The rationalize() is an inbuilt function in Ruby returns a simpler approximation of the value if the optional argument eps is given (rat-|eps| <= result <= rat+|eps|) otherwise returns its self.
Syntax: rat.rationalize(eps)
Parameters: The function accepts a single optional parameter
Return Value: It returns a simpler approximation of the value if the optional argument eps is given (rat-|eps| <= result <= rat+|eps|) otherwise returns its self
Example 1:
rat1 = Rational( 123 , 456 )
puts rat1.rationalize
|
Output:
41/152
Example 2:
rat1 = Rational( 123 , 456 )
puts rat1.rationalize(Rational( '.001' ))
|
Output:
7/26
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...