Modulo operation in Ruby
Today I was stumped by a very simple yet awesome problem by my friend Sandip
What is the answer of : 7 % -2
and to the surprise ruby irb printed it as : -1
After some research I what I found about this and why here is explanation and links
From Ruby Programming Language, O,really
Division, Modulo, and Negative Numbers
When one (but not both) of the operands is negative, Ruby performs the integer division and
modulo operations differently than languages like C, C++, and Java do (but the same as the
languages Python and Tcl). Consider the quotient -7/3. The floating-point result is –2.33. The
result of integer division must be an integer, however, so this number must be rounded. Ruby
rounds toward negative infinity and returns –3. C and related languages round toward zero
instead and return –2. (This is just one way to characterize the results; no floating-point division
is actually done, of course.)
An important corollary of Ruby's definition of integer division is that, in Ruby, -a/b equals
a/-b but may not equal -(a/b).
Ruby's definition of the modulo operation also differs from that of C and Java. In Ruby, –7%3 is
2.
In C and Java, the result is -1 instead. The magnitude of the result differs, of course, because
the value of the quotient differs. But the sign of the result differs, too. In Ruby, the sign of the
result is always the same as the sign of the second operand. In C and Java, the sign of the result is
always the same as the sign of the first operand. (Ruby also provides a remainder method that
behaves, in sign and magnitude, like the C modulo operator.)
More Links I suggest to must see for more better understanding
http://stackoverflow.com/questions/7395205/modulus-operand-in-ruby-compared-to-php
http://en.wikipedia.org/wiki/Modulus_operator#Remainder_calculation_for_the_modulo_operation
http://www.ruby-doc.org/core-1.9.3/Numeric.html#method-i-25
http://www.ruby-doc.org/core-1.9.3/Numeric.html#method-i-divmod
thank you so much for sharing this - so helpful!
ReplyDeleteThankyou for this article. Really a useful content to read.
ReplyDeletedata science training in chennai
ccna training in chennai
iot training in chennai
cyber security training in chennai
ethical hacking training in chennai
no deposit bonus forex 2021 - takipçi satın al - takipçi satın al - takipçi satın al - takipcialdim.com/tiktok-takipci-satin-al/ - instagram beğeni satın al - instagram beğeni satın al - google haritalara yer ekleme - btcturk - tiktok izlenme satın al - sms onay - youtube izlenme satın al - google haritalara yer ekleme - no deposit bonus forex 2021 - tiktok jeton hilesi - tiktok beğeni satın al - binance - takipçi satın al - uc satın al - finanspedia.com - sms onay - sms onay - tiktok takipçi satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - tiktok takipçi satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - perde modelleri - instagram takipçi satın al - instagram takipçi satın al - cami avizesi - marsbahis
ReplyDeletesmm panel
ReplyDeleteSmm panel
iş ilanları
instagram takipçi satın al
hirdavatciburada.com
Https://www.beyazesyateknikservisi.com.tr
servis
tiktok jeton hilesi
yurtdışı kargo
ReplyDeleteen son çıkan perde modelleri
özel ambulans
uc satın al
lisans satın al
en son çıkan perde modelleri
nft nasıl alınır
minecraft premium