Forum: Ruby simple division: -9 / 5 = -2 what?

A9ad4fa7d205855e06fb550edc6eea02?d=identicon&s=25 Derrick B. (dbuckhal)
on 2012-12-22 06:08
$ 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,
D16f7e99e707acbf06db6a4338f4e613?d=identicon&s=25 rubyinfo (Guest)
on 2012-12-22 07:11
(Received via mailing list)
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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
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]
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2012-12-22 09:46
(Received via mailing list)
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.
9c06a675f6937389c102d09e7ae58b93?d=identicon&s=25 Alan Forrester (aforrester)
on 2012-12-22 11:56
(Received via mailing list)
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
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2012-12-22 12:09
(Received via mailing list)
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.)
A9ad4fa7d205855e06fb550edc6eea02?d=identicon&s=25 Derrick B. (dbuckhal)
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
39093dd2b68b68960fecd0fe2b9a5045?d=identicon&s=25 Bartosz Dziewoński (matmarex)
on 2012-12-23 19:33
(Received via mailing list)
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.
51ded646955ab405c035e20f3f1827e2?d=identicon&s=25 andg (Guest)
on 2012-12-23 21:03
(Received via mailing list)
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.
3853dd5371ac1e094fc45d6c2aa0e459?d=identicon&s=25 Carlo E. Prelz (Guest)
on 2012-12-23 21:40
(Received via mailing list)
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
39093dd2b68b68960fecd0fe2b9a5045?d=identicon&s=25 Bartosz Dziewoński (matmarex)
on 2012-12-23 22:07
(Received via mailing list)
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.
A9ad4fa7d205855e06fb550edc6eea02?d=identicon&s=25 Derrick B. (dbuckhal)
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
A9ad4fa7d205855e06fb550edc6eea02?d=identicon&s=25 Derrick B. (dbuckhal)
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

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.