# Ruby floating point behavior typical of other langs?

**URL:** <https://www.ruby-forum.com/t/ruby-floating-point-behavior-typical-of-other-langs/75745>\
**Category:** Ruby\
**Created:** [November 13, 2006, 2:55am UTC](https://www.ruby-forum.com/t/ruby-floating-point-behavior-typical-of-other-langs/75745 "2006-11-13T02:55:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradjpeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@bradjpeek](https://www.ruby-forum.com/u/bradjpeek)\
**Post date:** [November 13, 2006, 2:55am UTC](https://www.ruby-forum.com/t/ruby-floating-point-behavior-typical-of-other-langs/75745/1 "2006-11-13T02:55:46Z")

</div>

The second edition of _The\_Ruby\_Way_ has an example similar to the  
following:

irb(main):001:0\> puts ‘not equal’ unless (3.2 - 2.0) == 1.2  
not equal  
=\>nil

The point is to illustrate why you might want to use BigDecimal (i.e.  
so 3.2 - 2.0 would in fact = 1.2).

require ‘bigdecimal’  
x = BigDecimal(“3.2”)  
y = BigDecimal(“2.0”)  
z = BigDecimal(“1.2”)  
p x - y == z ? “equal” : “not equal” # prints “equal”

I’m fairly new to Ruby and don’t do much programming, but when I saw  
this example I was surprised that the default behavior is that 3.2 -  
2.0 != 1.2

To me, this violates the “Principal of least surprise”, but I guess it  
isn’t a big deal because I don’t remember it being discussed in  
Programming Ruby book (but it certainly may have been).

Do other languages work this way?

---

<div class="post-metadata">

**Author:** ![bradjpeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@bradjpeek](https://www.ruby-forum.com/u/bradjpeek)\
**Post date:** [November 13, 2006, 3:03am UTC](https://www.ruby-forum.com/t/ruby-floating-point-behavior-typical-of-other-langs/75745/2 "2006-11-13T03:03:46Z")

</div>

On 11/12/06, bradjpeek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> To me, this violates the “Principal of least surprise”, but I guess it  
> isn’t a big deal because I don’t remember it being discussed in  
> Programming Ruby book (but it certainly may have been).
> 
> Do other languages work this way?

Yep. This is pretty standard.  
This article is tolerable, but rambles a bit:

> **[Floating-point arithmetic](https://en.wikipedia.org/wiki/Floating_point)**
>
> In computing, floating-point arithmetic (FP) is arithmetic that represents a subset of real numbers using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base.
> Numbers of this form are called floating-point numbers.: 3 : 10 
> For example, 12.345 is a floating-point number in a base-ten representation with five digits of precision:
> However, 12.3456 is not a floating-point number with five base-ten digits of precision.
> In practice, most floatin...

---

<div class="post-metadata">

**Author:** ![bradjpeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@bradjpeek](https://www.ruby-forum.com/u/bradjpeek)\
**Post date:** [November 13, 2006, 3:51am UTC](https://www.ruby-forum.com/t/ruby-floating-point-behavior-typical-of-other-langs/75745/3 "2006-11-13T03:51:50Z")

</div>

Wilson B. wrote:

> > so 3.2 - 2.0 would in fact = 1.2).
> 
> [Floating-point arithmetic - Wikipedia](http://en.wikipedia.org/wiki/Floating_point)  
> There was a personal computer that had a language in ROM  
> that used binary-coded-decimal floating point.  
> Using that language, 3.2 - 2.0 yielded precisely 1.2.

---

<div class="post-metadata">

**Author:** ![bradjpeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a183cd/32.png) [@bradjpeek](https://www.ruby-forum.com/u/bradjpeek)\
**Post date:** [November 13, 2006, 8:24am UTC](https://www.ruby-forum.com/t/ruby-floating-point-behavior-typical-of-other-langs/75745/4 "2006-11-13T08:24:11Z")

</div>

bradjpeek wrote:

/ …

> I’m fairly new to Ruby and don’t do much programming, but when I saw  
> this example I was surprised that the default behavior is that 3.2 -  
> 2.0 != 1.2
> 
> To me, this violates the “Principal of least surprise”,

Do you mean the “principle of least suprise”? One word means primary,  
the  
other means standard or canon. They sound the same, but that’s all they  
have in common.

> but I guess it  
> isn’t a big deal because I don’t remember it being discussed in  
> Programming Ruby book (but it certainly may have been).
> 
> Do other languages work this way?

All of them that use binary internal storage, yes. A decimal value like  
1.2  
is a continuing fraction in binary and cannot be precisely represented  
in  
binary in a finite number of places. Sort of like 1/3 in either binary  
or  
decimal.

> **[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 ...
