|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-08-23 13:09 UTC] 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. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 03 17:00:01 2025 UTC |
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 TechnologiesThank 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.