> now, a stupid solution is:
> 
> for (my $value = -1000; $value <= 1000; $value += 100) {
>         print $value/1000, "\n";
> }
> 
> hehe,
> 

Sadly, it's not as stupid as you think. Unless I misunderstand things, what 
you are seeing here is a problem called IEEE 754 floating point.

I'm sure there is some explanation that someone could offer as to why it's the 
"best" thing, but IEEE754 doesn't represent simple decimals very well. It 
converts them into binary using an odd method allowing it to represent the 
number in one chunk, avoiding the mantissa and exponent form.

However, this encoding can't represent any decimal not ending in 5 finitely, 
much the same way it's not possible to represent 1/3 in decimal finitely.

The upshot of this is that unless you *really* need floating point math, and 
are willing to do what is necessary to compensate for the error that will 
creep in, you should stay away from it.

Smart folks will often represent monetary values in hundreths or thousanths of 
cents, just to avoid floating point math.

-- 
May we have the clarity to see our work, the courage to embrace it, and the 
capacity to discharge it.
http://www.hacksaw.org -- http://www.privatecircus.com -- KB1FVD



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to