# Same operation (sort of), different results

**URL:** <https://www.ruby-forum.com/t/same-operation-sort-of-different-results/90604>\
**Category:** Ruby\
**Created:** [March 30, 2007, 7:30am UTC](https://www.ruby-forum.com/t/same-operation-sort-of-different-results/90604 "2007-03-30T07:30:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [March 30, 2007, 7:30am UTC](https://www.ruby-forum.com/t/same-operation-sort-of-different-results/90604/1 "2007-03-30T07:30:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [March 30, 2007, 7:35am UTC](https://www.ruby-forum.com/t/same-operation-sort-of-different-results/90604/2 "2007-03-30T07:35:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [March 30, 2007, 8:12am UTC](https://www.ruby-forum.com/t/same-operation-sort-of-different-results/90604/3 "2007-03-30T08:12:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [March 30, 2007, 9:21am UTC](https://www.ruby-forum.com/t/same-operation-sort-of-different-results/90604/4 "2007-03-30T09:21:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [March 30, 2007, 6:05pm UTC](https://www.ruby-forum.com/t/same-operation-sort-of-different-results/90604/5 "2007-03-30T18:05:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [March 30, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/same-operation-sort-of-different-results/90604/6 "2007-03-30T13:41:11Z")

</div>

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