📜  Ruby Float divmod() 方法与示例

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

Ruby Float divmod() 方法与示例

Float divmod()是一个浮点类方法,它返回一个数组,该数组具有两个数相除的商和余数。

示例 #1:

Ruby
# Ruby code for divmod() method
 
# Initializing value
a = 4.0
b = 2.0
 
# Printing result
puts "Division a/b : #{a.divmod(b)}\n\n"
puts "Division b/a : #{b.divmod(a)}\n\n"


Ruby
# Ruby program for divmod() method
 
# Initializing value
a = 0
b = 2.0
 
# dividing by zero - gives error
puts "Division a/b : #{a.divmod(b)}\n\n"
puts "Division b/a : #{b.divmod(a)}\n\n"


输出 :

Division a/b : [2, 0.0]

Division b/a : [0, 2.0]

示例 #2:

红宝石

# Ruby program for divmod() method
 
# Initializing value
a = 0
b = 2.0
 
# dividing by zero - gives error
puts "Division a/b : #{a.divmod(b)}\n\n"
puts "Division b/a : #{b.divmod(a)}\n\n"

输出 :

source_file.rb:8:in `divmod': divided by 0 (ZeroDivisionError)
    from source_file.rb:8:in `'

Division a/b : [0, 0.0]