bcmod


php128 apg

GET the modulus of an arbitrary precision number.





This function returns the remainder of dividing $num1 by $num2.

If $num1 = 0, this function returns NULL.

The parameter $scale was added in PHP 7.2.0.

$num1 and $num2 are no longer truncated to INTEGER.

Now the behavior of this function follows fmod functions rather than the  %  operator.


<?php

string bcmod 
( string $num1, 
                        
string $num2, 
                           ?
int $scale = null )


where,

$num1 = The dividend as STRING

$num2 
= The divisor as STRING

$scale 
= The number of decimal places considered by this operation

?>
 

$num1


The dividend treated as STRING.



$num2


The divisor treated as STRING.



$scale


The number of decimal places to be considered.

This is nullable as of PHP 8.0.XX.



  1 EXERCISE   

<?php

$s01a 
= 4;

$v01a = 88.9986;

$v01b = 52.132;

$bc01 = bcmod($v01a, $v01b, $s01a );

echo 
'The division:<br><br>( ' . $v01a . ' / ' . $v01b . ' ) =<br><br>= bcmod( ' . $v01a . ', ' . $v01b . ', ' . $s01a .  ' )<br><br>remainder = ' . $bc01 .' [ scale( ' . $s01a . ' )]<br><br><br>';

$fm01 = fmod($v01a, $v01b);

echo 
'The same as:<br><br>fmod( '  . $v01a . ', ' . $v01b . ' ) = ' . $fm01 . '<br><br><br>';

$nd01 = intdiv($v01a, $v01b);

echo 
'This leads us to:<br><br>intdiv( ' . $v01a . ', ' . $v01b .  ' ) = ' . $nd01 .'<br><br>where:<br><br>';

$cp01 = ($v01b * $nd01) + $bc01;

echo 
$cp01 . ' = (' . $v01a . ' * ' . $nd01 . ') + ' . $bc01 ;

?> 

 RESULT   

The division

( 88.9986 / 52.132 ) =

= bcmod( 88.9986, 52.132, 4 )

remainder = 36.8666 [ scale( 4 ) ]



The same as

fmod( 88.9986, 52.132 ) = 36.8666


This leads us to

( 88.9986 / 52.132 ) =

= intdiv( 88.9986, 52.132 ) = 1


where

88.9986 = (88.9986 * 1) + 36.8666