PHP floating point number remainder method

怪我咯
Release: 2023-03-13 14:46:02
Original
4248 people have browsed it

Floating point type (also called float, double or real. The word length of floating point numbers is platform-dependent, although usually the maximum value is 1.8e308 and has a precision of 14 decimal digits (64-bit IEEE format ).

Floating point numbers have limited precision, although depending on the system, PHP generally uses the IEEE 754 double precision format, the maximum relative error due to rounding is 1.11e-16. Gives a larger error, and the error propagation when performing compound operations must be taken into account. In addition, rational numbers that can be accurately expressed in decimal, such as 0.1 or 0.7, cannot be used internally in binary, no matter how many mantissas there are. exact representation, and therefore cannot be converted to a binary format without losing a little precision.

This would lead to confusing results:

For example,

floor((0.1+0.7)*10)

usually returns 7 instead of the expected 8, because the internal representation of the result is actually similar to 7.9999999999999991118.... #So never believe that the floating point number result is accurate to the last digit, and never compare whether two floating point numbers are equal.

The example in this article describes the method of taking the remainder of PHP floating point numbers.

## Share it with everyone for your reference, the details are as follows:

Generally, the first thing that comes to mind when we perform remainder operation is to use the percent sign %, but when the divisor is a large value, it exceeds In the int range, the remainder is inaccurate. 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

The above is the detailed content of PHP floating point number remainder method. For more information, please follow other related articles on 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!