ID: 45897 User updated by: hengkhee at gmail dot com Reported By: hengkhee at gmail dot com Status: Bogus Bug Type: Math related Operating System: UBuntu 8.04, CentOS 5.0 PHP Version: 5.2.4 New Comment:
Thank you for your reply, I still not very clear about why this happen, as for a PHP beginner, the arithmetic below is very straight, but output the unexpected result: $var1 = 0.15 * 3 * 100; $var2 = $var1 + 1.0 - 1.0; #Perform some dummy matematic operation $var3 = $var2 % 5; var_dump($var3); # Still unexpected result, int(4) For same arithmetic, Java is ok: float f, result; f = (float)(0.15 * 3 * 100); result = f%5; System.out.println(result); My question is: - Don't PHP processor should handle this situation? - So this consider not a bug of PHP? Or is weaknesses of it? - Any work around? My work around is concat an string with the variable.. May be it is a stupid way, but it works fine for me (until current version of PHP). Thank you for your discussion and advice, I really appreaciated. Previous Comments: ------------------------------------------------------------------------ [2008-08-26 22:41:54] [EMAIL PROTECTED] 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. $ php -dprecision=32 -r 'var_dump(0.15*3*100);' float(44.999999999999992894572642398998) ------------------------------------------------------------------------ [2008-08-23 13:14:38] hengkhee at gmail dot com The php version on ------------ Ubuntu 8.04: ------------ PHP 5.2.4-2ubuntu5.3 with Suhosin-Patch 0.9.6.2 (cli) (built: Jul 23 2008 06:46:18) Copyright (c) 1997-2007 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies ------------ CentOS: ------------ PHP 5.2.6 (cli) (built: Jul 29 2008 20:48:49) Copyright (c) 1997-2008 The PHP Group Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies with Zend Extension Manager v1.2.2, Copyright (c) 2003-2007, by Zend Technologies with Zend Optimizer v3.3.3, Copyright (c) 1998-2007, by Zend Technologies ------------------------------------------------------------------------ [2008-08-23 13:09:02] hengkhee at gmail dot com Description: ------------ - Modulus error, please refer to code. - Using default package/installation from Ubuntu 8.04 Server/Desktop and CentOS 5.0 - Using default php.ini from distro. Reproduce code: --------------- $var1 = 0.15 * 3 * 100; echo ($var1 % 5); # Output "4", unexpected result echo ((0.15 * 3 * 100) % 5); # Output "4", unexpected result echo (45.00 % 5); # Output "0", correct result echo (45 % 5); # Output "0", correct result echo (($var1."") % 5); # Output "0", correct result Expected result: ---------------- Correct result = "0" for all above sample. Actual result: -------------- Please refer to above sample code. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=45897&edit=1