php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79641 Type cast problem
Submitted: 2020-05-27 12:12 UTC Modified: 2020-05-27 12:13 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: mr dot vladkuz at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.4.6 OS: Centos 7, macos
Private report: No CVE-ID: None
 [2020-05-27 12:12 UTC] mr dot vladkuz at gmail dot com
Description:
------------
One bit is lost when using typed parameters function.

This occurs for 2^18-2^19.

Test script:
---------------
class TypeHintEvil
{
    private $num;
    public function getNum(): int
    {
        return $this->num;
    }
    public function setNum(int $num): void
    {
        $this->num = $num;
    }
}
$evil = new TypeHintEvil();
$evil->setNum(4860.98 * 100);

echo $evil->getNum(); //486097

Expected result:
----------------
486098

Actual result:
--------------
486097

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-05-27 12:13 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-05-27 12:13 UTC] requinix@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-05-27 12:25 UTC] mr dot vladkuz at gmail dot com
This problem present for range digits from 4096,11 to 5241,98
 [2020-05-27 12:30 UTC] bugreports at gmail dot com
> This problem present for range digits from 4096,11 to 5241,98

irrelevant

php -d precision=-1 -r "echo (4860.98 * 100);"
486097.99999999994

that casted to int without round is 486097, case closed
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Aug 17 11:05:46 2022 UTC