|  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
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:
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 May 21 17:01:36 2024 UTC