Bug #79301 [Nab]: Float number issue when using in class property

From: Date: Mon, 24 Feb 2020 14:45:53 +0000
Subject: Bug #79301 [Nab]: Float number issue when using in class property
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225702@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79301&edit=1 ID: 79301 Updated by: nikic@php.net Reported by: dirk dot gerigk at tui dot com Summary: Float number issue when using in class property Status: Not a bug Type: Bug Package: Variables related Operating System: Linux PHP Version: 7.4.3 Block user comment: N Private report: N New Comment: var_export() prints accurate result, string conversion prints results rounded to precision=14 by default. Use precision=-1 if you want string conversion to print non-rounded results. There is no relation to properties here at all. Previous Comments: ------------------------------------------------------------------------ [2020-02-24 14:43:15] bugreports at gmail dot com > This was working in PHP 5.5 irrelevant - the value for "precision" may have changed and it worked only by luck > and some number work some no irrelevant > This can be right it can - roud the values before the output ------------------------------------------------------------------------ [2020-02-24 14:39:44] dirk dot gerigk at atraveo dot com at nikic@php.net This was working in PHP 5.5 And this is an calculation with just multiply with 1000 and some number work some not. Also if you not bind the value to an class variable that will not happen. This can be right. ------------------------------------------------------------------------ [2020-02-24 14:35:55] nikic@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://www.floating-point-gui.de/ Thank you for your interest in PHP. ------------------------------------------------------------------------ [2020-02-24 14:34:53] dirk dot gerigk at tui dot com Description: ------------ Please have short look at the test code. If you do an float number calculation sometimes it results invalid result, when you bind the result directly into a class variable. An var_export() shows the wrong calculation, but if you cast the class property to string the value is right again (last line in the foreach). The wired part is that it not is always the case (see "11.8", "2.5"). What is wrong here? Test script: --------------- class X { public $v = null; } $x = new X(); foreach(["32.3","11.8","2.5"] as $float){ print ((float)(string)$float * 1000).PHP_EOL; $x->v = (float)(string)$float * 1000; print var_export($x->v,true).PHP_EOL; print "{$x->v}\n"; } Expected result: ---------------- 32300 32300.0 32300 11800 11800.0 11800 2500 2500.0 2500 Actual result: -------------- 32300 32299.999999999996 32300 11800 11800.0 11800 2500 2500.0 2500 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79301&edit=1

« previous php.bugs (#225702) next »