📜  红宝石 |有理rationalize()函数

📅  最后修改于: 2022-05-13 01:54:51.433000             🧑  作者: Mango

红宝石 |有理rationalize()函数

如果给定可选参数 eps (rat-|eps| <= result <= rat+|eps|),则rationalize()是 Ruby 中的内置函数,否则返回其自身。

示例 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