Re: Bug #17984 Updated: comparison of float values does not always work.
| From: | Derick Rethans | Date: | Wed, 26 Jun 2002 20:09:17 +0000 |
| Subject: | Re: Bug #17984 Updated: comparison of float values does not always work. | ||
| References: | 1 2 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-85614@lists.php.net to get a copy of this message | ||
Hello,
it's sad, but the best explanation I could find is @ M$:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q42980
the section "Floating-Point Complications"
Derick
On Wed, 26 Jun 2002 szii@sziisoft.com wrote:
> You know what? I understand that's it's not a "PHP specific bug."
> The test script I sent you does basic addition. No multiplication and
> nothing past precision 2.
>
> Yes I know the "rule of thumb" but don't you think PHP should
> "do the right thing" whenever possible?
>
> There's no reason I can find why the addition would fail. It's not
> a repeating sequence. It doesn't have digits out past the second
> field and doesn't do any multiplication/division/exponential work
> which would affect "hidden" precision numbers.
>
> It's not a support question. I don't expect a reply. I already know
> all the binary reasons for fp numbers and why not to compare them
> as a rule.
>
> Thanks.
>
> -Mike
>
>
> ----- Original Message -----
> From: "PHP Bug Database" <php-bugs@lists.php.net>
> To: <szii@sziisoft.com>
> Sent: Tuesday, June 25, 2002 10:23 PM
> Subject: Bug #17984 Updated: comparison of float values does not always work.
>
>
> > ATTENTION! Do NOT reply to this email!
> > To reply, use the web interface found at
> > http://bugs.php.net/?id=17984&edit=2
> >
> >
> > ID: 17984
> > Updated by: derick@php.net
> > Reported By: szii@sziisoft.com
> > -Status: Open
> > +Status: Bogus
> > Bug Type: Unknown/Other Function
> > Operating System: Linux Mandrake
> > PHP Version: 4.2.1
> > New Comment:
> >
> > Sorry, but the bug system is not the appropriate forum for asking
> > support questions. Your problem does not imply a bug in PHP itself.
> > For a list of more appropriate places to ask for help using PHP,
> > please visit http://www.php.net/support.php
> >
> > Thank you for your interest in PHP.
> >
> > http://www.php.net/manual/en/language.types.float.php
> >
> >
> > Previous Comments:
> > ------------------------------------------------------------------------
> >
> > [2002-06-25 18:53:42] szii@sziisoft.com
> >
> > Comparison of float values does not always work.
> >
> > ./configure --with-mysql=/usr/local/mysql --with-xml
> > --with-apache=../../apache/apache_1.3.26/
> >
> > Test code follows:
> >
> > <?php
> >
> > $a = 332.06;
> > $b = 45.11 + 134.38 + 85.35 + 67.22;
> >
> > echo "output\n";
> >
> > var_dump($a);
> > var_dump($b);
> >
> > print("Total = $a\n");
> > print("Production = $b\n");
> >
> >
> > if($a != $b)
> > {
> > print("Totals do not Match!\n");
> > }
> > else {
> > print("Totals are in Balance\n");
> > }
> >
> > if($a !== $b)
> > {
> > print("Totals do not Match!\n");
> > }
> > else {
> > print("Totals are in Balance\n");
> > }
> >
> > if($a <> $b)
> > {
> > print("Totals do not Match!\n");
> > }
> > else {
> > print("Totals are in Balance\n");
> > }
> >
> > if($a > $b)
> > {
> > print("a is greater!\n");
> > }
> > else {
> > print("b is greater!\n");
> > }
> >
> > settype($a,"string");
> > settype($b,"string");
> >
> > if ($a != $b)
> > echo "String compare failed!\n\n";
> >
> >
> > settype($a,"float");
> > settype($b,"float");
> >
> > if ($a != $b)
> > echo "recast to float compare failed!\n\n";
> >
> > echo "end output\n";
> > ?>
> >
> > -----------------------------
> > output
> > float(332.06)
> > float(332.06)
> > Total = 332.06
> > Production = 332.06
> > Totals do not Match!
> > Totals do not Match!
> > Totals do not Match!
> > b is greater!
> > end output
> > ------------------------------
> >
> > Summary:
> > float compare fails (!=)
> > float compare fails (<>)
> > float compare fails (!==)
> > "b" is somehow greater than "a"
> > cast to string, compare okay.
> > recast to float, compare okay.
> >
> > -Mike
> >
> > ------------------------------------------------------------------------
> >
>
---------------------------------------------------------------------------
Derick Rethans http://www.derickrethans.nl/
JDI Media Solutions http://www.jdimedia.nl/
---------------------------------------------------------------------------