This article mainly introduces the method of realizing the remainder of large numbers (floating point numbers) in PHP, and analyzes the operation skills related to PHP mathematical operations in the form of examples. Friends in need can refer to the following
The examples in this article describe PHP implements the remainder method of large numbers (floating point numbers). Share it with everyone for your reference. The details are as follows:
Generally, the first thing we think of when performing remainder operation is to use the percent sign %, but when the divisor is a large value and exceeds the range of int, like this The remainder is not accurate.
php large number (floating point number) remainder function:
/** * php大数取余 * * @param int or float $bn 除数 * @param int $sn 被除数 * @return int 余数 */ //大数(浮点数)取余方法 function Kmod($bn, $sn) { return intval(fmod(floatval($bn), $sn)); }
Test code:
//大数(浮点数)取余方法 function Kmod($bn, $sn) { return intval(fmod(floatval($bn), $sn)); } //整数取余方法 function mod($bn, $sn) { return $bn%$sn; } //最大的int整数 $bn = PHP_INT_MAX; $sn = 11; var_dump($bn); var_dump(Kmod($bn, $sn)); var_dump(mod($bn, $sn)); //给最大的int整数加1 $bn = PHP_INT_MAX + 1; var_dump($bn); var_dump(Kmod($bn, $sn)); var_dump(mod($bn, $sn));
Execution result:
int 2147483647 int 1 int 1 float 2147483648 int 2 int -2
More PHP implementation of large numbers (floating point numbers) For articles related to Yu’s method, please pay attention to the PHP Chinese website!