$ irb irb(main):001:0> -9 / 5 => -2 irb(main):002:0> -9.to_f / 5.to_f => -1.8 irb(main):003:0> (-9.to_f / 5.to_f).to_i => -1 If I want to assign the integer quotient of the above example, do I really need to go from float to integer? Thanks,
on 2012-12-22 06:08
on 2012-12-22 07:11
I believe ruby followed c in the convention of integer/integer => integer so you get 9/5 => 2 Another way of indicating float literals is by putting 9.0 / 5.0 dave
on 2012-12-22 08:53
Derrick B. wrote in post #1089918: > $ irb > irb(main):001:0> -9 / 5 > => -2 > irb(main):002:0> -9.to_f / 5.to_f > => -1.8 > irb(main):003:0> (-9.to_f / 5.to_f).to_i > => -1 > > If I want to assign the integer quotient of the above example, do I > really need to go from float to integer? > No: p -9.divmod 5 --output:-- [-2, 1]
on 2012-12-22 09:46
Am 22.12.2012 07:11, schrieb rubyinfo: > I believe ruby followed c in the convention of integer/integer => integer > so you get 9/5 => 2 No, 9 / 5 => 1 ! Integer division with quotient 1, remainder (modulus) 4.
on 2012-12-22 11:56
On 22 Dec 2012, at 08:45, sto.mar@web.de wrote: > Am 22.12.2012 07:11, schrieb rubyinfo: >> I believe ruby followed c in the convention of integer/integer => integer >> so you get 9/5 => 2 > > No, 9 / 5 => 1 ! > > Integer division with quotient 1, remainder (modulus) 4. What appears to happen is that Ruby does the division and rounds down to the nearest integer. Alan
on 2012-12-22 12:09
Am 22.12.2012 11:55, schrieb Alan Forrester: > > What appears to happen is that Ruby does the division and rounds down to the nearest integer. > > Alan See `ri Numeric#divmod' for a detailed description and examples: for integers a and b, a/b returns floor(a/b). (Also note that remainder and modulus are not the same.)
on 2012-12-23 19:23
7stud -- wrote in post #1089926: > Derrick B. wrote in post #1089918: >> $ irb >> irb(main):001:0> -9 / 5 >> => -2 >> irb(main):002:0> -9.to_f / 5.to_f >> => -1.8 >> irb(main):003:0> (-9.to_f / 5.to_f).to_i >> => -1 >> >> If I want to assign the integer quotient of the above example, do I >> really need to go from float to integer? >> > > No: > > p -9.divmod 5 > > --output:-- > [-2, 1] a = -9 b = 5 p a.divmod b )[0] --output:-- -2 But I want a "correct" answer, which is not -2. Hence: p ( a.to_f / b.to_f ).to_i --output:-- -1 ...which is ugly, or better (maybe?): p (a.fdiv b).to_i --output:-- -1
on 2012-12-23 19:33
On Sun, 23 Dec 2012 19:24:00 +0100, Derrick B. <lists@ruby-forum.com>
wrote:
> But I want a "correct" answer, which is not -2. Hence:
Why? -9 / 5 = -2 with a remainder of 1, because -2 * 5 + 1 = -9.
The math checks out, it's your intuition that's wrong. If you want the
numbers to behave as if they were positive, use their absolute value and
then adjust the sign.
on 2012-12-23 21:03
You are looking for (0.0-9)/5 or -9.0/5 or -9/5.0 To ruby numerals are integers unless they are bigints.
on 2012-12-23 21:40
Subject: Re: simple division: -9 / 5 = -2 what? Date: Mon 24 Dec 12 03:32:57AM +0900 Quoting Matma Rex (matma.rex@gmail.com): > On Sun, 23 Dec 2012 19:24:00 +0100, Derrick B. <lists@ruby-forum.com> wrote: > > >But I want a "correct" answer, which is not -2. Hence: > > Why? -9 / 5 = -2 with a remainder of 1, because -2 * 5 + 1 = -9. Yes, but... Why is it that this C code: #include "stdio.h" void main(void) { int i1=-9,i2=5; printf("%d/%d=%d\n",i1,i2,i1/i2); } produces this output: -9/5=-1 ? I don't know which one is right, but I thought both C and Ruby were doing integer maths in the same way... Carlo
on 2012-12-23 22:07
On Sun, 23 Dec 2012 21:39:42 +0100, Carlo E. Prelz <fluido@fluido.as> wrote: > > I don't know which one is right, but I thought both C and Ruby were > doing integer maths in the same way... Both are right. You can perform the division is two ways: one that gives negative remainders and one that doesn't. The precise behavior of Ruby implementation is documented here: http://www.ruby-doc.org/core-1.9.3/Numeric.html#me... I suggest just not doing integer division with negative numbers, less trouble.
on 2012-12-23 23:15
Bartosz Dziewoński wrote in post #1090021: > On Sun, 23 Dec 2012 19:24:00 +0100, Derrick B. <lists@ruby-forum.com> > wrote: > >> But I want a "correct" answer, which is not -2. Hence: > > Why? -9 / 5 = -2 with a remainder of 1, because -2 * 5 + 1 = -9. > > The math checks out, it's your intuition that's wrong. If you want the > numbers to behave as if they were positive, use their absolute value and > then adjust the sign. How can my intuition be wrong when you are not asking in what way I require that arithmetic operation to perform? You are showing your intuition to be wrong. "The math checks out" How? What is your basis for that statement? In the general sense of "math", an answer of "-1.8" would be that which checks out. Ruby rounds to negative infinity when one of two operands of an integer division operation is negative, hence -9 / 5 = -2 ("The Ruby Programming Language" book). So, the correct statement is "The Ruby math checks out" ruby -le 'print (-9 / 5).to_i' output: -2 and I can add to it with "The Perl math checks out" perl -le 'print int(-9 / 5)' output: -1
on 2012-12-23 23:21
Carlo E. Prelz wrote in post #1090035: > > I don't know which one is right, but I thought both C and Ruby were > doing integer maths in the same way... > > Carlo So did I until I translated a C++ homework assignment to Perl, then to Ruby. (See my other thread if you do not already have enough to read. heh ) C++ and Perl are similar in how they round negative quotients of integer division , but from what I read in "The Ruby Programming Language" book, page 45, Ruby is opposite.
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.