php.net |  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
 [2016-08-12 12:02 UTC] denton85 at gmail dot com
Description:
------------
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:
---------------
<?php

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>";


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-12 14:18 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-08-12 14:18 UTC] cmb@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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC