Bug #72820 [Opn->Nab]: simple float to int conversation bug (input param: 0.2 or -0.2)

From: Date: Fri, 12 Aug 2016 14:18:24 +0000
Subject: Bug #72820 [Opn->Nab]: simple float to int conversation bug (input param: 0.2 or -0.2)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203217@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72820&edit=1 ID: 72820 Updated by: cmb@php.net Reported by: denton85 at gmail dot com Summary: simple float to int conversation bug (input param: 0.2 or -0.2) -Status: Open +Status: Not a bug Type: Bug Package: Math related Operating System: Ubuntu 16.04, Win10, Win7 PHP Version: Irrelevant -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: 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://www.floating-point-gui.de/ Thank you for your interest in PHP. Previous Comments: ------------------------------------------------------------------------ [2016-08-12 12:02:24] denton85 at gmail dot com Description: ------------ The magic number is: 0.2 or -0.2 number - number = 0.2 or -0.2 then int conversion return bad result. I tried with (int) or intval. Same result. We tried with Win7, Win10 and Ubuntu 16.04 ubuntu's php version (official, installed with ap-get): PHP 7.0.8-0ubuntu0.16.04.2 (cli) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.8-0ubuntu0.16.04.2, Copyright (c) 1999-2016, by Zend Technologies Test script: --------------- <?php echo "<pre>"; $var = (1-1.2)*100; var_dump($var); //float(-20) $var2 = (-0.2)*100; var_dump($var2); //float(-20) $var_int = (int)$var; var_dump($var_int); //int(-19) ** INCORRECT RESULT ** $var2_int = (int)$var2; var_dump($var2_int); //int(-20) echo "</pre>"; //I changed values and no problem echo "<pre>"; $var = (2-1.2)*100; var_dump($var); //float(80) $var2 = (0.8)*100; var_dump($var2); //float(80) $var_int = (int)$var; var_dump($var_int); //int(80) $var2_int = (int)$var2; var_dump($var2_int); //int(80) echo "</pre>"; ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72820&edit=1

« previous php.bugs (#203217) next »