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. 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!?
Also, if 3.55 is represented "internally" as something else, ie.
3.550000001, then 63.67 must be something else as well. So then why does
casting to a string, then back to a double make them equal? Everything has
been ruled out except the addition, which is where I suspect the problem
lies. Again, a computer can compute that 3.55 + 60.12 is equal to 63.67
without taking the math out to 16 decimal places, even if the precision is
that high. The zeros why out there simply stay zeros.
I'm going to stop ranting and go check PHP's source, make some tests in C
and such. But with this type of number representation, PHP becomes
inaccurate in all but the most simple use of numbers.
Matthew
At 11:05 PM 12/4/2001 +0100, Derick Rethans wrote:
>Hello,
>
>this is how floating point numbers work.
>
>3.55 as float is not 'exactly' 3.55, but it is stored in memory as an as
>good as possible representation, something like 3.55000000001 perhaps.
>
>If you simply compare this:
>3.55000000000001 + 60.12000000000000001 != 63.66666666666669
>
>so that's why the comparison fails.
>
>Derick
>
>On Tue, 4 Dec 2001, Matthew Hagerty wrote:
>
> > Greetings,
> >
> > Explain to me why the first condition fails!
> >
> > <?php
> >
> > $fFloat1 = 3.55 + 60.12;
> > $fFloat2 = 63.67;
> >
> > if ( $fFloat1 == $fFloat2 )
> > print "Equal";
> > else
> > print "Not Equal";
> >
> > settype($fFloat1, "string");
> > settype($fFloat1, "double");
> >
> > if ( $fFloat1 == $fFloat2 )
> > print "Equal";
> > else
> > print "Not Equal";
> >
> > ?>
> >
> > Thanks,
> > Matthew
> >
> >
> >
> > --
> > 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]
> >
--
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]