php.net |  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
 [2020-02-24 14:34 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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

irrelevant

> This can be right

it can - roud the values before the output
 [2020-02-24 14:45 UTC] nikic@php.net
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] nikic@php.net
Your code behaves the same way whether the value is assigned to a property or not, see https://3v4l.org/CQEd7 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.

Thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 14:01:31 2024 UTC