php.net |  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: nikic@php.net 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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: nikic@php.net
New email:
PHP Version: OS:

 

 [2016-06-04 21:31 UTC] nikic@php.net
Description:
------------
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:
---------------
<?php

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


Expected result:
----------------
float(1)

Actual result:
--------------
float(0)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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

function test() {
    $b = false;
    $x = -(1<<53) -1;
    do {
        $x = 1.0 * ($x + (1<<53));
    } while ($b);
    return $x;
}
var_dump(test());
 [2016-06-06 12:03 UTC] dmitry@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: dmitry
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 21:01:36 2024 UTC