Bug #52315 [Opn->Bgs]: Error in type convert after specific round() operation
Edit report at http://bugs.php.net/bug.php?id=52315&edit=1
ID: 52315
Updated by: aharvey@php.net
Reported by: wisent at yandex dot ru
Summary: Error in type convert after specific round() operation
-Status: Open
+Status: Bogus
Type: Bug
Package: Math related
Operating System: Ubuntu 8.04
PHP Version: 5.2.13
New Comment:
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://docs.sun.com/source/806-3568/ncg_goldberg.html
Thank you for your interest in PHP.
Previous Comments:
------------------------------------------------------------------------
[2010-07-12 14:40:58] wisent at yandex dot ru
Description:
------------
First I perform specific round() operation. Then I convert the number
from float to integer. The result is wrong, the number differs by 1 from
what it should be.
The problem appears not for every number.
Test script:
---------------
// this works bad:
$sum = '4926.11';
$sum = round($sum, 2) * 100;
var_dump($sum); // float(492611)
$sum = (integer) $sum;
var_dump($sum); // int(492610)
// the same round() operation, the other number, works good
$sum = '426.11';
$sum = round($sum, 2) * 100;
var_dump($sum); // float(42611)
$sum = (integer) $sum;
var_dump($sum); // int(42611)
// the first numbers, the other round() operation, works good
$sum = '4926.11';
$sum = round($sum * 100);
var_dump($sum); // float(492611)
$sum = (integer) $sum;
var_dump($sum); // int(492611)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52315&edit=1
Thread (3 messages)