Bug #54187 [Com]: unexpected results to comparing strings converted to floating

From: Date: Wed, 09 Mar 2011 16:57:54 +0000
Subject: Bug #54187 [Com]: unexpected results to comparing strings converted to floating
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-159347@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=54187&edit=1

 ID:                 54187
 Comment by:         jose dot nobile at gmail dot com
 Reported by:        jose dot nobile at latinoaustralia dot com
 Summary:            unexpected results to comparing strings converted to
                     floating
 Status:             Bogus
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Windows XP Professional SP3
 PHP Version:        5.3.5
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2011-03-09 11:14:07] 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-08 01:17:05] 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)


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=54187&edit=1


Thread (5 messages)

« previous php.bugs (#159347) next »