# Bad maths

**URL:** <https://rubytalk.org/t/bad-maths/11634>\
**Category:** ruby-talk\
**Created:** [19 May 2004 22:56 UTC](https://rubytalk.org/t/bad-maths/11634 "2004-05-19T22:56:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Larsson1](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Martin\_Larsson1](https://rubytalk.org/u/Martin_Larsson1)\
**Post date:** [19 May 2004 22:56 UTC](https://rubytalk.org/t/bad-maths/11634/1 "2004-05-19T22:56:01Z")

</div>

Hi

i = 0  
100.times {  
p i += 0.6  
}

results in:  
5.6  
5.7  
5.8  
5.9  
5.99999999999999  
6.09999999999999  
6.19999999999999

on my XP maching with ruby 1.8.1-13. I used rubyinstaller to install it  
if that matters

anyone more have this problem? anyway to fix it?

---

<div class="post-metadata">

**Author:** ![Martin\_Larsson1](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@Martin\_Larsson1](https://rubytalk.org/u/Martin_Larsson1)\
**Post date:** [19 May 2004 22:58 UTC](https://rubytalk.org/t/bad-maths/11634/2 "2004-05-19T22:58:21Z")

</div>

Martin larsson wrote:

> Hi
> 
> i = 0  
> 100.times {  
> p i += 0.6  
> }
> 
> results in:  
> 5.6  
> 5.7  
> 5.8  
> 5.9  
> 5.99999999999999  
> 6.09999999999999  
> 6.19999999999999
> 
> on my XP maching with ruby 1.8.1-13. I used rubyinstaller to install  
> it if that matters
> 
> anyone more have this problem? anyway to fix it?

sry, i was testing around with different numbers so i copied wrong  
result for that litte script, just change 0.6 to 0.1 and the the result  
is right

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [19 May 2004 23:10 UTC](https://rubytalk.org/t/bad-maths/11634/3 "2004-05-19T23:10:02Z")

</div>

Martin larsson wrote:

> Hi
> 
> i = 0  
> 100.times {  
> p i += 0.6  
> }
> 
> results in:  
> 5.6  
> 5.7  
> 5.8  
> 5.9  
> 5.99999999999999  
> 6.09999999999999  
> 6.19999999999999
> 
> on my XP maching with ruby 1.8.1-13. I used rubyinstaller to install it  
> if that matters
> 
> anyone more have this problem? anyway to fix it?

Look for a thread about a week or so ago. 🙂

I suggest you round off the numbers for output and/or for  
calculations. And use integer arithmetic wherever you can.

Hal

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [20 May 2004 00:18 UTC](https://rubytalk.org/t/bad-maths/11634/4 "2004-05-20T00:18:48Z")

</div>

Martin larsson wrote:

> Hi

Moin!

> i = 0  
> 100.times {  
> p i += 0.6  
> }

Try this:

100.times { |i| p (i + 1) \* 0.6 }

Regards,  
Florian Gross

---

<div class="post-metadata">

**Author:** ![Lloyd\_Zusman](https://avatars.discourse-cdn.com/v4/letter/l/8baadc/32.png) [@Lloyd\_Zusman](https://rubytalk.org/u/Lloyd_Zusman)\
**Post date:** [19 May 2004 23:20 UTC](https://rubytalk.org/t/bad-maths/11634/5 "2004-05-19T23:20:47Z")

</div>

Martin larsson [morg@borgeby.se](mailto:morg@borgeby.se) writes:

> Martin larsson wrote:
> 
> > Hi
> > 
> > i = 0  
> > 100.times {  
> > p i += 0.6  
> > }
> > 
> > results in:  
> > 5.6  
> > 5.7  
> > 5.8  
> > 5.9  
> > 5.99999999999999  
> > 6.09999999999999  
> > 6.19999999999999
> > 
> > on my XP maching with ruby 1.8.1-13. I used rubyinstaller to install  
> > it if that matters
> > 
> > anyone more have this problem? anyway to fix it?
> 
> sry, i was testing around with different numbers so i copied wrong  
> result for that litte script, just change 0.6 to 0.1 and the the result  
> is right

First, look up “floating point arithmetic” on Google (include the  
double quotes in your search).

This link is also quite useful:

[http://docs.sun.com/source/806-3568/ncg\_goldberg.html](http://docs.sun.com/source/806-3568/ncg_goldberg.html)

Then, read the recent thread in this list called “Major addition bug?”.  
This thread starts with the following message ID:

[200405120948.51036.sean@celsoft.com](mailto:200405120948.51036.sean@celsoft.com)

> **···**
>
> –  
> Lloyd Zusman  
> [ljz@asfast.com](mailto:ljz@asfast.com)  
> God bless you.

---

<div class="post-metadata">

**Author:** ![oinkoink](https://avatars.discourse-cdn.com/v4/letter/o/8e8cbc/32.png) [@oinkoink](https://rubytalk.org/u/oinkoink)\
**Post date:** [20 May 2004 04:53 UTC](https://rubytalk.org/t/bad-maths/11634/6 "2004-05-20T04:53:48Z")

</div>

Martin larsson [morg@borgeby.se](mailto:morg@borgeby.se) wrote in message news:[40ABE654.7070601@borgeby.se](mailto:40ABE654.7070601@borgeby.se)…

> Martin larsson wrote:  
> [repeated adding of 0.1 not coming out exact]

The problem is that the computer thinks in binary and  
0.1 has an infinitely long binary representation. (You  
won’t see this “problem” if you instead add 0.5, which  
has a finitely long representation) This is not a ruby  
problem, but a limitation of the computer’s floating point.  
In general, you should regard floating point numbers as  
being slightly inexact. If you need exactness, require  
the mathn library and use rationals. Imitating your example,  
you’d get something like:

require ‘mathn’  
i = 5  
11.times {puts i += 1/10} # =\> exact rational results

As an alternative, you could limit the precision of your  
output, which effectively rounds the numbers to output  
precision:

i = 5.5  
5.times { puts “%5.3f” % (i += 0.1)}  
#output follows:  
5.600  
5.700  
5.800  
5.900  
6.000

Regards, Bret  
Bret Jolly (Jou Lii Bair)

---

<div class="post-metadata">

**Author:** ![oinkoink](https://avatars.discourse-cdn.com/v4/letter/o/8e8cbc/32.png) [@oinkoink](https://rubytalk.org/u/oinkoink)\
**Post date:** [20 May 2004 06:08 UTC](https://rubytalk.org/t/bad-maths/11634/7 "2004-05-20T06:08:49Z")

</div>

A small addendum to my previous answer. If you need exact decimal  
arithmetic (as you do if the decimals represent dollars and cents)  
you should use the BigDecimal class. That works in base 10 rather  
than base 2. (Actually, it works in base 10000, which is mathematically  
equivalent to base 10.) Imitating your example:  
require 'bigdecimal’  
a = BigDecimal(“5.5”, 20) # the 20 indicates the precision  
5.times {puts (a += BigDecimal(“0.1”)).to\_s}

# output follows:

0.56E1  
0.57E1  
0.58E1  
0.59E1  
0.6E1

No nonsense about 5.99999… here.

The BigDecimal class is useful when you want the arithmetic to  
be decimal rather than binary, and especially when you need extra  
precision beyond what floating-point can achieve. It is, however,  
slower than floating point, a bit trickier to use (since you have  
to set precision or make sure the implicit settings are acceptable)  
and its method library is less rich than what the Math module  
provides for Float.

Regards, Bret
