Home >Backend Development >PHP Tutorial >Why does 8%-3 in PHP equal 0?

Why does 8%-3 in PHP equal 0?

PHPz
PHPzOriginal
2024-01-26 10:11:07616browse

Why does 8%-3 in PHP equal 0?

Why does 8%-3 in PHP equal 0?

In PHP, for some simple mathematical operations, we always hope to get the correct results. However, sometimes the results of some simple operations confuse us. Just like in PHP, the result of calculating 8%-3 is 0. Why does this happen?

To understand this problem, we must first understand the definition of modulo operation. In mathematics, the modulo operation is to find the remainder, usually expressed as "a mod n", where a is the dividend and n is the divisor. The result of the modulo operation is a non-negative integer ranging from 0 to n-1. For example, the result of 9 mod 4 is 1 because 9 divided by 4 equals 2 remainder 1.

In PHP, the modulo operation is represented by the % symbol. According to the PHP manual, the sign of the result of the modulo operation is the same as the dividend. That is, if the dividend is positive, the result is also positive; if the dividend is negative, the result is also negative.

Back to our question, 8 divided by 3 equals 2 remainder 2. According to the definition of modulo operation, the result should be 2. But in PHP, the result of 8%-3 is 0. Does this violate the definition of modulo operation?

In fact, this phenomenon is not unique to PHP, but a way of defining modulo operations. In some programming languages, the result of the modulo operation can be negative. The specific definition will vary according to the specification of the programming language. In PHP, the "round toward zero" rule of modulo arithmetic is used.

According to the PHP manual, when the dividend is a positive number, the result of the modulo operation is equal to the remainder of the dividend and the divisor; when the dividend is a negative number, the result of the modulo operation is equal to the dividend divided by the divisor after rounding. The remainder of the result, whichever is greater than 0. In other words, the result of PHP's modulo operation is always smaller than the absolute value of the dividend, but the sign is the same as the dividend.

So, back to the example of 8%-3. 8 is a positive number and -3 is a negative number. According to the "round toward zero" rule, the result of the modulo operation should be the remainder of the larger of the result of dividing 8 by -3 and 0. The result of dividing 8 by -3 and rounding is -3, and the larger one compared with 0 is still 0.

Therefore, the result of 8%-3 is equal to 0.

This way of definition can simplify the logic of the code in some scenarios, but it can also be easily misunderstood. If we want to get the remainder of a positive number, we can use the function fmod() provided by PHP, which returns the remainder of the division of floating point numbers.

To summarize, 8%-3 in PHP is equal to 0 due to the way the modulo operation is defined, that is, the "rounding toward zero" rule. To understand this phenomenon, we need to understand the specifications of the modulo operation of the programming language used, and choose the appropriate operation method according to the actual situation.

The above is the detailed content of Why does 8%-3 in PHP equal 0?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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

Related articles

See more