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 ErfC
   8  {
   9      /**
  10       * ERFC.
  11       *
  12       *    Returns the complementary ERF function integrated between x and infinity
  13       *
  14       *    Note: In Excel 2007 or earlier, if you input a negative value for the lower bound argument,
  15       *        the function would return a #NUM! error. However, in Excel 2010, the function algorithm was
  16       *        improved, so that it can now calculate the function for both positive and negative x values.
  17       *            PhpSpreadsheet follows Excel 2010 behaviour, and accepts nagative arguments.
  18       *
  19       *    Excel Function:
  20       *        ERFC(x)
  21       *
  22       * @param mixed $value The float lower bound for integrating ERFC
  23       *
  24       * @return float|string
  25       */
  26      public static function ERFC($value)
  27      {
  28          $value = Functions::flattenSingleValue($value);
  29  
  30          if (is_numeric($value)) {
  31              return self::erfcValue($value);
  32          }
  33  
  34          return Functions::VALUE();
  35      }
  36  
  37      //
  38      //    Private method to calculate the erfc value
  39      //
  40      private static $oneSqrtPi = 0.564189583547756287;
  41  
  42      private static function erfcValue($value)
  43      {
  44          if (abs($value) < 2.2) {
  45              return 1 - Erf::erfValue($value);
  46          }
  47          if ($value < 0) {
  48              return 2 - self::ERFC(-$value);
  49          }
  50          $a = $n = 1;
  51          $b = $c = $value;
  52          $d = ($value * $value) + 0.5;
  53          $q1 = $q2 = $b / $d;
  54          do {
  55              $t = $a * $n + $b * $value;
  56              $a = $b;
  57              $b = $t;
  58              $t = $c * $n + $d * $value;
  59              $c = $d;
  60              $d = $t;
  61              $n += 0.5;
  62              $q1 = $q2;
  63              $q2 = $b / $d;
  64          } while ((abs($q1 - $q2) / $q2) > Functions::PRECISION);
  65  
  66          return self::$oneSqrtPi * exp(-$value * $value) * $q2;
  67      }
  68  }