Float math?

hey all,

I’ve got a delimited file with lines like so…

1234^2007-11-17^4321^Peter Pan^123 test
st^^portland^ir^97101^US^^^Statement 34567^87.92^Statement for
2007-11-17

quick script to total col 13

require ‘csv’
file = “myFile.txt”
total = 0.0
CSV.open(file, “r”, ‘^’) do |row|
print "#{total} + "
total += row[13].to_f
puts “#{row[13]} = #{total}”
end

output:

2415.06 + 20.99 = 2436.05


6704.58 + 87.92 = 6792.5
6792.5 + 75.0 = 6867.5
6867.5 + 15.98 = 6883.48
<= so far so good!
6883.48 + 15.99 = 6899.46999999999 <= uhh oh…
what happened?
6899.46999999999 + 50.0 = 6949.46999999999
6949.46999999999 + 32.0 = 6981.46999999999

Why do all the extra decimal places all of the sudden come into play?

Thanks for your time!
Tim

On Nov 19, 2007 10:05 PM, Tim McIntyre [email protected] wrote:

require ‘csv’
2415.06 + 20.99 = 2436.05

Why do all the extra decimal places all of the sudden come into play?

Thanks for your time!
Tim

Search the archive for ‘float’, or possibly ‘float precision’. It’s a
FAQ.