php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52188 Arithmetic Operator: Addition maybe have a problem
Submitted: 2010-06-26 01:05 UTC Modified: 2010-06-26 01:10 UTC
From: hostmaster at ovi dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.2 OS: openSuSE
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: hostmaster at ovi dot com
New email:
PHP Version: OS:

 

 [2010-06-26 01:05 UTC] hostmaster at ovi dot com
Description:
------------
PHP version: 5.3.2

I think addition operator have a problem.
Pls run the script below and the code in "Test script" box.

<?php
$a = 16.99;
$b = 6.99;
$c = 10;

if ($a < ($b + $c)) {
        print "<";
} else if ($a == ($b + $c)) {
        print "==";
} else {
        print ">";
}

print "\n";
print $a .":". ($b + $c);
print "\n";
?>

If the number is 16.98, the result will be ok.

Test script:
---------------
<?php
for ($x=0;$x<100;$x++ ) {
        for ($y=10;$y<100;$y++) {
                $str = $x.'.'.$y;
                print $str;
                $a = floatVal($str);
                $b = floatVal($x);
                $c = floatVal('0.'.$y);
                if ($a < ($b + $c)) {
                        print "<";
                } else if ($a == ($b + $c)) {
                        print "==";
                } else {
                        print ">";
                }
                print "\n";
        }
}



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-26 01:10 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-06-26 01:10 UTC] rasmus@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.

.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 09 23:01:33 2025 UTC