Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\MathTrig;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   9  
  10  class Operations
  11  {
  12      use ArrayEnabled;
  13  
  14      /**
  15       * MOD.
  16       *
  17       * @param mixed $dividend Dividend
  18       *                      Or can be an array of values
  19       * @param mixed $divisor Divisor
  20       *                      Or can be an array of values
  21       *
  22       * @return array|float|int|string Remainder, or a string containing an error
  23       *         If an array of numbers is passed as an argument, then the returned result will also be an array
  24       *            with the same dimensions
  25       */
  26      public static function mod($dividend, $divisor)
  27      {
  28          if (is_array($dividend) || is_array($divisor)) {
  29              return self::evaluateArrayArguments([self::class, __FUNCTION__], $dividend, $divisor);
  30          }
  31  
  32          try {
  33              $dividend = Helpers::validateNumericNullBool($dividend);
  34              $divisor = Helpers::validateNumericNullBool($divisor);
  35              Helpers::validateNotZero($divisor);
  36          } catch (Exception $e) {
  37              return $e->getMessage();
  38          }
  39  
  40          if (($dividend < 0.0) && ($divisor > 0.0)) {
  41              return $divisor - fmod(abs($dividend), $divisor);
  42          }
  43          if (($dividend > 0.0) && ($divisor < 0.0)) {
  44              return $divisor + fmod($dividend, abs($divisor));
  45          }
  46  
  47          return fmod($dividend, $divisor);
  48      }
  49  
  50      /**
  51       * POWER.
  52       *
  53       * Computes x raised to the power y.
  54       *
  55       * @param array|float|int $x
  56       *                      Or can be an array of values
  57       * @param array|float|int $y
  58       *                      Or can be an array of values
  59       *
  60       * @return array|float|int|string The result, or a string containing an error
  61       *         If an array of numbers is passed as an argument, then the returned result will also be an array
  62       *            with the same dimensions
  63       */
  64      public static function power($x, $y)
  65      {
  66          if (is_array($x) || is_array($y)) {
  67              return self::evaluateArrayArguments([self::class, __FUNCTION__], $x, $y);
  68          }
  69  
  70          try {
  71              $x = Helpers::validateNumericNullBool($x);
  72              $y = Helpers::validateNumericNullBool($y);
  73          } catch (Exception $e) {
  74              return $e->getMessage();
  75          }
  76  
  77          // Validate parameters
  78          if (!$x && !$y) {
  79              return ExcelError::NAN();
  80          }
  81          if (!$x && $y < 0.0) {
  82              return ExcelError::DIV0();
  83          }
  84  
  85          // Return
  86          $result = $x ** $y;
  87  
  88          return Helpers::numberOrNan($result);
  89      }
  90  
  91      /**
  92       * PRODUCT.
  93       *
  94       * PRODUCT returns the product of all the values and cells referenced in the argument list.
  95       *
  96       * Excel Function:
  97       *        PRODUCT(value1[,value2[, ...]])
  98       *
  99       * @param mixed ...$args Data values
 100       *
 101       * @return float|string
 102       */
 103      public static function product(...$args)
 104      {
 105          $args = array_filter(
 106              Functions::flattenArray($args),
 107              function ($value) {
 108                  return $value !== null;
 109              }
 110          );
 111  
 112          // Return value
 113          $returnValue = (count($args) === 0) ? 0.0 : 1.0;
 114  
 115          // Loop through arguments
 116          foreach ($args as $arg) {
 117              // Is it a numeric value?
 118              if (is_numeric($arg)) {
 119                  $returnValue *= $arg;
 120              } else {
 121                  return ExcelError::throwError($arg);
 122              }
 123          }
 124  
 125          return (float) $returnValue;
 126      }
 127  
 128      /**
 129       * QUOTIENT.
 130       *
 131       * QUOTIENT function returns the integer portion of a division. Numerator is the divided number
 132       *        and denominator is the divisor.
 133       *
 134       * Excel Function:
 135       *        QUOTIENT(value1,value2)
 136       *
 137       * @param mixed $numerator Expect float|int
 138       *                      Or can be an array of values
 139       * @param mixed $denominator Expect float|int
 140       *                      Or can be an array of values
 141       *
 142       * @return array|int|string
 143       *         If an array of numbers is passed as an argument, then the returned result will also be an array
 144       *            with the same dimensions
 145       */
 146      public static function quotient($numerator, $denominator)
 147      {
 148          if (is_array($numerator) || is_array($denominator)) {
 149              return self::evaluateArrayArguments([self::class, __FUNCTION__], $numerator, $denominator);
 150          }
 151  
 152          try {
 153              $numerator = Helpers::validateNumericNullSubstitution($numerator, 0);
 154              $denominator = Helpers::validateNumericNullSubstitution($denominator, 0);
 155              Helpers::validateNotZero($denominator);
 156          } catch (Exception $e) {
 157              return $e->getMessage();
 158          }
 159  
 160          return (int) ($numerator / $denominator);
 161      }
 162  }