Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

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