# Ruby float calculations

**URL:** <https://www.ruby-forum.com/t/ruby-float-calculations/79084>\
**Category:** Ruby\
**Created:** [March 9, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/ruby-float-calculations/79084 "2006-03-09T17:57:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![analoguedragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dec6dc/32.png) [@analoguedragon](https://www.ruby-forum.com/u/analoguedragon)\
**Post date:** [March 9, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/ruby-float-calculations/79084/1 "2006-03-09T17:57:20Z")

</div>

Could someone please explain to me why the following happens:

irb(main):001:0\> 0.29 - 0.38 + 0.1  
=\> 0.00999999999999998  
irb(main):002:0\> 0.29 + (-0.38 + 0.1)  
=\> 0.00999999999999995  
irb(main):003:0\> 0.29 + 0.1 - 0.38  
=\> 0.01

How can I prevent something like this from happening, or at the very  
least how can I work around such errors?

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![analoguedragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dec6dc/32.png) [@analoguedragon](https://www.ruby-forum.com/u/analoguedragon)\
**Post date:** [March 9, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/ruby-float-calculations/79084/2 "2006-03-09T18:07:48Z")

</div>

On Fri, 10 Mar 2006, Alexandru T. wrote:

> least how can I work around such errors?
> 
> Many thanks in advance.

it is a hardware limitation, even a C program will do the same:

```
 harp:~ > cat a.c
 #include <stdlib.h>
 #include <stdio.h>

 main () {
   printf ("%32.32f\n", 0.29 - 0.38 + 0.1);
   printf ("%32.32f\n", 0.29 + (-0.38 + 0.1));
   printf ("%32.32f\n", 0.29 + 0.1 - 0.38);
 }

 harp:~ > gcc a.c && a.out
 0.00999999999999998112620858137234
 0.00999999999999995337063296574343
 0.01000000000000000888178419700125

```

with ruby, at least, you can use BigDecimal or the like - but with a  
serious  
speed penalty. in short this is just the way computers work:

[Floating-point arithmetic - Wikipedia](http://en.wikipedia.org/wiki/Floating_point)

hth.

-a

---

<div class="post-metadata">

**Author:** ![analoguedragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dec6dc/32.png) [@analoguedragon](https://www.ruby-forum.com/u/analoguedragon)\
**Post date:** [March 9, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/ruby-float-calculations/79084/3 "2006-03-09T18:07:48Z")

</div>

On 3/9/06, Alexandru T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Could someone please explain to me why the following happens:
> 
> irb(main):001:0\> 0.29 - 0.38 + 0.1  
> =\> 0.00999999999999998  
> irb(main):002:0\> 0.29 + (-0.38 + 0.1)  
> =\> 0.00999999999999995  
> irb(main):003:0\> 0.29 + 0.1 - 0.38  
> =\> 0.01

Check this out:

d1 = 0.29 - 0.38 + 0.1  
d2 = 0.29 + (-0.38 + 0.1)  
d3 = 0.29 + 0.1 - 0.38  
p (d3 - d2)  
p Float::EPSILON  
(d3 - d2) \< Float::EPSILON #=\> true

Computers do not store numbers indefinitely, so there is an “accuracy”  
associated with floating point operations. For more information,  
check out:  
[Floating-point arithmetic - Wikipedia](http://en.wikipedia.org/wiki/Floating_point)

Cameron

---

<div class="post-metadata">

**Author:** ![analoguedragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dec6dc/32.png) [@analoguedragon](https://www.ruby-forum.com/u/analoguedragon)\
**Post date:** [March 9, 2006, 6:47pm UTC](https://www.ruby-forum.com/t/ruby-float-calculations/79084/4 "2006-03-09T18:47:45Z")

</div>

> My question now would be if there is an easy way to implement  
> floating-point decimal arithmetic in Ruby.

Forget about this… it has already been answered… I should have been  
more careful

> with ruby, at least, you can use BigDecimal or the like - but with a  
> serious  
> speed penalty. in short this is just the way computers work:

Thanks again

---

<div class="post-metadata">

**Author:** ![analoguedragon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dec6dc/32.png) [@analoguedragon](https://www.ruby-forum.com/u/analoguedragon)\
**Post date:** [March 9, 2006, 6:41pm UTC](https://www.ruby-forum.com/t/ruby-float-calculations/79084/5 "2006-03-09T18:41:46Z")

</div>

> [Floating-point arithmetic - Wikipedia](http://en.wikipedia.org/wiki/Floating_point)

Thanks for your answers. It’s clear now. Expecially this link on the IBM  
site made it very clear:  
[http://www2.hursley.ibm.com/decimal/decifaq1.html#inexact](http://www2.hursley.ibm.com/decimal/decifaq1.html#inexact)

My question now would be if there is an easy way to implement  
floating-point decimal arithmetic in Ruby.
