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
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:
MUST BE VALID
Solve the problem:
44 - 40 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 05:01:33 2024 UTC