Edit report at https://bugs.php.net/bug.php?id=62170&edit=1
ID: 62170 Comment by: cidahf at live dot co dot uk Reported by: cidahf at live dot co dot uk Summary: Inaccurate division Status: Not a bug Type: Bug Package: Math related Operating System: Ubuntu 12.04 PHP Version: 5.4.3-4~precise+1 Block user comment: N Private report: N New Comment: I understand now. I'm getting more accurate results now that I have lowered the precision down to 14. Thank you. Previous Comments: ------------------------------------------------------------------------ [2012-05-27 14:33:18] ras...@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. All languages have this issue when using the native floating point mechanism and displaying it with high precision. By default PHP will give you the right result: php > echo 25.4/1000; 0.0254 But, if you increase the precision you will see it is just an approximation because computers can't actually make that division precisely: php > ini_set('precision',32); php > echo 25.4/1000; 0.025399999999999998967492587098604 ------------------------------------------------------------------------ [2012-05-27 08:46:57] larue...@php.net float is inaccurate, http://cn.php.net/manual/en/language.types.float.php ------------------------------------------------------------------------ [2012-05-27 05:14:47] cidahf at live dot co dot uk Changed the version to the exact value returned from PHP_VERSION. ------------------------------------------------------------------------ [2012-05-27 05:01:08] cidahf at live dot co dot uk Description: ------------ When I divide 25.4 by 10000 I get an awkward 0.002539999999999999723276911112179732299409806728363037109375 instead of the desired 0.00254. I tried the same with Ruby 1.8.7 and it returned the correct result. This issue is problematic for unit conversion. I also got this in 5.3 but I upgraded before making a report. Test script: --------------- echo 25.4 / 10000; Expected result: ---------------- 0.00254 Actual result: -------------- 0.002539999999999999723276911112179732299409806728363037109375 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=62170&edit=1