|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72335 Misoptimize due to type narrowing
Submitted: 2016-06-04 21:31 UTC Modified: 2016-06-06 12:03 UTC
From: Assigned: dmitry (profile)
Status: Closed Package: opcache
PHP Version: master-Git-2016-06-04 (Git) OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
41 + 28 = ?
Subscribe to this entry?

 [2016-06-04 21:31 UTC]
We perform (long|double)->double narrowing even if chains of operations that only eventually result in a double are involved. This can lead to wrong results because parts of the calculation that would have been performed on integers are performed on doubles instead, with the corresponding loss in precision.

We need to either not narrow for such chains or ensure that the transformation is safe.

Test script:

function test() {
    $b = false;
    $x = (1<<53)+1;
    do {
        $x = 1.0 * ($x - (1<<53));
    } while ($b);
    return $x;

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-06 05:40 UTC]
we should check if there is any precision losing while converting long to double.
 [2016-06-06 06:33 UTC]
Added negative case:

function test() {
    $b = false;
    $x = -(1<<53) -1;
    do {
        $x = 1.0 * ($x + (1<<53));
    } while ($b);
    return $x;
 [2016-06-06 12:03 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: dmitry
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 23:01:31 2024 UTC