Bug #13550 Updated: intval() does not round off doubles correctly
| From: | rasmus@php.net | Date: | Fri, 05 Oct 2001 21:00:04 +0000 |
| Subject: | Bug #13550 Updated: intval() does not round off doubles correctly | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-67383@lists.php.net to get a copy of this message | ||
ID: 13550
Updated by: rasmus
Reported By: mlemos@acm.org
Status: Bogus
Bug Type: *Programming Data Structures
Operating System: Linux 2.2.18
PHP Version: 4.0.6
New Comment:
Ok, you could have stated this to begin with:
intval($double)!=intval(round($double))
Where are you getting the idea that intval does a round()?
intval(3.99) would be 3
round(3.99) would be 4
Therefore intval(round(3.99)) would be 4
and thus often intval($double) != intval(round($double))
Previous Comments:
------------------------------------------------------------------------
[2001-10-05 16:54:14] jeroen@php.net
<quote>
This is a real PHP bug that needs to be fixed, because if:
[1]echo $double;
prints the same as
[2]echo round($double);
then
[3]echo intval($double);
should print the same as
[4]echo intval(round($double));
</quote>
[1] and [2] _print_ the same, but they aren't. It's because of the precision setting in
php.ini (14 significant numbers, IIRC)
This is inherently to floating point numbers. Simply don't trust them to the last digit.
------------------------------------------------------------------------
[2001-10-05 16:45:13] jeroen@php.net
> Basically the bug is that in PHP:
>
> intval($double)!=intval(round($double))
converting to integer will round DOWN, while rounding first rounds towards the nearst integer.
intval($float) and intval(floor($float)) should be the same for nonnegative $float's, but in
the case of round that's not true.
So not a bug
------------------------------------------------------------------------
[2001-10-05 16:40:50] manuel@php.net
I don't think you are understanding the problem.
Basically the bug is that in PHP:
intval($double)!=intval(round($double))
round($double) is still a floating point number.
The problem is that currently in PHP the intval() floating point rounding rule is not the same as
round() rule.
This is a real PHP bug that needs to be fixed, because if:
echo $double;
prints the same as
echo round($double);
then
echo intval($double);
should print the same as
echo intval(round($double));
------------------------------------------------------------------------
[2001-10-05 15:56:41] jeroen@php.net
No, floating point numbers are ALWAYS inprecise, and you shouldn't trust on their exact values.
See the warning on http://www.php.net/manual/en/language.types.float.php
The problem is that $scaled is 877.99999999999999 or something, and because of the precision setting
in php.ini it is printed as 878, but when using (int) cast, or intval(), it will be rounded down (or
twowards zero, don't reacall).
By the way, pow(int,int) will yield integer values when possible since 4.0.7, which nukes one
possible source of floating point problems (unless you use 10.0 of course, but that doesn't
make sense in PHP)
BTW: this is a FAQ, will add an entry.
------------------------------------------------------------------------
[2001-10-05 08:35:55] manuel@php.net
The bug is real. Try it on the mentioned OS with glibc version mentioned in the report to see it.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/?id=13550
Edit this bug report at http://bugs.php.net/?id=13550&edit=1