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\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      //
  47      //    Private method to calculate the erfc value
  48      //
  49      private static $oneSqrtPi = 0.564189583547756287;
  50  
  51      private static function erfcValue($value)
  52      {
  53          if (abs($value) < 2.2) {
  54              return 1 - Erf::erfValue($value);
  55          }
  56          if ($value < 0) {
  57              return 2 - self::erfcValue(-$value);
  58          }
  59          $a = $n = 1;
  60          $b = $c = $value;
  61          $d = ($value * $value) + 0.5;
  62          $q1 = $q2 = $b / $d;
  63          do {
  64              $t = $a * $n + $b * $value;
  65              $a = $b;
  66              $b = $t;
  67              $t = $c * $n + $d * $value;
  68              $c = $d;
  69              $d = $t;
  70              $n += 0.5;
  71              $q1 = $q2;
  72              $q2 = $b / $d;
  73          } while ((abs($q1 - $q2) / $q2) > Functions::PRECISION);
  74  
  75          return self::$oneSqrtPi * exp(-$value * $value) * $q2;
  76      }
  77  }