|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #13550 intval() does not round off doubles correctly
Submitted: 2001-10-04 19:08 UTC Modified: 2001-10-05 17:06 UTC
From: mlemos at acm dot org Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: 4.0.6 OS: Linux 2.2.18
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
7 + 4 = ?
Subscribe to this entry?

 [2001-10-04 19:08 UTC] mlemos at acm dot org
It seems that intval does not round off double numbers like the function that prints those numbers. Try this script to see the problem.



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2001-10-05 03:58 UTC]
Could you be a little less cryptic please?  I would expect this to print:
int 2
float 100
string '8.78'
float 8.78
float 878
int 878
int 878
Which is exactly what I am seeing.  $scaled = 8.78 * 100.0 which gives a double value of 878.00 therefore round(878.00) would be 878 and intval(878) is 878.  What exactly are you expecting to be different here?
 [2001-10-05 08:30 UTC] mlemos at acm dot org
Except that it doesn't, here and in many other users machines it prints:

string(4) "8.78"

glibc rpm version is 2.2-7.

 [2001-10-05 08:35 UTC]
The bug is real. Try it on the mentioned OS with glibc version mentioned in the report to see it.
 [2001-10-05 15:56 UTC]
No, floating point numbers are ALWAYS inprecise, and you shouldn't trust on their exact values.

See the warning on

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 16:40 UTC]
I don't think you are understanding the problem.

Basically the bug is that in PHP:


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);


echo intval($double);

should print the same as

echo intval(round($double));

 [2001-10-05 16:45 UTC]
> 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:54 UTC]
This is a real PHP bug that needs to be fixed, because if:

[1]echo $double;

prints the same as

[2]echo round($double);


[3]echo intval($double);

should print the same as

[4]echo intval(round($double));


[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 17:00 UTC]
Ok, you could have stated this to begin with:


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))
 [2001-10-05 17:06 UTC]
Manuel ment that 


should be true in the context of 

$double == round($double)

So his problem wasn't with mixing up round and intval, but his problem was that he didn't realize that

(string)$double == (string)round($double)

does not necessarily imply 

$double == round($double)

(and which is not the case in this case, because the difference being to small to show up in the string representation)

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 22 03:01:32 2024 UTC