#41154 [Opn->Bgs]: settype 'float' yields incosistent results
| From: | tony2001@php.net | Date: | Mon, 23 Apr 2007 09:04:23 +0000 |
| Subject: | #41154 [Opn->Bgs]: settype 'float' yields incosistent results | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-111802@lists.php.net to get a copy of this message | ||
ID: 41154
Updated by: tony2001@php.net
Reported By: php at michaelho dot com
-Status: Open
+Status: Bogus
Bug Type: Variables related
Operating System: Mac OS X 10.4.9
PHP Version: 5.2.1
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2007-04-20 21:25:22] php at michaelho dot com
Description:
------------
Using settype() to alter FLOATs yields inconsistent values that will
sometimes fail comparison operators.
Reproduce code:
---------------
<?php
// THIS WILL WORK
$fltOriginal = 3 * .5;
$fltTransformed = $fltOriginal;
settype($fltTransformed, 'string');
settype($fltTransformed, 'float');
var_dump($fltOriginal);
var_dump($fltTransformed);
if ($fltOriginal != $fltTransformed) print ('COMPARISON FAILED --
BUG');
print ("--------------------------\r\n");
// THIS WILL **NOT** WORK
$fltOriginal = 3 * .6;
$fltTransformed = $fltOriginal;
settype($fltTransformed, 'string');
settype($fltTransformed, 'float');
var_dump($fltOriginal);
var_dump($fltTransformed);
if ($fltOriginal != $fltTransformed) print ('COMPARISON FAILED --
BUG');
?>
Expected result:
----------------
float(1.5)
float(1.5)
--------------------------
float(1.8)
float(1.8)
Actual result:
--------------
float(1.5)
float(1.5)
--------------------------
float(1.8)
float(1.8)
COMPARISON FAILED -- BUG
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=41154&edit=1