php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17984 comparison of float values does not always work.
Submitted: 2002-06-25 18:53 UTC Modified: 2002-06-26 01:23 UTC
From: szii at sziisoft dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 4.2.1 OS: Linux Mandrake
Private report: No CVE-ID: None
 [2002-06-25 18:53 UTC] szii at sziisoft dot 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-26 01:23 UTC] derick@php.net
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
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Oct 25 11:01:23 2020 UTC