How to find remainder of a division in Ruby?
Solution 1
The modulo operator:
> 208 % 11
=> 10
Solution 2
If you need just the integer portion, use integers with the /
operator, or the Numeric#div
method:
quotient = 208 / 11
#=> 18
quotient = 208.0.div 11
#=> 18
If you need just the remainder, use the %
operator or the Numeric#modulo
method:
modulus = 208 % 11
#=> 10
modulus = 208.0.modulo 11
#=> 10.0
If you need both, use the Numeric#divmod
method. This even works if either the receiver or argument is a float:
quotient, modulus = 208.divmod(11)
#=> [18, 10]
208.0.divmod(11)
#=> [18, 10.0]
208.divmod(11.0)
#=> [18, 10.0]
Also of interest is the Numeric#remainder
method. The differences between all of these can be seen in the documentation for divmod
.
Solution 3
please use Numeric#remainder because mod is not remainder
Modulo:
5.modulo(3)
#=> 2
5.modulo(-3)
#=> -1
Remainder:
5.remainder(3)
#=> 2
5.remainder(-3)
#=> 2
here is the link discussing the problem https://rob.conery.io/2018/08/21/mod-and-remainder-are-not-the-same/
Shpigford
Maker. Dabbler. Founder of Baremetrics. I can't stop starting things. Cedar & Sail, Laser Tweets, Founder Chats, Droptune, Rockburg. Bearded.
Updated on July 09, 2022Comments
-
Shpigford almost 2 years
I'm trying to get the remainder of a division using Ruby.
Let's say we're trying to divide 208 by 11.
The final should be "18 with a remainder of 10"...what I ultimately need is that
10
.Here's what I've got so far, but it chokes in this use case (saying the remainder is
0
).division = 208.to_f / 11 rounded = (division*10).ceil/10.0 remainder = rounded.round(1).to_s.last.to_i
-
Shpigford over 12 yearsHow the HECK did I make it so complicated? Thank you.
-
Michael Kohl over 12 yearsI don't want to nitpick, but it's not an operator, but a method of the
Fixnum
class:>> 208.%(11) #=> 10
-
fbonetti almost 11 years@MichaelKohl Technically all ruby "operators" are methods. For example,
5 + 5
is really just shorthand for5.+(5)
. -
Ben Aubin about 8 years@fbonetti not all...
&
,|
,and
,not
, and a few other operators are not methods. -
fbonetti about 8 years@penne12 That's not entirely true.
&
,|
are methods onFixnum
; they are NOT operators: ruby-doc.org/core-2.2.0/Fixnum.html#method-i-26. -
fbonetti about 8 years@penne12 here's a complete list of all ruby keywords: java2s.com/Code/Ruby/Language-Basics/Rubysreservedwords.htm
-
Ben Aubin about 8 years@fbonetti sorry - I meant
&&
and||
-
noraj about 4 yearsThe
divmod
method was exactly what I was looking for, needing both remainder and integer portion. -
Zane almost 3 yearsI wholeheartedly disagree with the claim that there is a uniqe definition for remainder and modulo. There are implementations in programming languages - a modulo in algebra is related, but not the same. Note that 11 = 3 mod 4 in algebra, and not 11 mod 4 = 3.
-
Adam Sibik over 2 yearsI must disagree with @Zane's above claim. In algebra, the expression
11 = 3 mod 4
does not contain an operator;mod
is not an operator here. It's merely a description of the number system for3
. To avoid confusion, it'd even be typically written11 = 3 (mod 4)
. -
Zane over 2 years@AdamSibik. I don't know why you think you "must disagree". From my point of your view, your remark supports the claim: there is no unique definition.