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]