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] [Versions 401 and 402] [Versions 401 and 403]

   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\Statistical;
   9  
  10  class Factorial
  11  {
  12      use ArrayEnabled;
  13  
  14      /**
  15       * FACT.
  16       *
  17       * Returns the factorial of a number.
  18       * The factorial of a number is equal to 1*2*3*...* number.
  19       *
  20       * Excel Function:
  21       *        FACT(factVal)
  22       *
  23       * @param array|float $factVal Factorial Value, or can be an array of numbers
  24       *
  25       * @return array|float|int|string Factorial, or a string containing an error
  26       *         If an array of numbers is passed as the argument, then the returned result will also be an array
  27       *            with the same dimensions
  28       */
  29      public static function fact($factVal)
  30      {
  31          if (is_array($factVal)) {
  32              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $factVal);
  33          }
  34  
  35          try {
  36              $factVal = Helpers::validateNumericNullBool($factVal);
  37              Helpers::validateNotNegative($factVal);
  38          } catch (Exception $e) {
  39              return $e->getMessage();
  40          }
  41  
  42          $factLoop = floor($factVal);
  43          if ($factVal > $factLoop) {
  44              if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
  45                  return Statistical\Distributions\Gamma::gammaValue($factVal + 1);
  46              }
  47          }
  48  
  49          $factorial = 1;
  50          while ($factLoop > 1) {
  51              $factorial *= $factLoop--;
  52          }
  53  
  54          return $factorial;
  55      }
  56  
  57      /**
  58       * FACTDOUBLE.
  59       *
  60       * Returns the double factorial of a number.
  61       *
  62       * Excel Function:
  63       *        FACTDOUBLE(factVal)
  64       *
  65       * @param array|float $factVal Factorial Value, or can be an array of numbers
  66       *
  67       * @return array|float|int|string Double Factorial, or a string containing an error
  68       *         If an array of numbers is passed as the argument, then the returned result will also be an array
  69       *            with the same dimensions
  70       */
  71      public static function factDouble($factVal)
  72      {
  73          if (is_array($factVal)) {
  74              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $factVal);
  75          }
  76  
  77          try {
  78              $factVal = Helpers::validateNumericNullSubstitution($factVal, 0);
  79              Helpers::validateNotNegative($factVal);
  80          } catch (Exception $e) {
  81              return $e->getMessage();
  82          }
  83  
  84          $factLoop = floor($factVal);
  85          $factorial = 1;
  86          while ($factLoop > 1) {
  87              $factorial *= $factLoop;
  88              $factLoop -= 2;
  89          }
  90  
  91          return $factorial;
  92      }
  93  
  94      /**
  95       * MULTINOMIAL.
  96       *
  97       * Returns the ratio of the factorial of a sum of values to the product of factorials.
  98       *
  99       * @param mixed[] $args An array of mixed values for the Data Series
 100       *
 101       * @return float|string The result, or a string containing an error
 102       */
 103      public static function multinomial(...$args)
 104      {
 105          $summer = 0;
 106          $divisor = 1;
 107  
 108          try {
 109              // Loop through arguments
 110              foreach (Functions::flattenArray($args) as $argx) {
 111                  $arg = Helpers::validateNumericNullSubstitution($argx, null);
 112                  Helpers::validateNotNegative($arg);
 113                  $arg = (int) $arg;
 114                  $summer += $arg;
 115                  $divisor *= self::fact($arg);
 116              }
 117          } catch (Exception $e) {
 118              return $e->getMessage();
 119          }
 120  
 121          $summer = self::fact($summer);
 122  
 123          return $summer / $divisor;
 124      }
 125  }