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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 22:01:33 2024 UTC