December 15th, 2011, 09:46 AM

Rounding error? (absolute beginner)
Hi there,
First of all: I'm an absolute beginner in Ruby, so please don't shoot me if I'm asking stupid questions...
Here's the problem:
Code:
print("Length: ")
s = gets()
length = s.to_f
print("Width: ")
s = gets()
width = s.to_f
surface = length * width
puts("Surface = #{length} x #{width} = #{surface}")
When running this code, it asks for Length and Width as expected, but when I type the values 4.9 and 5.9 it gives 28.910000000000004 as result.
The values 3.9 and 3.9 give 15.20999999999999 as result.
Those results are not what I expected them to be...
What do I need to do to avoid these roundingerrors?
Greetings, and thanks!
J.
January 15th, 2012, 03:11 AM

That is very strange... I have no idea (I'm new also) but think it's damn weird Ruby would do that.
I also think it's neat how I used a completely different code to do the same thing you did. Even though our codes look so very different, they do the same thing! Well... I think it's neat anyway
Code:
puts 'Length:'
length = gets.chomp
puts 'Width:'
width = gets.chomp
surface = (length.to_f * width.to_f)
puts surface
January 16th, 2012, 02:01 PM

Actually, this is to be expected when using floatingpoint arithmetic, and is not specific to Ruby; it will occur in any language using IEEE754 floatingpoint numbers.
Because the FP numbers are actually in base 2 rather than base 10, they cannot accurately represent certain decimal fractions  what in base 10 may be an exact fraction could be a repeating fraction in base 2, and vis versa. The best solution is to use BigDecimal numbers instead of floats:
Code:
require "bigdecimal"
print("Length: ")
s = gets()
length = BigDecimal.new(s)
print("Width: ")
s = gets()
width = BigDecimal.new(s)
surface = length * width
puts("Surface = #{length.to_s('F')} x #{width.to_s('F')} = #{surface.to_s('F')}")
Now, BigDecimal has some quirks of it's own  by default, the string representation returned by to_s() is in exponential notation, for example  but it does provide exact decimal fractions.
Last edited by ScholRLEA; January 16th, 2012 at 02:05 PM.