| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Engineering; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 8 |
|
| 9 |
class Erf |
| 10 |
{ |
| 11 |
use ArrayEnabled; |
| 12 |
|
| 13 |
private const TWO_SQRT_PI = 1.128379167095512574; |
| 14 |
|
| 15 |
/** |
| 16 |
* ERF. |
| 17 |
* |
| 18 |
* Returns the error function integrated between the lower and upper bound arguments. |
| 19 |
* |
| 20 |
* Note: In Excel 2007 or earlier, if you input a negative value for the upper or lower bound arguments, |
| 21 |
* the function would return a #NUM! error. However, in Excel 2010, the function algorithm was |
| 22 |
* improved, so that it can now calculate the function for both positive and negative ranges. |
| 23 |
* PhpSpreadsheet follows Excel 2010 behaviour, and accepts negative arguments. |
| 24 |
* |
| 25 |
* Excel Function: |
| 26 |
* ERF(lower[,upper]) |
| 27 |
* |
| 28 |
* @param mixed $lower Lower bound float for integrating ERF |
| 29 |
* Or can be an array of values |
| 30 |
* @param mixed $upper Upper bound float for integrating ERF. |
| 31 |
* If omitted, ERF integrates between zero and lower_limit |
| 32 |
* Or can be an array of values |
| 33 |
* |
| 34 |
* @return array<mixed>|float|string If an array of numbers is passed as an argument, then the returned result will also be an array |
| 35 |
* with the same dimensions |
| 36 |
*/ |
| 37 |
public static function ERF($lower, $upper = null) |
| 38 |
{ |
| 39 |
if (is_array($lower) || is_array($upper)) { |
| 40 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $lower, $upper); |
| 41 |
} |
| 42 |
|
| 43 |
if (is_numeric($lower)) { |
| 44 |
if ($upper === null) { |
| 45 |
return self::erfValue($lower); |
| 46 |
} |
| 47 |
if (is_numeric($upper)) { |
| 48 |
return self::erfValue($upper) - self::erfValue($lower); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return ExcelError::VALUE(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* ERFPRECISE. |
| 57 |
* |
| 58 |
* Returns the error function integrated between the lower and upper bound arguments. |
| 59 |
* |
| 60 |
* Excel Function: |
| 61 |
* ERF.PRECISE(limit) |
| 62 |
* |
| 63 |
* @param mixed $limit Float bound for integrating ERF, other bound is zero |
| 64 |
* Or can be an array of values |
| 65 |
* |
| 66 |
* @return array<mixed>|float|string If an array of numbers is passed as an argument, then the returned result will also be an array |
| 67 |
* with the same dimensions |
| 68 |
*/ |
| 69 |
public static function ERFPRECISE($limit) |
| 70 |
{ |
| 71 |
if (is_array($limit)) { |
| 72 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $limit); |
| 73 |
} |
| 74 |
|
| 75 |
return self::ERF($limit); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @param mixed $value |
| 80 |
*/ |
| 81 |
private static function makeFloat($value): float |
| 82 |
{ |
| 83 |
return is_numeric($value) ? ((float) $value) : 0.0; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Method to calculate the erf value. |
| 88 |
* @param float|int|string $value |
| 89 |
*/ |
| 90 |
public static function erfValue($value): float |
| 91 |
{ |
| 92 |
$value = (float) $value; |
| 93 |
if (abs($value) > 2.2) { |
| 94 |
return 1 - self::makeFloat(ErfC::ERFC($value)); |
| 95 |
} |
| 96 |
$sum = $term = $value; |
| 97 |
$xsqr = ($value * $value); |
| 98 |
$j = 1; |
| 99 |
do { |
| 100 |
$term *= $xsqr / $j; |
| 101 |
$sum -= $term / (2 * $j + 1); |
| 102 |
++$j; |
| 103 |
$term *= $xsqr / $j; |
| 104 |
$sum += $term / (2 * $j + 1); |
| 105 |
++$j; |
| 106 |
if ($sum == 0.0) { |
| 107 |
break; |
| 108 |
} |
| 109 |
} while (abs($term / $sum) > Functions::PRECISION); |
| 110 |
|
| 111 |
return self::TWO_SQRT_PI * $sum; |
| 112 |
} |
| 113 |
} |
| 114 |
|