Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   8  
   9  class ErfC
  10  {
  11      use ArrayEnabled;
  12  
  13      /**
  14       * ERFC.
  15       *
  16       *    Returns the complementary ERF function integrated between x and infinity
  17       *
  18       *    Note: In Excel 2007 or earlier, if you input a negative value for the lower bound argument,
  19       *        the function would return a #NUM! error. However, in Excel 2010, the function algorithm was
  20       *        improved, so that it can now calculate the function for both positive and negative x values.
  21       *            PhpSpreadsheet follows Excel 2010 behaviour, and accepts nagative arguments.
  22       *
  23       *    Excel Function:
  24       *        ERFC(x)
  25       *
  26       * @param mixed $value The float lower bound for integrating ERFC
  27       *                      Or can be an array of values
  28       *
  29       * @return array|float|string
  30       *         If an array of numbers is passed as an argument, then the returned result will also be an array
  31       *            with the same dimensions
  32       */
  33      public static function ERFC($value)
  34      {
  35          if (is_array($value)) {
  36              return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $value);
  37          }
  38  
  39          if (is_numeric($value)) {
  40              return self::erfcValue($value);
  41          }
  42  
  43          return ExcelError::VALUE();
  44      }
  45  
  46      private const ONE_SQRT_PI = 0.564189583547756287;
  47  
  48      /**
  49       * Method to calculate the erfc value.
  50       *
  51       * @param float|int|string $value
  52       *
  53       * @return float
  54       */
  55      private static function erfcValue($value)
  56      {
  57          $value = (float) $value;
  58          if (abs($value) < 2.2) {
  59              return 1 - Erf::erfValue($value);
  60          }
  61          if ($value < 0) {
  62              return 2 - self::erfcValue(-$value);
  63          }
  64          $a = $n = 1;
  65          $b = $c = $value;
  66          $d = ($value * $value) + 0.5;
  67          $q2 = $b / $d;
  68          do {
  69              $t = $a * $n + $b * $value;
  70              $a = $b;
  71              $b = $t;
  72              $t = $c * $n + $d * $value;
  73              $c = $d;
  74              $d = $t;
  75              $n += 0.5;
  76              $q1 = $q2;
  77              $q2 = $b / $d;
  78          } while ((abs($q1 - $q2) / $q2) > Functions::PRECISION);
  79  
  80          return self::ONE_SQRT_PI * exp(-$value * $value) * $q2;
  81      }
  82  }