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\Engineering;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   6  
   7  class Erf
   8  {
   9      private static $twoSqrtPi = 1.128379167095512574;
  10  
  11      /**
  12       * ERF.
  13       *
  14       * Returns the error function integrated between the lower and upper bound arguments.
  15       *
  16       *    Note: In Excel 2007 or earlier, if you input a negative value for the upper or lower bound arguments,
  17       *            the function would return a #NUM! error. However, in Excel 2010, the function algorithm was
  18       *            improved, so that it can now calculate the function for both positive and negative ranges.
  19       *            PhpSpreadsheet follows Excel 2010 behaviour, and accepts negative arguments.
  20       *
  21       *    Excel Function:
  22       *        ERF(lower[,upper])
  23       *
  24       * @param mixed $lower Lower bound float for integrating ERF
  25       * @param mixed $upper Upper bound float for integrating ERF.
  26       *                           If omitted, ERF integrates between zero and lower_limit
  27       *
  28       * @return float|string
  29       */
  30      public static function ERF($lower, $upper = null)
  31      {
  32          $lower = Functions::flattenSingleValue($lower);
  33          $upper = Functions::flattenSingleValue($upper);
  34  
  35          if (is_numeric($lower)) {
  36              if ($upper === null) {
  37                  return self::erfValue($lower);
  38              }
  39              if (is_numeric($upper)) {
  40                  return self::erfValue($upper) - self::erfValue($lower);
  41              }
  42          }
  43  
  44          return Functions::VALUE();
  45      }
  46  
  47      /**
  48       * ERFPRECISE.
  49       *
  50       * Returns the error function integrated between the lower and upper bound arguments.
  51       *
  52       *    Excel Function:
  53       *        ERF.PRECISE(limit)
  54       *
  55       * @param mixed $limit Float bound for integrating ERF, other bound is zero
  56       *
  57       * @return float|string
  58       */
  59      public static function ERFPRECISE($limit)
  60      {
  61          $limit = Functions::flattenSingleValue($limit);
  62  
  63          return self::ERF($limit);
  64      }
  65  
  66      //
  67      //    Private method to calculate the erf value
  68      //
  69      public static function erfValue($value)
  70      {
  71          if (abs($value) > 2.2) {
  72              return 1 - ErfC::ERFC($value);
  73          }
  74          $sum = $term = $value;
  75          $xsqr = ($value * $value);
  76          $j = 1;
  77          do {
  78              $term *= $xsqr / $j;
  79              $sum -= $term / (2 * $j + 1);
  80              ++$j;
  81              $term *= $xsqr / $j;
  82              $sum += $term / (2 * $j + 1);
  83              ++$j;
  84              if ($sum == 0.0) {
  85                  break;
  86              }
  87          } while (abs($term / $sum) > Functions::PRECISION);
  88  
  89          return self::$twoSqrtPi * $sum;
  90      }
  91  }