php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63960 Unexpected result of int() conversion
Submitted: 2013-01-10 13:23 UTC Modified: 2013-01-10 13:29 UTC
From: joern at hv23 dot net Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.4.10 OS: Mac OSX + Linux
Private report: No CVE-ID: None
 [2013-01-10 13:23 UTC] joern at hv23 dot net
Description:
------------
Putting an (int) in front of brackets creates a wrong result - or at least an 
unexpected result.

Output of below script is actually:

input = 0.58, output = 57
test3 = 58
test4 = 57

("0.58"*100) creates a correct 58 
(int) ("0.58"*100) gives an incorrect 57

The same happens with input of "0.29", where the output = is 28.

It happens on intel as well as AMD cpus.

Test script:
---------------
$test = "0.58";
$test2 = (int) ($test*100);
$test3 = ($test*100);
$test4 = (int) ($test*100);

echo "input = $test, output = $test2\n";
echo "test3 = $test3\n";
echo "test4 = $test4\n";


Expected result:
----------------
I would expect the output to be:
input = 0.58, output = 57
test3 = 58
test4 = 58

Actual result:
--------------
Current output:

input = 0.58, output = 57
test3 = 58
test4 = 57

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-10 13:29 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2013-01-10 13:29 UTC] johannes@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: Fri Dec 27 02:01:29 2024 UTC