From: daniel dot buschke at nextiraone dot de
Operating system: Linux
PHP version: 5.2.11
PHP Bug Type: Math related
Bug description: unexpected result converting float to int
Description:
------------
Hi,
when I convert following float:
float(232)
into an int I get:
int(231).
The float was calculated by 2.32 * 100. I know that this may result in
232.00000000001 or 231.999999999 but the behavior is unexcpected because
var_dump does not show the real (more or less wrong) float number.
This one is exact the same as #2835 but the bug was closed without any
further comments. The provided solution is unacceptable.
How to convert 2.32 into an integer of the value 232?
BTW: Please do not set this bug to bogus or closed with the default text
for that problem ;-). As you can see in your bugtracker the community wants
a solution for that. Please also remember that PHP is a scripting language
which mainly ignores datatypes. This problem is not comprehensible to
beginners (webdesigners ;-) ).
I do not want to blame you with that but I just want to find a usefull
solution. Please keep communication with community.
regards
Daniel
Reproduce code:
---------------
<?php
messUpFloat(2.32);
messUpFloat(8.28);
function messUpFloat($float) {
echo '--------- ' . $float . ' --------' . PHP_EOL;
$a = $float;
var_dump($a);
$b = $a * 100 + 0;
var_dump($b);
$c = (int)$b;
var_dump($c);
}
?>
Expected result:
----------------
--------- 2.32 --------
float(2.32)
float(232)
int(232)
--------- 8.28 --------
float(8.28)
float(828)
int(828)
or (but not preferred ;-) )
--------- 2.32 --------
float(2.32)
float(231.999999999999999)
int(231)
--------- 8.28 --------
float(8.28)
float(827.999999999999999)
int(827)
Actual result:
--------------
--------- 2.32 --------
float(2.32)
float(232)
int(231)
--------- 8.28 --------
float(8.28)
float(828)
int(827)
--
Edit bug report at http://bugs.php.net/?id=49871&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=49871&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=49871&r=trysnapshot53
Try a snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=49871&r=trysnapshot60
Fixed in SVN:
http://bugs.php.net/fix.php?id=49871&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=49871&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=49871&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=49871&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=49871&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=49871&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=49871&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=49871&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=49871&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=49871&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=49871&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=49871&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=49871&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=49871&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=49871&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=49871&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=49871&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=49871&r=mysqlcfg