# Strange behavior of while loop

**URL:** <https://www.ruby-forum.com/t/strange-behavior-of-while-loop/133414>\
**Category:** Rails\
**Created:** [March 31, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-while-loop/133414 "2008-03-31T12:30:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-while-loop/133414/1 "2008-03-31T12:30:26Z")

</div>

hai all,

I am very new to Ruby and when I am doing some small program I  
found the strange output for my program. Please let me know why this  
is happening…

num = 10.1

while num \< 10.5  
puts "num = " + num.to\_s  
num = num + 0.1  
end

output that I am getting is :

num = 10.1  
num = 10.2  
num = 10.3  
num = 10.4  
num = 10.5

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2008, 1:41pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-while-loop/133414/2 "2008-03-31T13:41:24Z")

</div>

removed\_email\_address@domain.invalid wrote:

> num = 10.1  
> while num \< 10.5  
> puts "num = " + num.to\_s  
> num = num + 0.1  
> end
> 
> output that I am getting is :  
> num = 10.1  
> num = 10.2  
> num = 10.3  
> num = 10.4  
> num = 10.5

Floating point numbers are not represented exactly. Google for  
“floating point accuracy” and I’m sure you’ll find lots of information  
about this. If you need exactly tenths, then use integers at ten times  
the size and divide by ten when displaying:

num = 101  
while num \< 105  
puts “num = #{num/10.0}”  
num += 1  
end

num = 10.1  
num = 10.2  
num = 10.3  
num = 10.4

Note you need to divide by 10.0 to get a float as the result as  
otherwise Ruby would do integer division.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-while-loop/133414/3 "2008-03-31T15:20:55Z")

</div>

You could also use BigDecimal instead of float:

num = BigDecimal(“10.1”)  
while num \< BigDecimal(“10.5”) do  
puts “num = #{num.to\_s}”  
num += BigDecimal(“0.1”)  
end

On Mar 31, 7:41 am, Mark B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-while-loop/133414/4 "2008-03-31T15:21:31Z")

</div>

Float rounding errors

> > num = 10.1  
> > =\> 10.1  
> > num += 0.1  
> > =\> 10.2  
> > num += 0.1  
> > =\> 10.3  
> > num += 0.1  
> > =\> 10.4  
> > num += 0.1  
> > =\> 10.5  
> > num \< 10.5  
> > =\> true  
> > num  
> > =\> 10.5

> **[The trouble with rounding floating point numbers](https://www.theregister.com/2006/08/12/floating_point_approximation/)**
>
> So, Prudence, computers do make mistakes…

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

Julian.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-while-loop/133414/5 "2008-03-31T15:59:39Z")

</div>

This is even more fun 😉

> > (10.1 \* 1000).to\_i  
> > =\> 10100
> 
> > ((10.1 + 0.1) \* 1000).to\_i  
> > =\> 10200
> 
> > ((10.1 + 0.1 + 0.1) \* 1000).to\_i  
> > =\> 10299

Julian.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 31, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/strange-behavior-of-while-loop/133414/6 "2008-03-31T16:25:51Z")

</div>

On 31 Mar 2008, at 14:20, Julian L. wrote:

> [The trouble with rounding floating point numbers • The Register](http://www.regdeveloper.co.uk/2006/08/12/floating_point_approximation/)  
> [Floating-point arithmetic - Wikipedia](http://en.wikipedia.org/wiki/Floating_point)

[http://docs.sun.com/source/806-3568/ncg\_goldberg.html](http://docs.sun.com/source/806-3568/ncg_goldberg.html) is one of the  
classics on this subject.

Fred
