[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

float math?

Tim McIntyre

11/19/2007 9:05:00 PM

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

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






1 Answer

Jano Svitok

11/19/2007 9:16:00 PM

0

On Nov 19, 2007 10:05 PM, Tim McIntyre <tmac@easystreet.com> wrote:
> 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

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