# Simple division: -9 / 5 = -2 what?

\$ 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,

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

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 22 Dec 2012, at 08:45, [email protected] 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

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.)

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 Sun, 23 Dec 2012 19:24:00 +0100, Derrick B. [email protected]
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

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 )
–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

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.

Subject: Re: simple division: -9 / 5 = -2 what?
Date: Mon 24 Dec 12 03:32:57AM +0900

Quoting Matma R. ([email protected]):

On Sun, 23 Dec 2012 19:24:00 +0100, Derrick B. [email protected] 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 Sun, 23 Dec 2012 21:39:42 +0100, Carlo E. Prelz [email protected]
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#method-i-divmod

I suggest just not doing integer division with negative numbers, less
trouble.

Bartosz Dziewoński wrote in post #1090021:

On Sun, 23 Dec 2012 19:24:00 +0100, Derrick B. [email protected]
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

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 P.ming
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

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.