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,

···

--
Posted via http://www.ruby-forum.com/.

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 12/21/2012 11:08 PM, Derrick B. wrote:

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

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]

···

--
Posted via http://www.ruby-forum.com/\.

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 Dec 21, 2012, at 9:08 PM, Derrick B. <lists@ruby-forum.com> wrote:

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

--
Posted via http://www.ruby-forum.com/\.

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.

···

Am 22.12.2012 07:11, schrieb rubyinfo:

Another way of indicating float literals is by putting 9.0 / 5.0

dave

--
<https://github.com/stomar/&gt;

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

···

--
Posted via http://www.ruby-forum.com/\.

What appears to happen is that Ruby does the division and rounds down to the nearest integer.

Alan

···

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.

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 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:

--
Matma Rex

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

···

Am 22.12.2012 11:55, schrieb Alan Forrester:

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

--
<https://github.com/stomar/&gt;

Quoting Matma Rex (matma.rex@gmail.com):

>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

···

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

On Sun, 23 Dec 2012 19:24:00 +0100, Derrick B. <lists@ruby-forum.com> wrote:

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Bartosz Dziewoński wrote in post #1090021:

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 Sun, 23 Dec 2012 19:24:00 +0100, Derrick B. <lists@ruby-forum.com> > wrote:

--
Posted via http://www.ruby-forum.com/\.

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: Class: Numeric (Ruby 1.9.3)

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

···

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

--
Matma Rex

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.

···

--
Posted via http://www.ruby-forum.com/\.