# Introduction to MOD (x, y) and x % y_PHP using the remainder function in PHP

Source: Internet
Author: User
The remainder functions of PHP are described in MOD (x, y) and x % y. The remainder function PHP is used to obtain the remainder MOD (x, y) x % yMOD of the remainder function PHP. for example, 93,9 is the divisor, and 3 is the divisor. the mod function is a remainder function in the format of mod (nExp1, nExp2), that is, two remainder functions, PHP, and PHP, and two remainder MOD (x, y) x % y.

MOD

For example, 9/3, 9 is the divisor, and 3 is the divisor. mod function is a remainder function in the format:
Mod (nExp1, nExp2) is the remainder of two numeric expressions after division. So: the remainder of the two same-number integers is exactly the same as the remainder of the two positive integers you know (that is, the two negative integers are the same as the algorithms of the two positive integers ).

I. calculate the remainder of two different integers

1. function value symbol law (symbol of the remainder)
Mod (negative, positive) = positive
Mod (positive, negative) = negative
Conclusion: when two integers are used to evaluate the remainder, the value symbol is the divisor symbol.
2. value rules
Consider two integers as positive numbers before division.
① When division is possible, its value is 0
② When division is not allowed, its value = divisor × (whole quotient + 1)-divisor
For example, mod (9,-8) =-7
That is, 9 is divided by the integer quotient of 8 as 1, and 1 is followed by 2. the product of 9 and divisor is 18, and the difference between 9 and divisor is 7. the divisor is used. So the value is-7.

2. perform the remainder operation with two decimal places

Value Rule: After the divisor x divisor, the number is rounded to the first decimal place.
For example, mod (9, 1.2) = 1
That is: 9 except 1.2, the total quotient is 7; 7 and the product of the divisor is 1.2; 8.4 is 8 after rounding; the difference between 9 and 8 is 1. The result is 1.
For example, mod (9, 2.4) = 0
That is: 9 In addition to 2.2, the whole quotient is 4; 4 and the division number is 2.2; 8.8 is 9 after rounding; the difference between 9 and 9 is 0. Therefore, the result is 0.
OK. I wonder if this meets your needs?
%

The code is as follows:

Echo 15% 4;
// The result is 3.

Let's talk about the point in the remainder operation (%) of php.

Today I saw Baidu know in this example: http://zhidao.baidu.com/question/41130500.html, just want to study the remaining php!

The code is as follows:

\$ Val = 9.45;
\$ Result = \$ val * 100;
Echo \$ result % 100; // 44
?>

The questioner wondered why the output above was recorded as 44 rather than the expected 45. if you do this:

Echo 945% 100 // 45

The result should be 45, because php performs the remainder operation on the variables by default.

About php floating point number, laruence blog: http://www.laruence.com/2013/03/26/2884.html

Then let's look at the following php code:

The code is as follows:

/*
* Name: mckee
* Blog: http://www.phpddt.com
*/
\$ Val = 9.45;
\$ Result = \$ val * 100;
Echo intval (\$ result); // output 944
Echo \$ result % 100; // output 44
Echo fmod (floatval (\$ result), 100); // output 45
?>

For a large integer, php will overflow and may return a negative number (this is because php uses an integer to take the remainder by default, so you need to convert it to the float type (as shown above )) you can do this:

The code is as follows:

Function Kmod (\$ bn, \$ sn)
{
Return intval (fmod (floatval (\$ bn), \$ sn ));
}

Using PHP to retrieve the remainder function PHP two get the remainder MOD (x, y) x % y MOD for example: 9/3, 9 is the divisor, and 3 is the divisor. the mod function is a remainder function in the format of mod (nExp1, nExp2), that is, two...

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.