|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72820 simple float to int conversation bug (input param: 0.2 or -0.2)
Submitted: 2016-08-12 12:02 UTC Modified: 2016-08-12 14:18 UTC
From: denton85 at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Math related
PHP Version: Irrelevant OS: Ubuntu 16.04, Win10, Win7
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:
From: denton85 at gmail dot com
New email:
PHP Version: OS:


 [2016-08-12 12:02 UTC] denton85 at gmail dot com
The magic number is: 0.2 or -0.2

number - number = 0.2 or -0.2

then int conversion return bad result.

I tried with (int) or intval. Same result.

We tried with Win7, Win10 and Ubuntu 16.04

ubuntu's php version (official, installed with ap-get):
PHP 7.0.8-0ubuntu0.16.04.2 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.8-0ubuntu0.16.04.2, Copyright (c) 1999-2016, by Zend Technologies

Test script:

echo "<pre>";

$var = (1-1.2)*100;
var_dump($var); //float(-20)

$var2 = (-0.2)*100;
var_dump($var2); //float(-20)

$var_int = (int)$var;
var_dump($var_int); //int(-19) ** INCORRECT RESULT **

$var2_int = (int)$var2;
var_dump($var2_int); //int(-20)

echo "</pre>";

//I changed values and no problem

echo "<pre>";

$var = (2-1.2)*100;
var_dump($var); //float(80)

$var2 = (0.8)*100;
var_dump($var2); //float(80)

$var_int = (int)$var;
var_dump($var_int); //int(80)

$var2_int = (int)$var2;
var_dump($var2_int); //int(80)

echo "</pre>";


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-12 14:18 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-08-12 14:18 UTC]
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:

Thank you for your interest in PHP.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 18:01:28 2024 UTC