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

From: Date: Mon, 24 Feb 2020 14:34:53 +0000
Subject: Bug #79301 [NEW]: Float number issue when using in class property
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-225698@lists.php.net to get a copy of this message
From: dirk dot gerigk at tui dot com Operating system: Linux PHP version: 7.4.3 Package: Variables related Bug Type: Bug Bug description:Float number issue when using in class property 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 bug report at https://bugs.php.net/bug.php?id=79301&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=79301&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=79301&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=79301&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=79301&r=needscript Try newer version: https://bugs.php.net/fix.php?id=79301&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=79301&r=support Expected behavior: https://bugs.php.net/fix.php?id=79301&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=79301&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=79301&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=79301&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=79301&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=79301&r=dst IIS Stability: https://bugs.php.net/fix.php?id=79301&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=79301&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=79301&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=79301&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=79301&r=mysqlcfg

« previous php.bugs (#225698) next »