# 2 - 2.01?

**URL:** <https://www.ruby-forum.com/t/2-2-01/117604>\
**Category:** Ruby\
**Created:** [October 18, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/2-2-01/117604 "2007-10-18T14:49:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [October 18, 2007, 2:49pm UTC](https://www.ruby-forum.com/t/2-2-01/117604/1 "2007-10-18T14:49:37Z")

</div>

Hey,  
How come 2 - 2.01 = -0.00999999999999979 and not -0.01  
???  
Can someone explain this to me please?

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [October 18, 2007, 2:55pm UTC](https://www.ruby-forum.com/t/2-2-01/117604/2 "2007-10-18T14:55:54Z")

</div>

On 10/18/07, Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey,  
> How come 2 - 2.01 = -0.00999999999999979 and not -0.01  
> ???  
> Can someone explain this to me please?

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/273263](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/273263)

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [October 18, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/2-2-01/117604/3 "2007-10-18T14:57:08Z")

</div>

Michal S. wrote:

> Hey,  
> How come 2 - 2.01 = -0.00999999999999979 and not -0.01  
> ???  
> Can someone explain this to me please?

Because 0.01 cannot be represented accurately in binary. See  
[Floating-point arithmetic - Wikipedia](http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems).

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [October 18, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/2-2-01/117604/4 "2007-10-18T14:58:49Z")

</div>

On Thu, Oct 18, 2007 at 09:49:37PM +0900, Michal S. wrote:

> Hey,  
> How come 2 - 2.01 = -0.00999999999999979 and not -0.01  
> ???  
> Can someone explain this to me please?

> **[Floating-point arithmetic | Accuracy problems](https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems)**
>
> The fact that floating-point numbers cannot accurately represent all real numbers, and that floating-point operations cannot accurately represent true arithmetic operations, leads to many surprising situations. This is related to the finite precision with which computers generally represent numbers.
> For example, the decimal numbers 0.1 and 0.01 cannot be represented exactly as binary floating-point numbers. In the IEEE 754 binary32 format with its 24-bit significand, the result of attempting to ...

–Greg

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [October 18, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/2-2-01/117604/5 "2007-10-18T15:01:12Z")

</div>

On Thu, 18 Oct 2007 21:49:37 +0900, Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hey,  
> How come 2 - 2.01 = -0.00999999999999979 and not -0.01  
> ???  
> Can someone explain this to me please?

Because you are using floating point.

Reas this article for an explanation:

“What Every Computer Scientist Should Know About Floating-Point  
Arithmetic”

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

Kristian

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [October 18, 2007, 3:02pm UTC](https://www.ruby-forum.com/t/2-2-01/117604/6 "2007-10-18T15:02:22Z")

</div>

On Oct 18, 8:49 am, Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Hey, How come 2 - 2.01 = -0.00999999999999979 and not -0.01 ??? Can someone explain this to me please?
> 
> Posted viahttp://www.ruby-forum.com/.

This question comes up a lot. Google “floating point errors” for some  
good links.
