# How to avoid Fixnum rounding error

**URL:** <https://www.ruby-forum.com/t/how-to-avoid-fixnum-rounding-error/120515>\
**Category:** Ruby\
**Created:** [November 17, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/how-to-avoid-fixnum-rounding-error/120515 "2007-11-17T20:09:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 17, 2007, 8:09pm UTC](https://www.ruby-forum.com/t/how-to-avoid-fixnum-rounding-error/120515/1 "2007-11-17T20:09:55Z")

</div>

Which is the best approach to avoid division rounding errors when  
performing integer division with a Fixnum? Is there any built-in method  
for this?

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 17, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/how-to-avoid-fixnum-rounding-error/120515/2 "2007-11-17T20:46:12Z")

</div>

Quoth Jari W.:

> Which is the best approach to avoid division rounding errors when  
> performing integer division with a Fixnum? Is there any built-in method  
> for this?
> 
> Best regards,
> 
> Jari W.

Convert an argument to float before doing the operation.

5 / 2 # =\> 2  
5.to\_f / 2 # =\> 2.5

HTH,

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 17, 2007, 8:50pm UTC](https://www.ruby-forum.com/t/how-to-avoid-fixnum-rounding-error/120515/3 "2007-11-17T20:50:52Z")

</div>

Jari W. wrote:

> Which is the best approach to avoid division rounding errors when  
> performing integer division with a Fixnum? Is there any built-in method  
> for this?
> 
> Best regards,
> 
> Jari W.

I’m not sure what you’re trying to accomplish here. Fixnums by  
definition are integers. When you divide an integer by another integer,  
you can get either a quotient, a remainder (the “modulo” operation), or  
a list of both the quotient and remainder. For example, in irb:

irb(main):001:0\> fixnum1 = 5  
=\> 5  
irb(main):002:0\> fixnum2 = 7  
=\> 7  
irb(main):003:0\> fixnum1/fixnum2  
=\> 0  
irb(main):004:0\> fixnum1%fixnum2  
=\> 5  
irb(main):005:0\> fixnum1.divmod fixnum2  
=\> [0, 5]

There are two other options. You can convert the Fixnums to Floats and  
get their quotient as another Float:

irb(main):006:0\> fixnum1.to\_f/fixnum2.to\_f  
=\> 0.714285714285714

Or, you can enter the world of Rational numbers and represent the  
quotient of two integers as a Rational, like this:

irb(main):007:0\> require ‘mathn’  
=\> true  
irb(main):008:0\> fixnum1/fixnum2  
=\> 5/7

The tradeoffs between these last two options are (in most languages)  
performance versus accuracy. The floating point version will usually be  
faster, and the amount of memory required is fixed. But its accuracy is  
limited to about 15 decimal places in most machines.

The rational version will be slower, and the numbers involved can take  
up arbitrarily large amounts of memory as the computation progresses.  
But it is “exact”, in the sense that if the result you’re seeking is a  
rational number, that’s what you’ll get. If the result isn’t a rational  
number, you’ll get a rational number that’s close to it.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 17, 2007, 11:53pm UTC](https://www.ruby-forum.com/t/how-to-avoid-fixnum-rounding-error/120515/4 "2007-11-17T23:53:30Z")

</div>

Jari W. wrote:

> Which is the best approach to avoid division rounding errors when  
> performing integer division with a Fixnum? Is there any built-in method  
> for this?
> 
> Best regards,
> 
> Jari W.

Since nobody brought it up so far: 5.quo(2) # =\> 2.5, If you required  
Rational it will be Rational(5,2).

Regards,  
Stefan

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 18, 2007, 2:36am UTC](https://www.ruby-forum.com/t/how-to-avoid-fixnum-rounding-error/120515/5 "2007-11-18T02:36:01Z")

</div>

Stefan R. wrote:

> Rational it will be Rational(5,2).
> 
> Regards,  
> Stefan

I don’t really like the “Rational(5,2)” notation when “Mathn” will give  
you “5/2”.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 18, 2007, 1:50am UTC](https://www.ruby-forum.com/t/how-to-avoid-fixnum-rounding-error/120515/6 "2007-11-18T01:50:26Z")

</div>

> Jari W. wrote:
> 
> > Which is the best approach to avoid division rounding errors when  
> > performing integer division with a Fixnum? Is there any built-in method  
> > for this?

If you are talking about money and avoiding rounding errors on dollars  
and cents, you can use the Money class (google it) or you can multiply  
your dollars and cents by 100 to get an integer and then only deal  
with whole cent values.

Multiplying by 100 is not a complete solution if you are really  
tracking money and not just the price of an item which there might be  
multiple of, but it is an easy solution to an old problem if all you  
need to do is add tax and minus shipping without getting something  
like a final price of $33.01 when it should really be $33.

Regards

Mikel

> **[Mikel Lindsaar](https://lindsaar.net/)**
>
> Internet Home for Mikel Lindsaar
