modulus operation gives negative value in php -
i expects 768 following statement. echos -360. why?
echo 1364808202768%1000;
update:
#: php -v php 5.3.2-1ubuntu4.17 suhosin-patch (cli) (built: jun 19 2012 01:35:33) copyright (c) 1997-2009 php group zend engine v2.3.0, copyright (c) 1998-2010 zend technologies xdebug v2.0.5, copyright (c) 2002-2008, derick rethans #: php -r 'echo 1364808202768%1000;' -360 #: uname -a linux tripura 2.6.32-37-generic #81-ubuntu smp fri dec 2 20:35:14 utc 2011 i686 gnu/linux
it because number exceeds maximum integer type can hold. can check if integer big this:
var_dump('1364808202768' > php_int_max);
the built-in function fmod
easy use, , should able handle big numbers:
echo fmod('1364808202768', 1000);
you can use 1 of 2 common extensions (gmp , bc) if, whatever reason, fmod
not available, or if work large numbers:
// using gmp extension $big = gmp_init('1364808202768', 10); $val = gmp_strval(gmp_mod($big, 1000)); echo $val; // using bc extension echo bcmod('1364808202768', 1000);
Comments
Post a Comment