PHP method to implement remainder of large numbers (floating point numbers)

高洛峰
Release: 2023-03-05 21:02:01
Original
1540 people have browsed it

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));
}
Copy after login


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));
Copy after login


Execution result:


int 2147483647
int 1
int 1
float 2147483648
int 2
int -2
Copy after login

More PHP implementation of large numbers (floating point numbers) For articles related to Yu’s method, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template