# same operation (sort of), different results

Discussion in 'Ruby' started by Chad Perrin, Mar 30, 2007.

I'm a little confused by these results. Perhaps someone can tell me
what I'm assuming incorrectly:

\$ irb
irb(main):001:0> 5 / 9 * ( 100 - 32 )
=> 0
irb(main):002:0> ( 5 / 9 ) * ( 100 - 32 )
=> 0
irb(main):004:0> ( 100 - 32 ) * ( 5 / 9 )
=> 0
irb(main):003:0> ( 100 - 32 ) * 5 / 9
=> 37

The last result is the only one that gives me what I actually wanted.

In case you're wondering, yes, that *is* an F-to-C temperature
conversion.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"The ability to quote is a serviceable
substitute for wit." - W. Somerset Maugham

On Fri, Mar 30, 2007 at 02:30:33PM +0900, Chad Perrin wrote:
> I'm a little confused by these results. Perhaps someone can tell me
> what I'm assuming incorrectly:
>
> \$ irb
> irb(main):001:0> 5 / 9 * ( 100 - 32 )
> => 0
> irb(main):002:0> ( 5 / 9 ) * ( 100 - 32 )
> => 0
> irb(main):004:0> ( 100 - 32 ) * ( 5 / 9 )
> => 0
> irb(main):003:0> ( 100 - 32 ) * 5 / 9
> => 37
>
> The last result is the only one that gives me what I actually wanted.
>
> In case you're wondering, yes, that *is* an F-to-C temperature
> conversion.

Never mind, that was a full-on brain fart. I forgot I was doing integer
division. Mea culpa.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
Amazon.com interview candidate: "When C++ is your
hammer, everything starts to look like your thumb."

3. ### John JoyceGuest

Yep, you can do integer division but do it last for best results.
you can also throw a .to_f method onto everything for your division..
The only way around real division errors is to use large integers,
keep track of decimal location in your own custom object for display
purposes and thus push the division error way way down to a small,
insignificant number.
On Mar 30, 2007, at 2:34 PM, Chad Perrin wrote:

> On Fri, Mar 30, 2007 at 02:30:33PM +0900, Chad Perrin wrote:
>> I'm a little confused by these results. Perhaps someone can tell me
>> what I'm assuming incorrectly:
>>
>> \$ irb
>> irb(main):001:0> 5 / 9 * ( 100 - 32 )
>> => 0
>> irb(main):002:0> ( 5 / 9 ) * ( 100 - 32 )
>> => 0
>> irb(main):004:0> ( 100 - 32 ) * ( 5 / 9 )
>> => 0
>> irb(main):003:0> ( 100 - 32 ) * 5 / 9
>> => 37
>>
>> The last result is the only one that gives me what I actually wanted.
>>
>> In case you're wondering, yes, that *is* an F-to-C temperature
>> conversion.

>
> Never mind, that was a full-on brain fart. I forgot I was doing
> integer
> division. Mea culpa.
>
> --
> CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
> Amazon.com interview candidate: "When C++ is your
> hammer, everything starts to look like your thumb."
>

John Joyce, Mar 30, 2007
4. ### Robert KlemmeGuest

On 30.03.2007 08:10, John Joyce wrote:
> Yep, you can do integer division but do it last for best results.
> you can also throw a .to_f method onto everything for your division..
> The only way around real division errors is to use large integers, keep
> track of decimal location in your own custom object for display purposes
> and thus push the division error way way down to a small, insignificant
> number.

There is another way: BigDecimal.

robert

Robert Klemme, Mar 30, 2007
5. ### John JoyceGuest

I meant in general. For portability and dealing with float math.
Lots of solutions exist of course. One of them is Ruby's BigDecimal
On Mar 30, 2007, at 4:20 PM, Robert Klemme wrote:

> On 30.03.2007 08:10, John Joyce wrote:
>> Yep, you can do integer division but do it last for best results.
>> you can also throw a .to_f method onto everything for your
>> division..
>> The only way around real division errors is to use large integers,
>> keep track of decimal location in your own custom object for
>> display purposes and thus push the division error way way down to
>> a small, insignificant number.

>
> There is another way: BigDecimal.
>
> robert
>

John Joyce, Mar 30, 2007

On Fri, Mar 30, 2007 at 03:10:48PM +0900, John Joyce wrote:
> Yep, you can do integer division but do it last for best results.
> you can also throw a .to_f method onto everything for your division..
> The only way around real division errors is to use large integers,
> keep track of decimal location in your own custom object for display
> purposes and thus push the division error way way down to a small,
> insignificant number.

Thanks. I knew all this -- I just managed to completely forget
everything of use yesterday (the first day after a debilitating
migraine took me out of action for most of the day). Apparently, I
became temporarily very stupid as an after-effect. Unfortunately,
some of the evidence of this made it to the ruby-talk mailing list.

--
CCD CopyWrite Chad Perrin [ http://ccd.apotheon.org ]
"A script is what you give the actors. A program
is what you give the audience." - Larry Wall