Blame view

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