Remain function PHP Remain function PHP two remain MOD (x,y) x%y
MOD
For example: 9/3, 9 is the dividend, and 3 is the dividend. The mod function is a remnant function, and its format is:
mod(nExp1,nExp2) is the remainder after two numerical expressions are divided. Then: finding the remainder of two integers of the same sign is exactly the same as finding the remainder of two positive numbers as you know (that is, the algorithm for two negative integers and two positive integers is the same).
1. Find the remaining number of two different numbers
1. Function value symbol rule (remaining symbol)
mod(negative, positive)=positive
mod(positive, negative)=negative
Conclusion: When two integers are calculated, the sign of their value is the sign of the divisor.
2. Value rules
First, treat two integers as positive numbers, and then divide
① When divisible, its value is 0
② When it cannot be divided, its value = divisor × (total quotient + 1)-divided
Example: mod(9,-8)=-7
That is: the integer quotient divided by 8 is 1, and the product with 1 is 2; the product with the divisor is 18; the difference with the number is 7; the sign of the divisor is taken. So the value is -7.
2. Find the remaining two decimals
Value rule: After being divisor-(whole quotient × divisor), rounded in the first decimal place.
Example: mod(9,1.2)=1
That is, the whole quotient of 9 is divided into 1.2 is 7; the product of 7 and divisor 1.2 is 8.4; after rounding 8.4, it is 8; the difference between divisor 9 and 8 is 1. Therefore, the result is 1.
Example: mod(9,2.4)=0
That is, the whole quotient of 9 is 4 when dividing 2.2 is 4; the product of 4 and divisor 2.2 is 8.8; after rounding 8.8, it is 9; the difference between divisor 9 and 9 is 0. Therefore, the result is 0.
OK, I wonder if these can meet your needs?
%
<?php
echo 15%4;
//The result is 3
Tell me about the php remnant calculation (%)
Today I saw this example in Baidu Knows:/question/, I just want to study the php to get the rest!
<?php
$val=9.45;
$result=$val*100;
echo $result%100; //44
?>
The questioner is wondering why the above demerit is 44 instead of the imagined 45, if you do this:
echo 945%100 //45
Then the result should be 45, because PHP uses rounding variables by default to perform the remainder operation.
About php floating point numbers, situation laruence blog post:/2013/03/26/
Then look at the following php code:
<?php
/*
* name: mckee
* blog:
*/
$val=9.45;
$result=$val*100;
echo intval($result); //The output here is 944
echo $result%100; //Output 44 here
echo fmod(floatval($result),100); //Output 45 here
?>
For large integers, php will overflow and may return negative numbers (this is because php uses integers to get the remainder by default, so you want to convert it to float type (as above)) You can do this:
function Kmod($bn, $sn)
{
return intval(fmod(floatval($bn), $sn));
}