fmod


php128 apg

RETURNS the floating point remainder of the division of the arguments.

Compatible with LOCALE only, up to PHP 7.4.XX.





This function returns the floating point remainder of dividing the $num1 by $num2 with $num2 ≠ 0.



<?php

float fmod 
( float $num1 , float $num2 )


where,

$num1 = The dividend
            
$num2 
= The divisor
            
?>

$num1


The dividend.



$num2


The divisor.



  1 EXERCISE   

<?php
 
$x01a 
= mt_rand(30, 42);

$y01a =  mt_rand(1, 30);

$mod01a = fmod($x01a, $y01a);

echo 
$x01a . ' divided by ' . $y01a . 
                    
' produces a remainder of ' . $mod01a . '<br><br>';

$mod01b = $x01a%$y01a;

echo 
'The same result as ( ' . $x01a . '%' . $y01a . ' ) = ' . 
                               
$mod01b . '<br><br><br>';

$quoc01a = intdiv($x01a, $y01a);

echo 
'If intdiv ( ' . $x01a . ', ' . $y01a . ' ) = ' . $quoc01a; 

echo 
'<br><br>then ' . $x01a . 
         
' = ( ' . $y01a . ' * ' . $quoc01a . ' ) + ' . $mod01a . '<br>'; 

?> 

  2 EXERCISE   

<?php


setlocale
(LC_ALL, "fr_FR.utf-8", "fr", "fr-FR");
 
$x02a = 88.9986;

$y02a =  52.132;

$mod02a = fmod($x02a, $y02a);

echo 
$x02a . ' divided by ' . $y02a . 
          
' produces a remainder of ' . $mod02a . '<br><br>';

$quoc02a = intdiv($x02a,$y02a);

echo 
'If intdiv( ' . $x02a . ',' . $y02a . ' ) &approx; ' . $quoc02a; 

echo 
'<br><br>then ' . $x02a . ' &approx; ( ' . 
                  
$y02a . ' * ' . $quoc02a . ' ) + ' . $mod02a . '<br>'; 

?> 

  3 EXERCISE   

<?php

$values1 
= [234,
                -
234,
                
23.45e1,
                -
23.45e1,
                
0xEA,
                
0352,
                
"234",
                
"234.5",
                
"23.45e1",
                
null,
                
true,
                
false ];

$values2 = [2,
                -
2,
                
2.3e1,
                -
2.3e1,
                
0x2,
                
02,
                
"2",
                
"2.3",
                
"2.3e1",
                
null,
                
true,
                
false ];
for (
$i = 0; $i < count($values1); $i++) {
    echo 
"<br><br>Iteration: ", $i, "<br>";

    for (
$j = 0; $j < count($values2); $j++) {
        
$res = fmod($values1[$i], $values2[$j]);
        
var_dump($res);
    }
}

?>

  4 EXERCISE   

<?php

if (PHP_INT_SIZE != 8) 
die(
"skip this test is for 64bit platform only");


define("MAX_64Bit", 9223372036854775807);
define("MAX_32Bit", 2147483647);
define("MIN_64Bit", -9223372036854775807 - 1);
define("MIN_32Bit", -2147483647 - 1);

$longVals = array(
    
MAX_64Bit, MIN_64Bit, MAX_32Bit, 
    
MIN_32Bit, MAX_64Bit - MAX_32Bit, 
    
MIN_64Bit - MIN_32Bit,
    
MAX_32Bit + 1, MIN_32Bit - 1, 
    
MAX_32Bit * 2, (MAX_32Bit * 2) + 1, 
    (
MAX_32Bit * 2) - 1,
    
MAX_64Bit -1, MAX_64Bit + 1, 
    
MIN_64Bit + 1, MIN_64Bit - 1
);

$otherVals = array(0, 1, -1, 7, 9, 65, -44, 
       
MAX_32Bit, MIN_32Bit, MAX_64Bit, MIN_64Bit);


foreach (
$longVals as $longVal) {
   foreach(
$otherVals as $otherVal) {
       echo 
"<br><br>Testing: $longVal, $otherVal<br>";
      
var_dump(fmod($longVal, $otherVal));
   }
}

?>