#31007 [Bgs]: intval
ID: 31007
Updated by: tony2001@php.net
Reported By: mc at paycash dot ru
Status: Bogus
Bug Type: Math related
Operating System: Windows 2000
PHP Version: 5.0.2
New Comment:
Read here about floating point limitations:
http://www.php.net/manual/en/language.types.float.php
Previous Comments:
------------------------------------------------------------------------
[2005-01-12 00:32:02] 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.
------------------------------------------------------------------------
[2004-12-07 10:52:22] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=31007&edit=1
Thread (3 messages)