English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the method of implementing large number (floating-point number) modulus in PHP. Shared for everyone's reference, as follows:
Generally, when performing the modulus operation, the first thing we think of is to use the percentage sign %, but when the divisor is a very large number, exceeding the int range, such modulus operation is not accurate.
php Large Number (Floating Point Number) Modulus Function:
/** * php Large Number Modulus * * @param int or float $bn Divisor * @param int $sn Dividend * @return int Remainder */ //Large Number (Floating Point Number) Modulus Method function Kmod($bn, $sn) { return intval(fmod(floatval($bn), $sn)); }
Test Code:
//Large Number (Floating Point Number) Modulus Method function Kmod($bn, $sn) { return intval(fmod(floatval($bn), $sn)); } //Integer Modulus Method function mod($bn, $sn) { return $bn%$sn; } //The largest int integer $bn = PHP_INT_MAX; $sn = 11; var_dump($bn); var_dump(Kmod($bn, $sn)); var_dump(mod($bn, $sn)); //Add to the largest int integer1 $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
Readers who are interested in more content related to PHP can check the special topics on this site: 'Summary of PHP Mathematical Operation Techniques', 'Summary of PHP Operation and Operator Usage', 'Summary of php string (string) Usage', 'Comprehensive Techniques of PHP Array (Array) Operation', 'Summary of Common Traversal Algorithms and Techniques in PHP', 'PHP Data Structures and Algorithms Tutorial', 'Summary of php Program Design Algorithms', 'Summary of php Regular Expression Usage', and 'Summary of Common Database Operation Techniques in php'.
I hope the content described in this article will be helpful to everyone in PHP program design.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously, and this website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)