At 00:38 05/12/2001, Matthew Hagerty wrote:
>Okay, then why when I ask PHP to show me the value of $fFloat1, do I get this:
>
>printf("%f", $fFloat1);  -> 63.670000
>
>Not some long draw out number like 63.67000000001 or even 
>63.66999999999...  If PHP uses precision past the 6 digit then it needs to 
>show me that it does.
>
>I can't agree with your reason because computers are not that imprecise.

They are.  Generally, the equality operator on floats is inaccurate by 
definition, you learn that in Numeric Analysis 101 :)  Comparing floating 
point numbers should only be done by comparing ranges.  E.g., something like

abs($num1-$num2)<0.00001

>   Doing math with a double is the same no matter where you stick the 
> decimal point, so why would $i = 99999999 be any different than $i = 
> .99999999??  I'll go check the datasheet for AMD's and Intel's floating 
> point units and see how they say number representation is supposed to be 
> just to make sure.
>
>However, even if the number is not stored as indicated, ie. 3.55000000001 
>instead of a clean 3.550000000, then why does PHP take the liberty to chop 
>off that precision when converting to a string?  And why is that precision 
>not put back on when going back to a double?  It is not put back on 
>because PHP can represent 3.55 as a clean 3.5500000000, so the assignment 
>of floats, ie. $fFloat = 3.55;, is coded in error in PHP's internals!?

There's no clean 3.55, there simply isn't.  It's just how computers 
work.  The only way to do what you're asking for is to switch to a whole 
different string-based representation, which is going to slow things down 
very considerably.

It was annoying to discover this reality for me as well when I took this 
course a few years ago, but it's reality all the same.  You can search the 
Web for this issue, e.g.
http://www.google.com/search?q=%22never+compare%22+%22floating+point+numbers%22

Zeev


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to