What is the best function or module calculation method (remainder $ a divided by $ b) in PHP?

PHP provides various functions for calculating a module:

What function should be used that is considered safe and effective? considering (the description of the zero problem described here: Division by zero error when using the module and floating numbers?

+7
php numbers modulus
source share
4 answers
  • Only the module operator (%) and fmod are native

  • The module operator cannot process numbers outside of 2^32 (in PHP with x86 architecture)

  • fmod is faster than bcmod / gmp_mod ~ benchmark

  • bcmod does not work with floats ~ here

I think it's better to use fmod , simply because it works faster in Math Functions than other functions, and, most importantly, it can process large numbers and float.

If you do not plan to use numbers per limit or floats, use % as it should be the fastest.

+21
source share

All functions are safe to use if you know what they are doing:

  • Module operator: fast, accurate, but you need to stay under max int. In these terms, there is no division by the null problem.

  • fmod: fast, floating point speed is no longer a problem in the last 10 years, but complex. Floating-point numbers can reach much higher values, but their accuracy is lacking. Therefore, work with large numbers will increasingly introduce rounding errors. Thus, it will not solve anything that the module operator lacks, it will introduce problems with accuracy.

  • bcmod and gmp_mod are similar. The difference is that these functions use different implementations (modules) of arbitrary precision arithmetic. Their use is different, but their results are not so: theoretically, the size of the numbers supported by these libraries is limited only by the amount of free memory, so in practice the result will always be good and accurate, however, the algorithms used are overloaded both computationally and from memory.

+2
source share

You can still use the module operator, and it can still be safe if you plan on expecting a zero point at some point, as shown below.

 if ($y > 0) { // Do the calculation } else { // Do this instead } 

You can also use the Error Control Operator to hide warnings. Using the @ operator, the error is suppressed.

However, with % integer can be so large that it is converted to a float, hence the introduction of bcmod and fmod , but note that they have their limitations.

I would recommend GMP, or perhaps take a look at one of the other advanced math functions, there is also BCMath

Since you mentioned something safe, there is a class for this: Eval Math

Here is an example using fmod

 <?php echo fmod('4294967296', 34); ?> 

outputs 18

+1
source share

Note that fmod is NOT equivalent to this basic function:

  <?php function modulo($a, $b) { return $a - $b * floor($a / $b); } ?> 

because fmod () will return a value with the same sign as $ a. In other words, the floor () function is incorrect, because it is rounded to -INF, and not to zero.

To emulate fmod ($ a, $ b), the correct way:

  <?php function fmod($a, $b) { return $a - $b * (($b < 0) ? ceil($a / $b) : floor($a / $b))); } ?> 

Note that both functions will generate a DIVISION BY ZERO if $ b is null.

The first function modulo () above is a mathematical function that is useful for working on cyclic structures (such as calendar calculations or trigonometric functions:

  - fmod($a, 2*PI) returns a value in [0..2*PI) if $a is positive - fmod($a, 2*PI) returns a value in [-2*PI..0] if $a is negative - modulo($a, 2*PI) returns a value always in [0..2*PI) independantly of the sign of $a 
0
source share

All Articles