红宝石 |有理rationalize()函数
如果给定可选参数 eps (rat-|eps| <= result <= rat+|eps|),则rationalize()是 Ruby 中的内置函数,否则返回其自身。
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
示例 1 :
# Ruby program for rationalize() method
# Initialize rational number
rat1 = Rational(123, 456)
# Prints the rational number
puts rat1.rationalize
输出:
41/152
示例 2 :
# Ruby program for rationalize() method
# Initialize rational number
rat1 = Rational(123, 456)
# Prints the rational number
puts rat1.rationalize(Rational('.001'))
输出:
7/26