Same operation (sort of), different results

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.

On Fri, Mar 30, 2007 at 02:30:33PM +0900, Chad P. wrote:

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.

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 30.03.2007 08:10, John J. 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

On Fri, Mar 30, 2007 at 03:10:48PM +0900, John J. 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.

I meant in general. For portability and dealing with float math.
Lots of solutions exist of course. One of them is Ruby’s BigDecimal