Bug #13550 Updated: intval() does not round off doubles correctly

From: Date: Fri, 05 Oct 2001 20:40:50 +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-67379@lists.php.net to get a copy of this message
ID: 13550 Updated by: manuel Reported By: mlemos@acm.org Old Status: Bogus Status: Open Bug Type: *Programming Data Structures Operating System: Linux 2.2.18 PHP Version: 4.0.6 New Comment: 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)); Previous Comments: ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ [2001-10-05 08:30:42] mlemos@acm.org Except that it doesn't, here and in many other users machines it prints: int(2) float(100) string(4) "8.78" float(8.78) float(878) int(877) int(878) glibc rpm version is 2.2-7. ------------------------------------------------------------------------ [2001-10-05 03:58:28] rasmus@php.net 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-04 19:08:52] mlemos@acm.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. <? $decimal_places=2; $decimal_factor=pow(10.0,$decimal_places); $value='8.78'; $double=doubleval($value); $scaled=$double*$decimal_factor; $integer=intval($scaled); $round=intval(round($scaled)); var_dump($decimal_places,$decimal_factor,$value,$double,$scaled,$integer,$round); ?> ------------------------------------------------------------------------ Edit this bug report at http://bugs.php.net/?id=13550&edit=1

« previous php.dev (#67379) next »