This is classic floating point rounding/truncation symptom. The two values are calculated in different ways so end up being rounded/truncated differently and become very slightly different values. You could fix it by storing cents rather than dollars (integers rather than floats).
DWIM is Perl's answer to Gödel
| [reply] |
| [reply] |
if not necessary, don't calculate with numbers with after comma positions because in many cases there are storing/rounding problems.
Better calculate in cent, not in dollars, and so you just need to store and caculate with plain integers
Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"
| [reply] |
This is why us oldtimers have always stored prices in cents, not dollars. Accountants don't like rounding errors, and the bean counters rule the world.
--
tbone1, YAPS (Yet Another Perl Schlub)
And remember, if he succeeds, so what.
- Chick McGee
| [reply] |
Hmm...
Are you sure your values are as you say they are before the -= op?
$ cat tam
#!/usr/bin/perl
$cid = 1;
$c{$cid}{total} = 194.55;
$tp = 194.55;
$c{$cid}{total} -= $tp;
print "new val: $c{$cid}{total}\n";
$ perl tam
new val: 0
$/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ }
+ sig
map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu
+" );
| [reply] [d/l] [select] |
Thanks everyone. I did a sprintf( "%6.2f", $value); on everything before the subtraction and it's giving me the correct results. | [reply] |