|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79301 Float number issue when using in class property
Submitted: 2020-02-24 14:34 UTC Modified: 2020-02-24 14:53 UTC
From: dirk dot gerigk at tui dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 7.4.3 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: dirk dot gerigk at tui dot com
New email:
PHP Version: OS:


 [2020-02-24 14:34 UTC] dirk dot gerigk at tui dot com
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:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-02-24 14:35 UTC]
-Status: Open +Status: Not a bug
 [2020-02-24 14:35 UTC]
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:

Thank you for your interest in PHP.

 [2020-02-24 14:39 UTC] dirk dot gerigk at atraveo dot com

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:43 UTC] 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


> This can be right

it can - roud the values before the output
 [2020-02-24 14:45 UTC]
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.
 [2020-02-24 14:49 UTC] dirk dot gerigk at atraveo dot com
May point in the first place, was that it only happens when you bind the result directly into a class property. If you are using an variable it won't happen.

Rounding is not a case for my related code. 

Thanks for the fast reply.
 [2020-02-24 14:53 UTC]
Your code behaves the same way whether the value is assigned to a property or not, see for example.
 [2020-02-24 15:01 UTC] dirk dot gerigk at atraveo dot com
Ok, sorry, my fault. 
I used just 'print' in the variable test.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 20 12:01:33 2024 UTC