PHP 4.0 Bug #5819 Updated: Floating-point precison lost only but always for some "magic" numbers

From: Date: Thu, 27 Jul 2000 16:22:38 +0000
Subject: PHP 4.0 Bug #5819 Updated: Floating-point precison lost only but always for some "magic" numbers
Groups: php.dev 
Request: Send a blank email to php-dev+get-26507@lists.php.net to get a copy of this message
ID: 5819 Updated by: rasmus Reported By: nightfall@mail.ru Status: Closed Bug Type: Other Assigned To: Comments: Not a bug. You need to understand how computers store floating point values in order to figure this one out. Basically floating numbers are always approximated. For example, if you write out 1/3 in floating point, you can never actually write a perfectly accurate version of that in floating point. 0.333333333333333333... But when do you stop? You stop when you have reached the desired precision level. Now, if you multiply this value by 1000 and then divide by a 1000 you have just multiplied your precision error by 1000. The floating point value itself won't gain more information to make up for this. For your example, you set $a to 1.16. But, in the computer this is actually stored a 1.599999999999999999999999999999999999999 or 1.160000000000000000000000001 because approximations are never exact. So, to fix you particular problem you would have to add a tiny fuzz factor to make sure your effective rounding will round to 16 if it is within your fuzz factor. eg. $fuzz = 0.000000000001; $b = (int)($fuzz+($a*100))/100; (and no, this is not specific to PHP) Full Bug description available at: http://bugs.php.net/?id=5819

« previous php.dev (#26507) next »