PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > float math?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
float math?

Réponse
 
LinkBack Outils de la discussion
Vieux 19/11/2007, 22h05   #1
Tim McIntyre
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut float math?

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






  Réponse avec citation
Vieux 19/11/2007, 22h16   #2
Jano Svitok
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: float math?

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.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 20h41.


Édité par : vBulletin® version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10840 seconds with 10 queries