Strange behavior of while loop

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

[email protected] 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.

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. [email protected]

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

http://www.regdeveloper.co.uk/2006/08/12/floating_point_approximation/
http://en.wikipedia.org/wiki/Floating_point

Julian.

This is even more fun :wink:

(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.

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

http://www.regdeveloper.co.uk/2006/08/12/floating_point_approximation/
http://en.wikipedia.org/wiki/Floating_point

http://docs.sun.com/source/806-3568/ncg_goldberg.html is one of the
classics on this subject.

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs