Introduction to the PHP function mod (x,y) and x%y_php skills

Source: Internet
Author: User

The remainder function PHP takes the remainder function PHP two to take over MOD (x,y) x%y

MOD

For example: 9/3,9 is a divisor, 3 is a divisor. The MoD function is a remainder function in the form of:
MoD (NEXP1,NEXP2), which is the remainder of two numeric expressions that are division-calculated. So: Two identical integer remainder is exactly the same as one of the two positive numbers you know (i.e. two negative integers and two positive integer algorithms).

One or two odd numbers

1. Function value symbol rule (symbol of remainder)
MoD (negative, positive) = positive
MoD (positive, negative) = negative
Conclusion: When two integers are left, the symbol of the value is the divisor.
2. Rule of value
First, consider two integers as positive numbers, then divide.
When ① can be divisible, its value is 0
② is not divisible, its value = divisor x (whole quotient +1)-dividend
Example: MoD (9,-8) =-7
That is: 9 divided by 8 of the integer quotient of 1, plus 1 after 2, and the product of the divisor is 18, then the difference between the number is 7, the symbol of the divisor. So the value is-7.

second, two decimal to find surplus

Value rule: the divisor-(whole quotient x divisor) is rounded at the first decimal place.
Example: MoD (9,1.2) =1
That is: 9 except 1.2 its whole quotient for 7;7 and divisor 1.2 of the product for the 8.4;8.4 rounded 8; the difference between 9 and 8 is 1. So the result is 1.
Example: MoD (9,2.4) =0
That is: 9 except 2.2 its whole quotient for 4;4 and divisor 2.2 This product is rounded for 8.8;8.8 9; the difference between 9 and 9 is 0. So the result is 0.
OK, do not know if this will meet your needs?
%

Copy Code code as follows:

<?php
Echo 15%4;
The result is 3.


talk about the point of the PHP take-over operation (%)

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

Copy Code code as follows:

<?php
$val = 9.45;
$result = $val *100;
echo $result% 100; 44
?>

The questioner wondered why the demerit was 44 rather than the imaginary 45, if you did:

Echo 945%100//45

The result should be 45, because PHP defaults to the variable to take the remainder of the operation.

About PHP floating-point number, Situation laruence blog article: http://www.laruence.com/2013/03/26/2884.html

Then look at the following PHP code:

Copy Code code as follows:

<?php
/*
* Name:mckee
* blog:http://www.phpddt.com
*/
$val = 9.45;
$result = $val *100;
echo intval ($result); Output 944 Here
echo $result% 100; Output 44 here
Echo Fmod (Floatval ($result), 100); Output 45 here
?>

For large integers, PHP will overflow and may return a negative situation (this is because PHP defaults to use integer remainder, so you have to convert it to float type (as above)) can do this:

Copy Code code as follows:

function Kmod ($bn, $SN)
{
Return Intval (Fmod (Floatval ($bn), $SN));
}

Contact Us

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.