php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41154 settype 'float' yields incosistent results
Submitted: 2007-04-20 21:25 UTC Modified: 2007-04-23 09:04 UTC
From: php at michaelho dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.2.1 OS: Mac OS X 10.4.9
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at michaelho dot com
New email:
PHP Version: OS:

 

 [2007-04-20 21:25 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-23 09:04 UTC] tony2001@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-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 04 11:01:37 2025 UTC