php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54187 unexpected results to comparing strings converted to floating
Submitted: 2011-03-08 01:17 UTC Modified: 2011-03-11 15:22 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: jose dot nobile at latinoaustralia dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.5 OS: Windows XP Professional SP3
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: jose dot nobile at latinoaustralia dot com
New email:
PHP Version: OS:

 

 [2011-03-08 01:17 UTC] jose dot nobile at latinoaustralia dot com
Description:
------------
I have a strings with float numbers, in format integer.decimal (eg 916.32), and 
using operators (<,>,==,===) don't working as expecting.

Additional info:

System	Windows NT WEBMASTER 5.1 build 2600 (Windows XP Professional Service 
Pack 3) i586

Build Date	Jan 5 2011 20:26:24

Compiler	MSVC9 (Visual C++ 2008)

Architecture	x86

Configure Command	cscript /nologo configure.js "--enable-snapshot-build" 
"--disable-isapi" "--enable-debug-pack" "--disable-
isapi"http://bugs.php.net/bugs-generating-backtrace.php "--without-mssql" "--
without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=D:\php-
sdk\oracle\instantclient10\sdk,shared" "--with-oci8=D:\php-
sdk\oracle\instantclient10\sdk,shared" "--with-oci8-11g=D:\php-
sdk\oracle\instantclient11\sdk,shared" "--enable-object-out-dir=../obj/" "--
enable-com-dotnet" "--with-mcrypt=static"

precision	        14	14
serialize_precision	100	100
PROCESSOR_ARCHITECTURE	x86
PROCESSOR_IDENTIFIER	x86 Family 6 Model 15 Stepping 13, GenuineIntel
PROCESSOR_LEVEL	        6
PROCESSOR_REVISION	0f0d

Intel Core 2 Duo CPU E4500 @ 2.2Ghz

Download from: http://windows.php.net/download/


Test script:
---------------
echo "<pre>";
$string1 = "932.16";
$substring1 = "3.16";
$substring2 = "454";
$substring3 = "475";
$number2 = floatval(floatval($substring1) + floatval($substring2) + floatval($substring3));
$number1 = floatval($string1);
var_dump($number1);//expected: float(932.16), current: float(932.16), OK
var_dump($number2);//expected: float(932.16), current: float(932.16), OK
var_dump($number1 < $number2);//expected: bool(false), current: bool(true), FAIL
var_dump($number1 > $number2);//expected: bool(false), current: bool(false), OK
var_dump($number1 == $number2);//expected: bool(true), current: bool(false), FAIL
var_dump($number1 === $number2);//expected: bool(true), current: bool(false), FAIL
var_dump(bccomp($number1,$number2));//expected: int(0), current: int(0), OK
echo "</pre>";

Expected result:
----------------
float(932.16)
float(932.16)
bool(false)
bool(false)
bool(true)
bool(true)
int(0)

Actual result:
--------------
float(932.16)
float(932.16)
bool(true)
bool(false)
bool(false)
bool(false)
int(0)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-03-09 11:14 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2011-03-09 11:14 UTC] cataphract@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.


 [2011-03-09 17:57 UTC] jose dot nobile at gmail dot com
I understand about IEEE 754 standard, Two's complement, Booth's Algorithm, and 
more.

I found a workaround, 
$string = "1.123";//string
$number = 1.123;//float
$string2number = floatval($string);
var_dump($number < $string2number);//Fail, <, >, ==, === all fail
but ....
$string = "1.123";//string
$number = 1.123;//float
$string2number = floatval($string);
$float2float = floatval("$number");//float to string, string to float
var_dump($string2number < $float2float);//OK, <, >, ==, === all OK

This is a small fix, but complex to implement in all code.

Other solutions:
BCMath Arbitrary Precision Mathematics

But, I question, is possible overwrite all Mathematics Operators with by example 
BCMath? some is possible overwrite string with Multi-Byte string functions?

How is the correct to operate with floats? (in invoicing applications is 
critical point)

Thanks.
 [2011-03-11 14:36 UTC] jose dot nobile at latinoaustralia dot com
-Status: Bogus +Status: Open -Type: Bug +Type: Feature/Change Request
 [2011-03-11 14:36 UTC] jose dot nobile at latinoaustralia dot com
It is possible to override the mathematical operators with the functions of 
arbitrary mathematical precision?

or to print accurate internal representation of floating-point number. var_dump or 
print_r not show the exact value.
 [2011-03-11 15:22 UTC] pajoye@php.net
-Status: Open +Status: Bogus
 [2011-03-11 15:22 UTC] pajoye@php.net
Please read the provided links.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 22:01:33 2025 UTC