(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
I'm finding some simple subtractions that result in negative numbers
give wierd results. I know I could use sprintf to format it, but
shouldn't this just work?
My perl version is v5.8.6 built for i586-linux-thread-multi.
I swear, they should teach kids about assembly language, registers, and
data representations. Why, back in my day....
</old-person-rant>
Okay. Duuuude. You think computers have infinite precision? No, that
would require infinite memory. Ah, but I hear you say, "17.50 doesn't
require infinite precision, just two decimal places! Same with 18.30!"
Right. *Decimal places*. Computers do computations in *binary*. The
numbers 17.50 and 18.30 simply *cannot* be represented exactly in the
commonly-used binary floating-point representations used in most modern
computers. Suck it up. (actually, I lied. 17.50 *can* be represented
exactly. But not 18.30).
You cannot expect 17.50 plus 0.80 to equal 18.30. You have to allow for
this. (Yeah, you'd expect a modern proglang like Perl to account for
this, but it doesn't. Oh well.)
Read the FAQ; also read up on data representation of floating-point
numbers.
--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`