Home Backend Development PHP Tutorial PHP floating point number remainder method

PHP floating point number remainder method

Jul 11, 2017 pm 02:05 PM
php method

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!

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles