#36730 [Opn->Bgs]: int and intval produce wrong results

From: Date: Tue, 14 Mar 2006 01:04:24 +0000
Subject: #36730 [Opn->Bgs]: int and intval produce wrong results
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-94318@lists.php.net to get a copy of this message
ID: 36730 Updated by: rasmus@php.net Reported By: scott dot fagg at arup dot com -Status: Open +Status: Bogus Bug Type: *Math Functions Operating System: linux PHP Version: 4.4.2 New Comment: This is not a bug. Please read the short note at http://php.net/float Previous Comments: ------------------------------------------------------------------------ [2006-03-14 01:20:46] scott dot fagg at arup dot com Description: ------------ (int) and intval() generate wrong values. Output of (int) and intval() is affected by how the values passed to them, were calculated. Reproduce code: --------------- $x = 0.57 ; $a = (int) ($x * 100); $b = intval($x * 100); # produce incorrect results , $b = 56 print "$x $a $b\n"; $x = 0.56 + 0.01; $a = (int) ($x * 100); $b = intval($x * 100); # produce expected results , $b = 57 print "$x $a $b\n"; Expected result: ---------------- (int) and intval() should produce correct answers and should not be affected by how the values passed to them are calculated. Workaround seems to be to use round() instead of intval(). Actual result: -------------- Above code produces : 0.57 56 56 0.57 57 57 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=36730&edit=1

« previous php.bugs (#94318) next »