#49871 [Bgs]: unexpected result converting float to int

From: Date: Wed, 14 Oct 2009 13:28:54 +0000
Subject: #49871 [Bgs]: unexpected result converting float to int
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-143560@lists.php.net to get a copy of this message
ID: 49871 Updated by: rasmus@php.net Reported By: daniel dot buschke at nextiraone dot de Status: Bogus Bug Type: Math related Operating System: Linux PHP Version: 5.2.11 New Comment: Add ini_set('precision',20); to the top of your script. That sets the output precision and will cause your var_dump() to show: --------- 2.3199999999999998401 -------- float(2.3199999999999998401) float(231.99999999999997158) int(231) --------- 8.2799999999999993605 -------- float(8.2799999999999993605) float(827.99999999999988631) int(827) This precision setting, because it is lossy, isn't applied until you try to convert the float to a string, usually on output, but as the previous comment mentioned, you could force it to apply before output with an explicit conversion. Or you can set your own fuzz factor and use that. If we magically applied this precision setting on every math operation, these small floating point errors would become much bigger when you actually were expecting your floating point to behave as floating point values. Previous Comments: ------------------------------------------------------------------------ [2009-10-14 11:37:44] daniel dot buschke at nextiraone dot de (int)(string)$b that does the job but is ugly, too ------------------------------------------------------------------------ [2009-10-14 11:16:23] sjoerd@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. If you would like to know more about "floats" and what IEEE 754 is, read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. ------------------------------------------------------------------------ [2009-10-14 08:36:03] daniel dot buschke at nextiraone dot de Description: ------------ Hi, when I convert following float: float(232) into an int I get: int(231). The float was calculated by 2.32 * 100. I know that this may result in 232.00000000001 or 231.999999999 but the behavior is unexcpected because var_dump does not show the real (more or less wrong) float number. This one is exact the same as #2835 but the bug was closed without any further comments. The provided solution is unacceptable. How to convert 2.32 into an integer of the value 232? BTW: Please do not set this bug to bogus or closed with the default text for that problem ;-). As you can see in your bugtracker the community wants a solution for that. Please also remember that PHP is a scripting language which mainly ignores datatypes. This problem is not comprehensible to beginners (webdesigners ;-) ). I do not want to blame you with that but I just want to find a usefull solution. Please keep communication with community. regards Daniel Reproduce code: --------------- <?php messUpFloat(2.32); messUpFloat(8.28); function messUpFloat($float) { echo '--------- ' . $float . ' --------' . PHP_EOL; $a = $float; var_dump($a); $b = $a * 100 + 0; var_dump($b); $c = (int)$b; var_dump($c); } ?> Expected result: ---------------- --------- 2.32 -------- float(2.32) float(232) int(232) --------- 8.28 -------- float(8.28) float(828) int(828) or (but not preferred ;-) ) --------- 2.32 -------- float(2.32) float(231.999999999999999) int(231) --------- 8.28 -------- float(8.28) float(827.999999999999999) int(827) Actual result: -------------- --------- 2.32 -------- float(2.32) float(232) int(231) --------- 8.28 -------- float(8.28) float(828) int(827) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=49871&edit=1

« previous php.bugs (#143560) next »