|  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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
New email:
PHP Version: OS:


 [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: Tue Apr 23 06:01:30 2024 UTC