php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31007 intval
Submitted: 2004-12-07 10:52 UTC Modified: 2005-01-12 00:39 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mc at paycash dot ru Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.0.2 OS: Windows 2000
Private report: No CVE-ID: None
 [2004-12-07 10:52 UTC] mc at paycash dot ru
Description:
------------
Incorrect result of intval function or combination of functions where intval present.

Reproduce code:
---------------
/* Rounds float by tuncating any numbers after second digit since decimal point */
function myRound($f) {
	/* Step by step trace */
	print "--Begin--\n\t\t";
	print floatval($f);													print "\n\t\t";
	print 100.0*floatval($f);										print "\n\t\t";
	print intval(100.0*floatval($f));						print "\n\t\t";	//This line working incorrect, cause intval(4023) must be 4023 not 4022!!!!
	print intval(100.0*floatval($f))/100.0;			print "\n\t";
	print "--End--\n";
	
	return sprintf("%.2f",intval(100.0*floatval($f))/100.0);
}
//////Error appears only with this number, any other number i've tried gave the correct result
$a=40.2396;
print "----Lets try $a\n\t";
$a=myRound($a);
print "----Returned:";print $a;print "\n\n";
print "----Lets try $a\n\t";
$a=myRound($a); //a==40.23
print "----Returned:";print $a;print "\n\n"; //a==40.22!!!!!!!!

Expected result:
----------------
----Lets try 40.2396
	--Begin--
		40.2396
		4023.96
		4023
		40.23
	--End--
----Returned:40.23

----Lets try 40.23
	--Begin--
		40.23
		4023
		4023
		40.23
	--End--
----Returned:40.23

Actual result:
--------------
----Lets try 40.2396
	--Begin--
		40.2396
		4023.96
		4023
		40.23
	--End--
----Returned:40.23

----Lets try 40.23
	--Begin--
		40.23
		4023
		4022
		40.22
	--End--
----Returned:40.22

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-01-12 00:32 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.
 
Thank you for your interest in PHP.

Also, your reproduce code can be easily reduced to ONE string:
echo intval(100.0*40.23);
Next time plz provide a *readable* reproduce code.
 [2005-01-12 00:39 UTC] tony2001@php.net
Read here about floating point limitations: 
http://www.php.net/manual/en/language.types.float.php 
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC