php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70261 Error when comparing floating point
Submitted: 2015-08-13 18:13 UTC Modified: 2015-08-13 18:59 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: egdn2004 at gmail dot com Assigned:
Status: Not a bug Package: Reproducible crash
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
 [2015-08-13 18:13 UTC] egdn2004 at gmail dot com
Description:
------------
Error when comparing floating point with two decimal places

Test script:
---------------
<?php

if (9.2 > 9.0 + 0.2) {
    echo 'WRONG';
} else {
    echo 'OK';
}

Expected result:
----------------
OK

Actual result:
--------------
WRONG

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-13 18:24 UTC] egdn2004 at gmail dot com
Test script:
---------------
<?php
$var1 = 9.22;
$var2 = 9.20;

$var_test = $var2 + 0.02;

if ($var1 > $var_test) {
    echo "WRONG\n";
} else {
    echo "OK\n";
}

if (9.22 > 9.20 + 0.02) {
    echo "WRONG";
} else {
    echo "OK";
}

Expected result:
----------------
OK
OK

Actual result:
--------------
WRONG
WRONG
 [2015-08-13 18:59 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2015-08-13 18:59 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://www.floating-point-gui.de/

Thank you for your interest in PHP.

.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 13:01:30 2024 UTC