same operation (sort of), different results

C

Chad Perrin

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

Chad Perrin

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

John Joyce

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

Robert Klemme

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
 
J

John Joyce

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

Chad Perrin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top