30 define(
'EPS', 2.22e-16);
33 define(
'MAX_VALUE', 1.2e308);
36 define(
'LOG_GAMMA_X_MAX_VALUE', 2.55e305);
39 define(
'SQRT2PI', 2.5066282746310005024157652848110452530069867406099);
42 define(
'XMININ', 2.23e-308);
45 define(
'MAX_ITERATIONS', 150);
48 define(
'PRECISION', 8.88E-016);
51 define(
'EULER', 2.71828182845904523536);
55 ini_set(
'precision',16);
60 if (!defined(
'PHPEXCEL_ROOT')) {
64 define(
'PHPEXCEL_ROOT', dirname(__FILE__) .
'/../../');
68 require_once PHPEXCEL_ROOT .
'PHPExcel/Cell.php';
71 require_once PHPEXCEL_ROOT .
'PHPExcel/Cell/DataType.php';
74 require_once PHPEXCEL_ROOT .
'PHPExcel/Style/NumberFormat.php';
77 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/Date.php';
80 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/JAMA/Matrix.php';
81 require_once PHPEXCEL_ROOT .
'PHPExcel/Shared/trend/trendClass.php';
126 'divisionbyzero' =>
'#DIV/0!',
127 'value' =>
'#VALUE!',
128 'reference' =>
'#REF!',
132 'gettingdata' =>
'#GETTING_DATA'
188 if (($returnDateType == self::RETURNDATE_PHP_NUMERIC) ||
189 ($returnDateType == self::RETURNDATE_PHP_OBJECT) ||
190 ($returnDateType == self::RETURNDATE_EXCEL)) {
191 self::$ReturnDateType = $returnDateType;
222 return '#Not Yet Implemented';
233 public static function NA() {
234 return self::$_errorCodes[
'na'];
245 public static function NaN() {
246 return self::$_errorCodes[
'num'];
257 public static function NAME() {
258 return self::$_errorCodes[
'name'];
269 public static function REF() {
270 return self::$_errorCodes[
'reference'];
302 foreach ($aArgs as $arg) {
305 $returnValue = $returnValue && $arg;
307 }
elseif ((is_numeric($arg)) && (!is_string($arg))) {
308 $returnValue = $returnValue && ($arg != 0);
310 }
elseif (is_string($arg)) {
311 $arg = strtoupper($arg);
312 if ($arg ==
'TRUE') {
314 }
elseif ($arg ==
'FALSE') {
317 return self::$_errorCodes[
'value'];
319 $returnValue = $returnValue && ($arg != 0);
325 if ($argCount == 0) {
326 return self::$_errorCodes[
'value'];
355 $returnValue = False;
360 foreach ($aArgs as $arg) {
363 $returnValue = $returnValue || $arg;
365 }
elseif ((is_numeric($arg)) && (!is_string($arg))) {
366 $returnValue = $returnValue || ($arg != 0);
368 }
elseif (is_string($arg)) {
369 $arg = strtoupper($arg);
370 if ($arg ==
'TRUE') {
372 }
elseif ($arg ==
'FALSE') {
375 return self::$_errorCodes[
'value'];
377 $returnValue = $returnValue || ($arg != 0);
383 if ($argCount == 0) {
384 return self::$_errorCodes[
'value'];
472 public static function STATEMENT_IF($condition =
true, $returnIfTrue = 0, $returnIfFalse = False) {
476 if (is_null($returnIfTrue)) { $returnIfTrue = 0; }
477 if (is_null($returnIfFalse)) { $returnIfFalse = 0; }
479 return ($condition ? $returnIfTrue : $returnIfFalse);
518 $xCoordinate = (float) self::flattenSingleValue($xCoordinate);
519 $yCoordinate = (float) self::flattenSingleValue($yCoordinate);
521 if (($xCoordinate == 0) && ($yCoordinate == 0)) {
522 return self::$_errorCodes[
'divisionbyzero'];
525 return atan2($yCoordinate, $xCoordinate);
543 public static function LOG_BASE($number, $base=10) {
547 return log($number, $base);
564 public static function SUM() {
570 foreach ($aArgs as $arg) {
572 if ((is_numeric($arg)) && (!is_string($arg))) {
573 $returnValue += $arg;
601 foreach ($aArgs as $arg) {
603 if ((is_numeric($arg)) && (!is_string($arg))) {
604 $returnValue += pow($arg,2);
632 foreach ($aArgs as $arg) {
634 if ((is_numeric($arg)) && (!is_string($arg))) {
635 if (is_null($returnValue)) {
638 $returnValue *= $arg;
644 if (is_null($returnValue)) {
671 foreach ($aArgs as $arg) {
673 if ((is_numeric($arg)) && (!is_string($arg))) {
674 if (is_null($returnValue)) {
675 $returnValue = ($arg == 0) ? 0 : $arg;
677 if (($returnValue == 0) || ($arg == 0)) {
680 $returnValue /= $arg;
687 return intval($returnValue);
705 public static function MIN() {
711 foreach ($aArgs as $arg) {
713 if ((is_numeric($arg)) && (!is_string($arg))) {
714 if ((is_null($returnValue)) || ($arg < $returnValue)) {
721 if(is_null($returnValue)) {
741 public static function MINA() {
747 foreach ($aArgs as $arg) {
749 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg !=
'')))) {
751 $arg = (integer) $arg;
752 }
elseif (is_string($arg)) {
755 if ((is_null($returnValue)) || ($arg < $returnValue)) {
762 if(is_null($returnValue)) {
788 $n = array_pop($aArgs);
790 if ((is_numeric(
$n)) && (!is_string(
$n))) {
792 foreach ($aArgs as $arg) {
794 if ((is_numeric($arg)) && (!is_string($arg))) {
800 if ((
$n < 0) || (
$n >= $count) || ($count == 0)) {
801 return self::$_errorCodes[
'num'];
806 return self::$_errorCodes[
'value'];
824 public static function MAX() {
830 foreach ($aArgs as $arg) {
832 if ((is_numeric($arg)) && (!is_string($arg))) {
833 if ((is_null($returnValue)) || ($arg > $returnValue)) {
840 if(is_null($returnValue)) {
860 public static function MAXA() {
866 foreach ($aArgs as $arg) {
868 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg !=
'')))) {
870 $arg = (integer) $arg;
871 }
elseif (is_string($arg)) {
874 if ((is_null($returnValue)) || ($arg > $returnValue)) {
881 if(is_null($returnValue)) {
908 $n = floor(array_pop($aArgs));
910 if ((is_numeric(
$n)) && (!is_string(
$n))) {
912 foreach ($aArgs as $arg) {
914 if ((is_numeric($arg)) && (!is_string($arg))) {
920 if ((
$n < 0) || (
$n >= $count) || ($count == 0)) {
921 return self::$_errorCodes[
'num'];
926 return self::$_errorCodes[
'value'];
948 $entry = array_pop($aArgs);
950 if ((is_numeric($entry)) && (!is_string($entry))) {
951 if (($entry < 0) || ($entry > 1)) {
952 return self::$_errorCodes[
'num'];
955 foreach ($aArgs as $arg) {
957 if ((is_numeric($arg)) && (!is_string($arg))) {
961 $mValueCount = count($mArgs);
962 if ($mValueCount > 0) {
965 $index = $entry * ($count-1);
966 $iBase = floor($index);
967 if ($index == $iBase) {
968 return $mArgs[$index];
971 $iProportion = $index - $iBase;
972 return $mArgs[$iBase] + (($mArgs[$iNext] - $mArgs[$iBase]) * $iProportion) ;
976 return self::$_errorCodes[
'value'];
998 $entry = floor(array_pop($aArgs));
1000 if ((is_numeric($entry)) && (!is_string($entry))) {
1002 if (($entry < 0) || ($entry > 1)) {
1003 return self::$_errorCodes[
'num'];
1007 return self::$_errorCodes[
'value'];
1030 foreach ($aArgs as $arg) {
1031 if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
1035 if ((is_numeric($arg)) && (!is_string($arg))) {
1041 return $returnValue;
1064 foreach ($aArgs as $arg) {
1066 if ((is_null($arg)) || ((is_string($arg)) && ($arg ==
''))) {
1072 return $returnValue;
1095 foreach ($aArgs as $arg) {
1097 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg !=
'')))) {
1103 return $returnValue;
1121 public static function COUNTIF($aArgs,$condition) {
1126 if (!in_array($condition{0},array(
'>',
'<',
'='))) {
1128 $condition =
'='.$condition;
1131 foreach ($aArgs as $arg) {
1133 $testCondition =
'='.$arg.$condition;
1141 return $returnValue;
1159 public static function SUMIF($aArgs,$condition,$sumArgs = array()) {
1165 if (count($sumArgs) == 0) {
1168 if (!in_array($condition{0},array(
'>',
'<',
'='))) {
1170 $condition =
'='.$condition;
1173 foreach ($aArgs as
$key => $arg) {
1175 $testCondition =
'='.$arg.$condition;
1178 $returnValue += $sumArgs[
$key];
1183 return $returnValue;
1207 foreach ($aArgs as $arg) {
1208 if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
1209 $arg = (integer) $arg;
1212 if ((is_numeric($arg)) && (!is_string($arg))) {
1213 if (is_null($returnValue)) {
1214 $returnValue = $arg;
1216 $returnValue += $arg;
1224 return $returnValue / $aCount;
1226 return self::$_errorCodes[
'divisionbyzero'];
1246 $returnValue = null;
1251 foreach ($aArgs as $arg) {
1252 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) && ($arg !=
'')))) {
1253 if (is_bool($arg)) {
1254 $arg = (integer) $arg;
1255 }
elseif (is_string($arg)) {
1258 if (is_null($returnValue)) {
1259 $returnValue = $arg;
1261 $returnValue += $arg;
1269 return $returnValue / $aCount;
1271 return self::$_errorCodes[
'divisionbyzero'];
1291 $returnValue = self::$_errorCodes[
'num'];
1296 foreach ($aArgs as $arg) {
1298 if ((is_numeric($arg)) && (!is_string($arg))) {
1303 $mValueCount = count($mArgs);
1304 if ($mValueCount > 0) {
1305 sort($mArgs,SORT_NUMERIC);
1306 $mValueCount = $mValueCount / 2;
1307 if ($mValueCount == floor($mValueCount)) {
1308 $returnValue = ($mArgs[$mValueCount--] + $mArgs[$mValueCount]) / 2;
1310 $mValueCount == floor($mValueCount);
1311 $returnValue = $mArgs[$mValueCount];
1316 return $returnValue;
1325 $frequencyArray = array();
1326 foreach(
$data as $datum) {
1328 foreach($frequencyArray as
$key => $value) {
1329 if ((
string) $value[
'value'] == (
string) $datum) {
1330 ++$frequencyArray[
$key][
'frequency'];
1336 $frequencyArray[] = array(
'value' => $datum,
1341 foreach($frequencyArray as
$key => $value) {
1342 $frequencyList[
$key] = $value[
'frequency'];
1343 $valueList[
$key] = $value[
'value'];
1345 array_multisort($frequencyList, SORT_DESC, $valueList, SORT_ASC, SORT_NUMERIC, $frequencyArray);
1347 if ($frequencyArray[0][
'frequency'] == 1) {
1350 return $frequencyArray[0][
'value'];
1375 foreach ($aArgs as $arg) {
1377 if ((is_numeric($arg)) && (!is_string($arg))) {
1382 if (count($mArgs) > 0) {
1387 return $returnValue;
1406 $returnValue = null;
1409 if (!is_null($aMean)) {
1413 foreach ($aArgs as $arg) {
1415 if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
1418 if ((is_numeric($arg)) && (!is_string($arg))) {
1419 if (is_null($returnValue)) {
1420 $returnValue = pow(($arg - $aMean),2);
1422 $returnValue += pow(($arg - $aMean),2);
1429 if (is_null($returnValue)) {
1430 return self::$_errorCodes[
'num'];
1432 return $returnValue;
1457 $returnValue = null;
1460 if ($aMean != self::$_errorCodes[
'divisionbyzero']) {
1462 foreach ($aArgs as $arg) {
1463 if ((is_bool($arg)) && (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE)) {
1464 $arg = (integer) $arg;
1467 if ((is_numeric($arg)) && (!is_string($arg))) {
1468 if (is_null($returnValue)) {
1469 $returnValue = abs($arg - $aMean);
1471 $returnValue += abs($arg - $aMean);
1478 return $returnValue / $aCount ;
1480 return self::$_errorCodes[
'num'];
1501 if (is_numeric($aMean) && ($aMean > 0)) {
1504 if (self::MIN($aArgs) > 0) {
1505 return pow($aMean, (1 / $aCount));
1508 return self::$_errorCodes[
'num'];
1532 if (self::MIN($aArgs) < 0) {
1533 return self::$_errorCodes[
'num'];
1536 foreach ($aArgs as $arg) {
1538 if ((is_numeric($arg)) && (!is_string($arg))) {
1540 return self::$_errorCodes[
'num'];
1542 if (is_null($returnValue)) {
1543 $returnValue = (1 / $arg);
1545 $returnValue += (1 / $arg);
1553 return 1 / ($returnValue / $aCount);
1555 return $returnValue;
1580 $percent = array_pop($aArgs);
1582 if ((is_numeric($percent)) && (!is_string($percent))) {
1583 if (($percent < 0) || ($percent > 1)) {
1584 return self::$_errorCodes[
'num'];
1587 foreach ($aArgs as $arg) {
1589 if ((is_numeric($arg)) && (!is_string($arg))) {
1593 $discard = floor(self::COUNT($mArgs) * $percent / 2);
1595 for ($i=0; $i < $discard; ++$i) {
1597 array_shift($mArgs);
1601 return self::$_errorCodes[
'value'];
1621 $returnValue = null;
1624 if (!is_null($aMean)) {
1628 foreach ($aArgs as $arg) {
1630 if ((is_numeric($arg)) && (!is_string($arg))) {
1631 if (is_null($returnValue)) {
1632 $returnValue = pow(($arg - $aMean),2);
1634 $returnValue += pow(($arg - $aMean),2);
1641 if (($aCount > 0) && ($returnValue > 0)) {
1642 return sqrt($returnValue / $aCount);
1645 return self::$_errorCodes[
'divisionbyzero'];
1664 $returnValue = null;
1667 if (!is_null($aMean)) {
1671 foreach ($aArgs as $arg) {
1673 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg !=
'')))) {
1674 if (is_bool($arg)) {
1675 $arg = (integer) $arg;
1676 }
elseif (is_string($arg)) {
1679 if (is_null($returnValue)) {
1680 $returnValue = pow(($arg - $aMean),2);
1682 $returnValue += pow(($arg - $aMean),2);
1689 if (($aCount > 0) && ($returnValue > 0)) {
1690 return sqrt($returnValue / $aCount);
1693 return self::$_errorCodes[
'divisionbyzero'];
1712 $returnValue = null;
1715 if (!is_null($aMean)) {
1719 foreach ($aArgs as $arg) {
1721 if ((is_numeric($arg)) && (!is_string($arg))) {
1722 if (is_null($returnValue)) {
1723 $returnValue = pow(($arg - $aMean),2);
1725 $returnValue += pow(($arg - $aMean),2);
1732 if (($aCount > 0) && ($returnValue > 0)) {
1733 return sqrt($returnValue / $aCount);
1736 return self::$_errorCodes[
'divisionbyzero'];
1755 $returnValue = null;
1758 if (!is_null($aMean)) {
1762 foreach ($aArgs as $arg) {
1764 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg !=
'')))) {
1765 if (is_bool($arg)) {
1766 $arg = (integer) $arg;
1767 }
elseif (is_string($arg)) {
1770 if (is_null($returnValue)) {
1771 $returnValue = pow(($arg - $aMean),2);
1773 $returnValue += pow(($arg - $aMean),2);
1780 if (($aCount > 0) && ($returnValue > 0)) {
1781 return sqrt($returnValue / $aCount);
1784 return self::$_errorCodes[
'divisionbyzero'];
1803 $returnValue = self::$_errorCodes[
'divisionbyzero'];
1805 $summerA = $summerB = 0;
1810 foreach ($aArgs as $arg) {
1812 if ((is_numeric($arg)) && (!is_string($arg))) {
1813 $summerA += ($arg * $arg);
1821 $summerA = $summerA * $aCount;
1822 $summerB = ($summerB * $summerB);
1823 $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1));
1825 return $returnValue;
1844 $returnValue = self::$_errorCodes[
'divisionbyzero'];
1846 $summerA = $summerB = 0;
1851 foreach ($aArgs as $arg) {
1853 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg !=
'')))) {
1854 if (is_bool($arg)) {
1855 $arg = (integer) $arg;
1856 }
elseif (is_string($arg)) {
1859 $summerA += ($arg * $arg);
1867 $summerA = $summerA * $aCount;
1868 $summerB = ($summerB * $summerB);
1869 $returnValue = ($summerA - $summerB) / ($aCount * ($aCount - 1));
1871 return $returnValue;
1890 $returnValue = self::$_errorCodes[
'divisionbyzero'];
1892 $summerA = $summerB = 0;
1897 foreach ($aArgs as $arg) {
1899 if ((is_numeric($arg)) && (!is_string($arg))) {
1900 $summerA += ($arg * $arg);
1908 $summerA = $summerA * $aCount;
1909 $summerB = ($summerB * $summerB);
1910 $returnValue = ($summerA - $summerB) / ($aCount * $aCount);
1912 return $returnValue;
1931 $returnValue = self::$_errorCodes[
'divisionbyzero'];
1933 $summerA = $summerB = 0;
1938 foreach ($aArgs as $arg) {
1940 if ((is_numeric($arg)) || (is_bool($arg)) || ((is_string($arg) & ($arg !=
'')))) {
1941 if (is_bool($arg)) {
1942 $arg = (integer) $arg;
1943 }
elseif (is_string($arg)) {
1946 $summerA += ($arg * $arg);
1954 $summerA = $summerA * $aCount;
1955 $summerB = ($summerB * $summerB);
1956 $returnValue = ($summerA - $summerB) / ($aCount * $aCount);
1958 return $returnValue;
1972 public static function RANK($value,$valueSet,$order=0) {
1977 foreach($valueSet as
$key => $valueEntry) {
1978 if (!is_numeric($valueEntry)) {
1979 unset($valueSet[
$key]);
1984 rsort($valueSet,SORT_NUMERIC);
1986 sort($valueSet,SORT_NUMERIC);
1988 $pos = array_search($value,$valueSet);
1989 if ($pos === False) {
1990 return self::$_errorCodes[
'na'];
2007 public static function PERCENTRANK($valueSet,$value,$significance=3) {
2012 foreach($valueSet as
$key => $valueEntry) {
2013 if (!is_numeric($valueEntry)) {
2014 unset($valueSet[
$key]);
2017 sort($valueSet,SORT_NUMERIC);
2018 $valueCount = count($valueSet);
2019 if ($valueCount == 0) {
2020 return self::$_errorCodes[
'num'];
2023 $valueAdjustor = $valueCount - 1;
2024 if (($value < $valueSet[0]) || ($value > $valueSet[$valueAdjustor])) {
2025 return self::$_errorCodes[
'na'];
2028 $pos = array_search($value,$valueSet);
2029 if ($pos === False) {
2031 $testValue = $valueSet[0];
2032 while ($testValue < $value) {
2033 $testValue = $valueSet[++$pos];
2036 $pos += (($value - $valueSet[$pos]) / ($testValue - $valueSet[$pos]));
2039 return round($pos / $valueAdjustor,$significance);
2044 foreach($values as
$key => $value) {
2045 if ((is_bool($value)) || ((is_string($value)) && (trim($value) ==
''))) {
2046 unset($values[
$key]);
2047 }
elseif (is_string($value)) {
2048 if (is_numeric($value)) {
2049 $values[
$key] = (float) $value;
2051 unset($values[
$key]);
2073 $yValueCount = count($yValues);
2075 $xValueCount = count($xValues);
2077 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2078 return self::$_errorCodes[
'na'];
2079 }
elseif ($yValueCount == 1) {
2080 return self::$_errorCodes[
'divisionbyzero'];
2084 return $bestFitLinear->getIntersect();
2097 public static function RSQ($yValues,$xValues) {
2102 $yValueCount = count($yValues);
2104 $xValueCount = count($xValues);
2106 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2107 return self::$_errorCodes[
'na'];
2108 }
elseif ($yValueCount == 1) {
2109 return self::$_errorCodes[
'divisionbyzero'];
2113 return $bestFitLinear->getGoodnessOfFit();
2126 public static function SLOPE($yValues,$xValues) {
2131 $yValueCount = count($yValues);
2133 $xValueCount = count($xValues);
2135 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2136 return self::$_errorCodes[
'na'];
2137 }
elseif ($yValueCount == 1) {
2138 return self::$_errorCodes[
'divisionbyzero'];
2142 return $bestFitLinear->getSlope();
2155 public static function STEYX($yValues,$xValues) {
2160 $yValueCount = count($yValues);
2162 $xValueCount = count($xValues);
2164 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2165 return self::$_errorCodes[
'na'];
2166 }
elseif ($yValueCount == 1) {
2167 return self::$_errorCodes[
'divisionbyzero'];
2171 return $bestFitLinear->getStdevOfResiduals();
2184 public static function COVAR($yValues,$xValues) {
2189 $yValueCount = count($yValues);
2191 $xValueCount = count($xValues);
2193 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2194 return self::$_errorCodes[
'na'];
2195 }
elseif ($yValueCount == 1) {
2196 return self::$_errorCodes[
'divisionbyzero'];
2200 return $bestFitLinear->getCovariance();
2213 public static function CORREL($yValues,$xValues) {
2218 $yValueCount = count($yValues);
2220 $xValueCount = count($xValues);
2222 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2223 return self::$_errorCodes[
'na'];
2224 }
elseif ($yValueCount == 1) {
2225 return self::$_errorCodes[
'divisionbyzero'];
2229 return $bestFitLinear->getCorrelation();
2245 public static function LINEST($yValues,$xValues,$const=True,$stats=False) {
2248 $const = (boolean) self::flattenSingleValue($const);
2249 $stats = (boolean) self::flattenSingleValue($stats);
2252 $yValueCount = count($yValues);
2254 $xValueCount = count($xValues);
2256 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2257 return self::$_errorCodes[
'na'];
2258 }
elseif ($yValueCount == 1) {
2259 return self::$_errorCodes[
'divisionbyzero'];
2264 return array( array( $bestFitLinear->getSlope(),
2265 $bestFitLinear->getSlopeSE(),
2266 $bestFitLinear->getGoodnessOfFit(),
2267 $bestFitLinear->getF(),
2268 $bestFitLinear->getSSRegression(),
2270 array( $bestFitLinear->getIntersect(),
2271 $bestFitLinear->getIntersectSE(),
2272 $bestFitLinear->getStdevOfResiduals(),
2273 $bestFitLinear->getDFResiduals(),
2274 $bestFitLinear->getSSResiduals()
2278 return array( $bestFitLinear->getSlope(),
2279 $bestFitLinear->getIntersect()
2297 public static function LOGEST($yValues,$xValues,$const=True,$stats=False) {
2300 $const = (boolean) self::flattenSingleValue($const);
2301 $stats = (boolean) self::flattenSingleValue($stats);
2304 $yValueCount = count($yValues);
2306 $xValueCount = count($xValues);
2308 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2309 return self::$_errorCodes[
'na'];
2310 }
elseif ($yValueCount == 1) {
2311 return self::$_errorCodes[
'divisionbyzero'];
2316 return array( array( $bestFitExponential->getSlope(),
2317 $bestFitExponential->getSlopeSE(),
2318 $bestFitExponential->getGoodnessOfFit(),
2319 $bestFitExponential->getF(),
2320 $bestFitExponential->getSSRegression(),
2322 array( $bestFitExponential->getIntersect(),
2323 $bestFitExponential->getIntersectSE(),
2324 $bestFitExponential->getStdevOfResiduals(),
2325 $bestFitExponential->getDFResiduals(),
2326 $bestFitExponential->getSSResiduals()
2330 return array( $bestFitExponential->getSlope(),
2331 $bestFitExponential->getIntersect()
2347 public static function FORECAST($xValue,$yValues,$xValues) {
2352 if (!is_numeric($xValue)) {
2353 return self::$_errorCodes[
'value'];
2357 $yValueCount = count($yValues);
2359 $xValueCount = count($xValues);
2361 if (($yValueCount == 0) || ($yValueCount != $xValueCount)) {
2362 return self::$_errorCodes[
'na'];
2363 }
elseif ($yValueCount == 1) {
2364 return self::$_errorCodes[
'divisionbyzero'];
2368 return $bestFitLinear->getValueOfYForX($xValue);
2383 public static function TREND($yValues,$xValues=array(),$newValues=array(),$const=True) {
2387 $const = (boolean) self::flattenSingleValue($const);
2390 if (count($newValues) == 0) {
2391 $newValues = $bestFitLinear->getXValues();
2394 $returnArray = array();
2395 foreach($newValues as $xValue) {
2396 $returnArray[0][] = $bestFitLinear->getValueOfYForX($xValue);
2399 return $returnArray;
2414 public static function GROWTH($yValues,$xValues=array(),$newValues=array(),$const=True) {
2418 $const = (boolean) self::flattenSingleValue($const);
2421 if (count($newValues) == 0) {
2422 $newValues = $bestFitExponential->getXValues();
2425 $returnArray = array();
2426 foreach($newValues as $xValue) {
2427 $returnArray[0][] = $bestFitExponential->getValueOfYForX($xValue);
2430 return $returnArray;
2435 return ($num - ($num %
$n ) ) /
$n;
2439 public static function ROMAN($aValue, $style=0) {
2440 $aValue = (integer) self::flattenSingleValue($aValue);
2441 if ((!is_numeric($aValue)) || ($aValue < 0) || ($aValue >= 4000)) {
2442 return self::$_errorCodes[
'value'];
2448 $mill = Array(
'',
'M',
'MM',
'MMM',
'MMMM',
'MMMMM');
2449 $cent = Array(
'',
'C',
'CC',
'CCC',
'CD',
'D',
'DC',
'DCC',
'DCCC',
'CM');
2450 $tens = Array(
'',
'X',
'XX',
'XXX',
'XL',
'L',
'LX',
'LXX',
'LXXX',
'XC');
2451 $ones = Array(
'',
'I',
'II',
'III',
'IV',
'V',
'VI',
'VII',
'VIII',
'IX');
2454 while ($aValue > 5999) {
2462 return $roman.$mill[$m].$cent[$c].$tens[
$t].$ones[$aValue];
2480 $subtotal = array_shift($aArgs);
2482 if ((is_numeric($subtotal)) && (!is_string($subtotal))) {
2519 return self::$_errorCodes[
'value'];
2534 if (is_numeric($number)) {
2536 return self::$_errorCodes[
'num'];
2538 return sqrt($number * pi()) ;
2540 return self::$_errorCodes[
'value'];
2552 public static function FACT($factVal) {
2555 if (is_numeric($factVal)) {
2557 return self::$_errorCodes[
'num'];
2559 $factLoop = floor($factVal);
2560 if (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC) {
2561 if ($factVal > $factLoop) {
2562 return self::$_errorCodes[
'num'];
2566 while ($factLoop > 1) {
2567 $factorial *= $factLoop--;
2571 return self::$_errorCodes[
'value'];
2584 $factLoop = floor(self::flattenSingleValue($factVal));
2586 if (is_numeric($factLoop)) {
2588 return self::$_errorCodes[
'num'];
2591 while ($factLoop > 1) {
2592 $factorial *= $factLoop--;
2597 return self::$_errorCodes[
'value'];
2614 foreach ($aArgs as $arg) {
2616 if (is_numeric($arg)) {
2618 return self::$_errorCodes[
'num'];
2620 $summer += floor($arg);
2623 return self::$_errorCodes[
'value'];
2630 return $summer / $divisor;
2645 public static function CEILING($number,$significance=null) {
2649 if ((is_null($significance)) && (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC)) {
2650 $significance = $number/abs($number);
2653 if ((is_numeric($number)) && (is_numeric($significance))) {
2654 if (self::SIGN($number) == self::SIGN($significance)) {
2655 if ($significance == 0.0) {
2658 return ceil($number / $significance) * $significance;
2660 return self::$_errorCodes[
'num'];
2663 return self::$_errorCodes[
'value'];
2675 public static function EVEN($number) {
2678 if (is_numeric($number)) {
2682 return self::$_errorCodes[
'value'];
2694 public static function ODD($number) {
2697 if (is_numeric($number)) {
2699 if ($significance == 0) {
2708 return self::$_errorCodes[
'value'];
2723 if (is_numeric($number)) {
2724 return (
int) floor($number);
2726 return self::$_errorCodes[
'value'];
2743 if ((is_numeric($number)) && (is_numeric($digits))) {
2744 $significance = pow(10,$digits);
2745 if ($number < 0.0) {
2746 return floor($number * $significance) / $significance;
2748 return ceil($number * $significance) / $significance;
2751 return self::$_errorCodes[
'value'];
2768 if ((is_numeric($number)) && (is_numeric($digits))) {
2769 $significance = pow(10,$digits);
2770 if ($number < 0.0) {
2771 return ceil($number * $significance) / $significance;
2773 return floor($number * $significance) / $significance;
2776 return self::$_errorCodes[
'value'];
2789 public static function MROUND($number,$multiple) {
2793 if ((is_numeric($number)) && (is_numeric($multiple))) {
2794 if ($multiple == 0) {
2797 if ((self::SIGN($number)) == (self::SIGN($multiple))) {
2798 $multiplier = 1 / $multiple;
2799 return round($number * $multiplier) / $multiplier;
2801 return self::$_errorCodes[
'num'];
2803 return self::$_errorCodes[
'value'];
2816 public static function SIGN($number) {
2819 if (is_numeric($number)) {
2820 if ($number == 0.0) {
2823 return $number / abs($number);
2825 return self::$_errorCodes[
'value'];
2838 public static function FLOOR($number,$significance=null) {
2842 if ((is_null($significance)) && (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC)) {
2843 $significance = $number/abs($number);
2846 if ((is_numeric($number)) && (is_numeric($significance))) {
2847 if ((
float) $significance == 0.0) {
2848 return self::$_errorCodes[
'divisionbyzero'];
2850 if (self::SIGN($number) == self::SIGN($significance)) {
2851 return floor($number / $significance) * $significance;
2853 return self::$_errorCodes[
'num'];
2856 return self::$_errorCodes[
'value'];
2873 public static function PERMUT($numObjs,$numInSet) {
2877 if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
2878 if ($numObjs < $numInSet) {
2879 return self::$_errorCodes[
'num'];
2881 return round(self::FACT($numObjs) / self::FACT($numObjs - $numInSet));
2883 return self::$_errorCodes[
'value'];
2897 public static function COMBIN($numObjs,$numInSet) {
2901 if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
2902 if ($numObjs < $numInSet) {
2903 return self::$_errorCodes[
'num'];
2904 }
elseif ($numInSet < 0) {
2905 return self::$_errorCodes[
'num'];
2907 return round(self::FACT($numObjs) / self::FACT($numObjs - $numInSet)) /
self::FACT($numInSet);
2909 return self::$_errorCodes[
'value'];
2931 $x = array_shift($aArgs);
2932 $n = array_shift($aArgs);
2933 $m = array_shift($aArgs);
2935 if ((is_numeric(
$x)) && (is_numeric(
$n)) && (is_numeric($m))) {
2938 foreach($aArgs as $arg) {
2940 if ((is_numeric($arg)) && (!is_string($arg))) {
2941 $returnValue += $arg * pow(
$x,
$n + ($m * $i++));
2943 return self::$_errorCodes[
'value'];
2947 return $returnValue;
2949 return self::$_errorCodes[
'value'];
2968 if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
2970 return self::$_errorCodes[
'num'];
2972 return ($value - $mean) / $stdDev ;
2974 return self::$_errorCodes[
'value'];
2982 $startVal = floor(sqrt($value));
2984 $factorArray = array();
2985 for ($i = $startVal; $i > 1; --$i) {
2986 if (($value % $i) == 0) {
2987 $factorArray = array_merge($factorArray,self::_factors($value / $i));
2988 $factorArray = array_merge($factorArray,self::_factors($i));
2989 if ($i <= sqrt($value)) {
2994 if (count($factorArray) > 0) {
2995 rsort($factorArray);
2996 return $factorArray;
2998 return array((integer) $value);
3011 public static function LCM() {
3015 $allPoweredFactors = array();
3016 foreach($aArgs as $value) {
3017 if (!is_numeric($value)) {
3018 return self::$_errorCodes[
'value'];
3023 return self::$_errorCodes[
'num'];
3026 $myCountedFactors = array_count_values($myFactors);
3027 $myPoweredFactors = array();
3028 foreach($myCountedFactors as $myCountedFactor => $myCountedPower) {
3029 $myPoweredFactors[$myCountedFactor] = pow($myCountedFactor,$myCountedPower);
3031 foreach($myPoweredFactors as $myPoweredValue => $myPoweredFactor) {
3032 if (array_key_exists($myPoweredValue,$allPoweredFactors)) {
3033 if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) {
3034 $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
3037 $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
3041 foreach($allPoweredFactors as $allPoweredFactor) {
3042 $returnValue *= (integer) $allPoweredFactor;
3044 return $returnValue;
3056 public static function GCD() {
3060 $allPoweredFactors = array();
3061 foreach($aArgs as $value) {
3066 $myCountedFactors = array_count_values($myFactors);
3067 $allValuesFactors[] = $myCountedFactors;
3069 $allValuesCount = count($allValuesFactors);
3070 $mergedArray = $allValuesFactors[0];
3071 for ($i=1;$i < $allValuesCount; ++$i) {
3072 $mergedArray = array_intersect_key($mergedArray,$allValuesFactors[$i]);
3074 $mergedArrayValues = count($mergedArray);
3075 if ($mergedArrayValues == 0) {
3076 return $returnValue;
3077 }
elseif ($mergedArrayValues > 1) {
3078 foreach($mergedArray as $mergedKey => $mergedValue) {
3079 foreach($allValuesFactors as $highestPowerTest) {
3080 foreach($highestPowerTest as $testKey => $testValue) {
3081 if (($testKey == $mergedKey) && ($testValue < $mergedValue)) {
3082 $mergedArray[$mergedKey] = $testValue;
3083 $mergedValue = $testValue;
3090 foreach($mergedArray as
$key => $value) {
3091 $returnValue *= pow(
$key,$value);
3093 return $returnValue;
3095 $keys = array_keys($mergedArray);
3097 $value = $mergedArray[
$key];
3098 foreach($allValuesFactors as $testValue) {
3099 foreach($testValue as $mergedKey => $mergedValue) {
3100 if (($mergedKey ==
$key) && ($mergedValue < $value)) {
3101 $value = $mergedValue;
3105 return pow(
$key,$value);
3128 public static function BINOMDIST($value, $trials, $probability, $cumulative) {
3129 $value = floor(self::flattenSingleValue($value));
3130 $trials = floor(self::flattenSingleValue($trials));
3133 if ((is_numeric($value)) && (is_numeric($trials)) && (is_numeric($probability))) {
3134 if (($value < 0) || ($value > $trials)) {
3135 return self::$_errorCodes[
'num'];
3137 if (($probability < 0) || ($probability > 1)) {
3138 return self::$_errorCodes[
'num'];
3140 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
3143 for ($i = 0; $i <= $value; ++$i) {
3144 $summer +=
self::COMBIN($trials,$i) * pow($probability,$i) * pow(1 - $probability,$trials - $i);
3148 return self::COMBIN($trials,$value) * pow($probability,$value) * pow(1 - $probability,$trials - $value) ;
3152 return self::$_errorCodes[
'value'];
3172 $failures = floor(self::flattenSingleValue($failures));
3173 $successes = floor(self::flattenSingleValue($successes));
3176 if ((is_numeric($failures)) && (is_numeric($successes)) && (is_numeric($probability))) {
3177 if (($failures < 0) || ($successes < 1)) {
3178 return self::$_errorCodes[
'num'];
3180 if (($probability < 0) || ($probability > 1)) {
3181 return self::$_errorCodes[
'num'];
3183 if (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC) {
3184 if (($failures + $successes - 1) <= 0) {
3185 return self::$_errorCodes[
'num'];
3188 return (self::COMBIN($failures + $successes - 1,$successes - 1)) * (pow($probability,$successes)) * (pow(1 - $probability,$failures)) ;
3190 return self::$_errorCodes[
'value'];
3213 public static function CRITBINOM($trials, $probability, $alpha) {
3214 $trials = floor(self::flattenSingleValue($trials));
3218 if ((is_numeric($trials)) && (is_numeric($probability)) && (is_numeric($alpha))) {
3220 return self::$_errorCodes[
'num'];
3222 if (($probability < 0) || ($probability > 1)) {
3223 return self::$_errorCodes[
'num'];
3225 if (($alpha < 0) || ($alpha > 1)) {
3226 return self::$_errorCodes[
'num'];
3228 if ($alpha <= 0.5) {
3229 $t = sqrt(log(1 / pow($alpha,2)));
3230 $trialsApprox = 0 - (
$t + (2.515517 + 0.802853 *
$t + 0.010328 *
$t *
$t) / (1 + 1.432788 *
$t + 0.189269 *
$t *
$t + 0.001308 *
$t *
$t *
$t));
3232 $t = sqrt(log(1 / pow(1 - $alpha,2)));
3233 $trialsApprox =
$t - (2.515517 + 0.802853 *
$t + 0.010328 *
$t *
$t) / (1 + 1.432788 *
$t + 0.189269 *
$t *
$t + 0.001308 *
$t *
$t *
$t);
3235 $Guess = floor($trials * $probability + $trialsApprox * sqrt($trials * $probability * (1 - $probability)));
3238 }
elseif ($Guess > $trials) {
3242 $TotalUnscaledProbability = $UnscaledPGuess = $UnscaledCumPGuess = 0.0;
3243 $EssentiallyZero = 10e-12;
3245 $m = floor($trials * $probability);
3246 ++$TotalUnscaledProbability;
3247 if ($m == $Guess) { ++$UnscaledPGuess; }
3248 if ($m <= $Guess) { ++$UnscaledCumPGuess; }
3253 while ((!$Done) && ($k <= $trials)) {
3254 $CurrentValue = $PreviousValue * ($trials - $k + 1) * $probability / ($k * (1 - $probability));
3255 $TotalUnscaledProbability += $CurrentValue;
3256 if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; }
3257 if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; }
3258 if ($CurrentValue <= $EssentiallyZero) { $Done = True; }
3259 $PreviousValue = $CurrentValue;
3266 while ((!$Done) && ($k >= 0)) {
3267 $CurrentValue = $PreviousValue * $k + 1 * (1 - $probability) / (($trials - $k) * $probability);
3268 $TotalUnscaledProbability += $CurrentValue;
3269 if ($k == $Guess) { $UnscaledPGuess += $CurrentValue; }
3270 if ($k <= $Guess) { $UnscaledCumPGuess += $CurrentValue; }
3271 if ($CurrentValue <= $EssentiallyZero) { $Done = True; }
3272 $PreviousValue = $CurrentValue;
3276 $PGuess = $UnscaledPGuess / $TotalUnscaledProbability;
3277 $CumPGuess = $UnscaledCumPGuess / $TotalUnscaledProbability;
3280 $CumPGuessMinus1 = $CumPGuess - 1;
3283 if (($CumPGuessMinus1 < $alpha) && ($CumPGuess >= $alpha)) {
3285 }
elseif (($CumPGuessMinus1 < $alpha) && ($CumPGuess < $alpha)) {
3286 $PGuessPlus1 = $PGuess * ($trials - $Guess) * $probability / $Guess / (1 - $probability);
3287 $CumPGuessMinus1 = $CumPGuess;
3288 $CumPGuess = $CumPGuess + $PGuessPlus1;
3289 $PGuess = $PGuessPlus1;
3291 }
elseif (($CumPGuessMinus1 >= $alpha) && ($CumPGuess >= $alpha)) {
3292 $PGuessMinus1 = $PGuess * $Guess * (1 - $probability) / ($trials - $Guess + 1) / $probability;
3293 $CumPGuess = $CumPGuessMinus1;
3294 $CumPGuessMinus1 = $CumPGuessMinus1 - $PGuess;
3295 $PGuess = $PGuessMinus1;
3300 return self::$_errorCodes[
'value'];
3313 public static function CHIDIST($value, $degrees) {
3315 $degrees = floor(self::flattenSingleValue($degrees));
3317 if ((is_numeric($value)) && (is_numeric($degrees))) {
3319 return self::$_errorCodes[
'num'];
3322 if (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC) {
3325 return self::$_errorCodes[
'num'];
3329 return self::$_errorCodes[
'value'];
3342 public static function CHIINV($probability, $degrees) {
3344 $degrees = floor(self::flattenSingleValue($degrees));
3346 if ((is_numeric($probability)) && (is_numeric($degrees))) {
3349 $maxIteration = 100;
3374 if (($xNew < $xLo) || ($xNew > $xHi) || (
$result == 0.0)) {
3375 $xNew = ($xLo + $xHi) / 2;
3381 return self::$_errorCodes[
'na'];
3383 return round(
$x,12);
3385 return self::$_errorCodes[
'value'];
3401 public static function EXPONDIST($value, $lambda, $cumulative) {
3406 if ((is_numeric($value)) && (is_numeric($lambda))) {
3407 if (($value < 0) || ($lambda < 0)) {
3408 return self::$_errorCodes[
'num'];
3410 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
3412 return 1 - exp(0-$value*$lambda);
3414 return $lambda * exp(0-$value*$lambda);
3418 return self::$_errorCodes[
'value'];
3435 if (is_numeric($value)) {
3436 if (($value <= -1) || ($value >= 1)) {
3437 return self::$_errorCodes[
'num'];
3439 return 0.5 * log((1+$value)/(1-$value));
3441 return self::$_errorCodes[
'value'];
3458 if (is_numeric($value)) {
3459 return (exp(2 * $value) - 1) / (exp(2 * $value) + 1);
3461 return self::$_errorCodes[
'value'];
3478 if ($p != self::$_logBetaCache_p ||
$q != self::$_logBetaCache_q) {
3479 self::$_logBetaCache_p = $p;
3480 self::$_logBetaCache_q =
$q;
3482 self::$_logBetaCache_result = 0.0;
3500 $p_minus = $p - 1.0;
3501 $h = 1.0 - $sum_pq *
$x / $p_plus;
3512 $d = $m * ($q - $m) *
$x / ( ($p_minus + $m2) * ($p + $m2));
3524 $d = -($p + $m) * ($sum_pq + $m) *
$x / (($p + $m2) * ($p_plus + $m2));
3591 static $lg_d1 = -0.5772156649015328605195174;
3592 static $lg_d2 = 0.4227843350984671393993777;
3593 static $lg_d4 = 1.791759469228055000094023;
3595 static $lg_p1 = array( 4.945235359296727046734888,
3596 201.8112620856775083915565,
3597 2290.838373831346393026739,
3598 11319.67205903380828685045,
3599 28557.24635671635335736389,
3600 38484.96228443793359990269,
3601 26377.48787624195437963534,
3602 7225.813979700288197698961 );
3603 static $lg_p2 = array( 4.974607845568932035012064,
3604 542.4138599891070494101986,
3605 15506.93864978364947665077,
3606 184793.2904445632425417223,
3607 1088204.76946882876749847,
3608 3338152.967987029735917223,
3609 5106661.678927352456275255,
3610 3074109.054850539556250927 );
3611 static $lg_p4 = array( 14745.02166059939948905062,
3612 2426813.369486704502836312,
3613 121475557.4045093227939592,
3614 2663432449.630976949898078,
3615 29403789566.34553899906876,
3616 170266573776.5398868392998,
3617 492612579337.743088758812,
3618 560625185622.3951465078242 );
3620 static $lg_q1 = array( 67.48212550303777196073036,
3621 1113.332393857199323513008,
3622 7738.757056935398733233834,
3623 27639.87074403340708898585,
3624 54993.10206226157329794414,
3625 61611.22180066002127833352,
3626 36351.27591501940507276287,
3627 8785.536302431013170870835 );
3628 static $lg_q2 = array( 183.0328399370592604055942,
3629 7765.049321445005871323047,
3630 133190.3827966074194402448,
3631 1136705.821321969608938755,
3632 5267964.117437946917577538,
3633 13467014.54311101692290052,
3634 17827365.30353274213975932,
3635 9533095.591844353613395747 );
3636 static $lg_q4 = array( 2690.530175870899333379843,
3637 639388.5654300092398984238,
3638 41355999.30241388052042842,
3639 1120872109.61614794137657,
3640 14886137286.78813811542398,
3641 101680358627.2438228077304,
3642 341747634550.7377132798597,
3643 446315818741.9713286462081 );
3645 static $lg_c = array( -0.001910444077728,
3647 -5.952379913043012e-4,
3648 7.93650793500350248e-4,
3649 -0.002777777777777681622553,
3650 0.08333333333333333331554247,
3654 static $lg_frtbig = 2.25e76;
3655 static $pnt68 = 0.6796875;
3658 if (
$x == self::$_logGammaCache_x) {
3676 if ($y <= 0.5 || $y >= $pnt68) {
3679 for ($i = 0; $i < 8; ++$i) {
3680 $xnum = $xnum * $xm1 + $lg_p1[$i];
3681 $xden = $xden * $xm1 + $lg_q1[$i];
3683 $res = $corr + $xm1 * ($lg_d1 + $xm1 * ($xnum / $xden));
3688 for ($i = 0; $i < 8; ++$i) {
3689 $xnum = $xnum * $xm2 + $lg_p2[$i];
3690 $xden = $xden * $xm2 + $lg_q2[$i];
3692 $res = $corr + $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden));
3701 for ($i = 0; $i < 8; ++$i) {
3702 $xnum = $xnum * $xm2 + $lg_p2[$i];
3703 $xden = $xden * $xm2 + $lg_q2[$i];
3705 $res = $xm2 * ($lg_d2 + $xm2 * ($xnum / $xden));
3713 for ($i = 0; $i < 8; ++$i) {
3714 $xnum = $xnum * $xm4 + $lg_p4[$i];
3715 $xden = $xden * $xm4 + $lg_q4[$i];
3717 $res = $lg_d4 + $xm4 * ($xnum / $xden);
3723 if (
$y <= $lg_frtbig) {
3726 for ($i = 0; $i < 6; ++$i)
3732 $res +=
$y * ($corr - 1.0);
3743 self::$_logGammaCache_x =
$x;
3744 self::$_logGammaCache_result =
$res;
3762 return exp(self::_logBeta($p,
$q));
3787 $beta_gam = exp((0 - self::_logBeta($p,
$q)) + $p * log(
$x) +
$q * log(1.0 -
$x));
3788 if (
$x < ($p + 1.0) / ($p +
$q + 2.0)) {
3808 public static function BETADIST($value,$alpha,$beta,$rMin=0,$rMax=1) {
3815 if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
3816 if (($value < $rMin) || ($value > $rMax) || ($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax)) {
3817 return self::$_errorCodes[
'num'];
3819 if ($rMin > $rMax) {
3825 $value /= ($rMax - $rMin);
3828 return self::$_errorCodes[
'value'];
3844 public static function BETAINV($probability,$alpha,$beta,$rMin=0,$rMax=1) {
3851 if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta)) && (is_numeric($rMin)) && (is_numeric($rMax))) {
3852 if (($alpha <= 0) || ($beta <= 0) || ($rMin == $rMax) || ($probability <= 0) || ($probability > 1)) {
3853 return self::$_errorCodes[
'num'];
3855 if ($rMin > $rMax) {
3862 $maxIteration = 100;
3866 $guess = ($a + $b) / 2;
3877 return self::$_errorCodes[
'na'];
3879 return round($rMin + $guess * ($rMax - $rMin),12);
3881 return self::$_errorCodes[
'value'];
3891 for (
$n=0;
$n<=$max; ++
$n) {
3893 for ($i=1; $i<=
$n; ++$i) {
3894 $divisor *= ($a + $i);
3896 $summer += (pow(
$x,
$n) / $divisor);
3898 return pow(
$x,$a) * exp(0-
$x) * $summer;
3906 if (
$data == 0.0)
return 0;
3908 static $p0 = 1.000000000190015;
3909 static $p = array ( 1 => 76.18009172947146,
3910 2 => -86.50532032941677,
3911 3 => 24.01409824083091,
3912 4 => -1.231739572450155,
3913 5 => 1.208650973866179e-3,
3914 6 => -5.395239384953e-6
3919 $tmp -= (
$x + 0.5) * log($tmp);
3922 for ($j=1;$j<=6;++$j) {
3923 $summer += ($p[$j] / ++
$y);
3925 return exp(0 - $tmp + log(2.5066282746310005 * $summer /
$x));
3941 public static function GAMMADIST($value,$a,$b,$cumulative) {
3946 if ((is_numeric($value)) && (is_numeric($a)) && (is_numeric($b))) {
3947 if (($value < 0) || ($a <= 0) || ($b <= 0)) {
3948 return self::$_errorCodes[
'num'];
3950 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
3954 return (1 / (pow($b,$a) * self::_gamma($a))) * pow($value,$a-1) * exp(0-($value / $b));
3958 return self::$_errorCodes[
'value'];
3974 public static function GAMMAINV($probability,$alpha,$beta) {
3981 if ((is_numeric($probability)) && (is_numeric($alpha)) && (is_numeric($beta))) {
3982 if (($alpha <= 0) || ($beta <= 0) || ($probability <= 0) || ($probability > 1)) {
3983 return self::$_errorCodes[
'num'];
3987 $maxIteration = 100;
4012 if (($xNew < $xLo) || ($xNew > $xHi) || (
$result == 0.0)) {
4013 $xNew = ($xLo + $xHi) / 2;
4019 return self::$_errorCodes[
'na'];
4021 return round(
$x,12);
4023 return self::$_errorCodes[
'value'];
4038 if (is_numeric($value)) {
4040 return self::$_errorCodes[
'num'];
4042 return log(self::_gamma($value));
4044 return self::$_errorCodes[
'value'];
4062 public static function NORMDIST($value, $mean, $stdDev, $cumulative) {
4067 if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
4069 return self::$_errorCodes[
'num'];
4071 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
4073 return 0.5 * (1 +
self::_erfVal(($value - $mean) / ($stdDev * sqrt(2))));
4075 return (1 / (
SQRT2PI * $stdDev)) * exp(0 - (pow($value - $mean,2) / (2 * pow($stdDev,2))));
4079 return self::$_errorCodes[
'value'];
4114 if ((is_numeric($value)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
4115 if (($value <= 0) || ($stdDev <= 0)) {
4116 return self::$_errorCodes[
'num'];
4120 return self::$_errorCodes[
'value'];
4146 static $a = array( 1 => -3.969683028665376e+01,
4147 2 => 2.209460984245205e+02,
4148 3 => -2.759285104469687e+02,
4149 4 => 1.383577518672690e+02,
4150 5 => -3.066479806614716e+01,
4151 6 => 2.506628277459239e+00
4154 static $b = array( 1 => -5.447609879822406e+01,
4155 2 => 1.615858368580409e+02,
4156 3 => -1.556989798598866e+02,
4157 4 => 6.680131188771972e+01,
4158 5 => -1.328068155288572e+01
4161 static $c = array( 1 => -7.784894002430293e-03,
4162 2 => -3.223964580411365e-01,
4163 3 => -2.400758277161838e+00,
4164 4 => -2.549732539343734e+00,
4165 5 => 4.374664141464968e+00,
4166 6 => 2.938163982698783e+00
4169 static $d = array( 1 => 7.784695709041462e-03,
4170 2 => 3.224671290700398e-01,
4171 3 => 2.445134137142996e+00,
4172 4 => 3.754408661907416e+00
4177 $p_high = 1 - $p_low;
4179 if (0 < $p && $p < $p_low) {
4181 $q = sqrt(-2 * log($p));
4182 return ((((($c[1] *
$q + $c[2]) *
$q + $c[3]) *
$q + $c[4]) *
$q + $c[5]) *
$q + $c[6]) /
4184 }
elseif ($p_low <= $p && $p <= $p_high) {
4188 return ((((($a[1] * $r + $a[2]) * $r + $a[3]) * $r + $a[4]) * $r + $a[5]) * $r + $a[6]) * $q /
4189 ((((($b[1] * $r + $b[2]) * $r + $b[3]) * $r + $b[4]) * $r + $b[5]) * $r + 1);
4190 }
elseif ($p_high < $p && $p < 1) {
4192 $q = sqrt(-2 * log(1 - $p));
4193 return -((((($c[1] *
$q + $c[2]) *
$q + $c[3]) *
$q + $c[4]) *
$q + $c[5]) *
$q + $c[6]) /
4197 return self::$_errorCodes[
'null'];
4205 $a1 = 2.50662823884;
4206 $a2 = -18.61500062529;
4207 $a3 = 41.39119773534;
4208 $a4 = -25.44106049637;
4210 $b1 = -8.4735109309;
4211 $b2 = 23.08336743743;
4212 $b3 = -21.06224101826;
4213 $b4 = 3.13082909833;
4215 $c1 = 0.337475482272615;
4216 $c2 = 0.976169019091719;
4217 $c3 = 0.160797971491821;
4218 $c4 = 2.76438810333863E-02;
4219 $c5 = 3.8405729373609E-03;
4220 $c6 = 3.951896511919E-04;
4221 $c7 = 3.21767881768E-05;
4222 $c8 = 2.888167364E-07;
4223 $c9 = 3.960315187E-07;
4226 if (abs(
$y) < 0.42) {
4228 $z =
$y * ((($a4 * $z + $a3) * $z + $a2) * $z + $a1) / (((($b4 * $z + $b3) * $z + $b2) * $z + $b1) * $z + 1);
4231 $z = log(-log(1 - $prob));
4233 $z = log(-log($prob));
4235 $z = $c1 + $z * ($c2 + $z * ($c3 + $z * ($c4 + $z * ($c5 + $z * ($c6 + $z * ($c7 + $z * ($c8 + $z * $c9)))))));
4257 $a0 = 3.3871328727963666080;
4258 $a1 = 1.3314166789178437745E+2;
4259 $a2 = 1.9715909503065514427E+3;
4260 $a3 = 1.3731693765509461125E+4;
4261 $a4 = 4.5921953931549871457E+4;
4262 $a5 = 6.7265770927008700853E+4;
4263 $a6 = 3.3430575583588128105E+4;
4264 $a7 = 2.5090809287301226727E+3;
4266 $b1 = 4.2313330701600911252E+1;
4267 $b2 = 6.8718700749205790830E+2;
4268 $b3 = 5.3941960214247511077E+3;
4269 $b4 = 2.1213794301586595867E+4;
4270 $b5 = 3.9307895800092710610E+4;
4271 $b6 = 2.8729085735721942674E+4;
4272 $b7 = 5.2264952788528545610E+3;
4275 $c0 = 1.42343711074968357734;
4276 $c1 = 4.63033784615654529590;
4277 $c2 = 5.76949722146069140550;
4278 $c3 = 3.64784832476320460504;
4279 $c4 = 1.27045825245236838258;
4280 $c5 = 2.41780725177450611770E-1;
4281 $c6 = 2.27238449892691845833E-2;
4282 $c7 = 7.74545014278341407640E-4;
4284 $d1 = 2.05319162663775882187;
4285 $d2 = 1.67638483018380384940;
4286 $d3 = 6.89767334985100004550E-1;
4287 $d4 = 1.48103976427480074590E-1;
4288 $d5 = 1.51986665636164571966E-2;
4289 $d6 = 5.47593808499534494600E-4;
4290 $d7 = 1.05075007164441684324E-9;
4293 $e0 = 6.65790464350110377720;
4294 $e1 = 5.46378491116411436990;
4295 $e2 = 1.78482653991729133580;
4296 $e3 = 2.96560571828504891230E-1;
4297 $e4 = 2.65321895265761230930E-2;
4298 $e5 = 1.24266094738807843860E-3;
4299 $e6 = 2.71155556874348757815E-5;
4300 $e7 = 2.01033439929228813265E-7;
4302 $f1 = 5.99832206555887937690E-1;
4303 $f2 = 1.36929880922735805310E-1;
4304 $f3 = 1.48753612908506148525E-2;
4305 $f4 = 7.86869131145613259100E-4;
4306 $f5 = 1.84631831751005468180E-5;
4307 $f6 = 1.42151175831644588870E-7;
4308 $f7 = 2.04426310338993978564E-15;
4313 if (abs(
$q) <= split1) {
4314 $R = $const1 -
$q *
$q;
4315 $z = $q * ((((((($a7 * $R + $a6) * $R + $a5) * $R + $a4) * $R + $a3) * $R + $a2) * $R + $a1) * $R + $a0) /
4316 ((((((($b7 * $R + $b6) * $R + $b5) * $R + $b4) * $R + $b3) * $R + $b2) * $R + $b1) * $R + 1);
4323 $R = pow(-log($R),2);
4326 If ($R <= $split2) {
4328 $z = ((((((($c7 * $R + $c6) * $R + $c5) * $R + $c4) * $R + $c3) * $R + $c2) * $R + $c1) * $R + $c0) /
4329 ((((((($d7 * $R + $d6) * $R + $d5) * $R + $d4) * $R + $d3) * $R + $d2) * $R + $d1) * $R + 1);
4333 $z = ((((((($e7 * $R + $e6) * $R + $e5) * $R + $e4) * $R + $e3) * $R + $e2) * $R + $e1) * $R + $e0) /
4334 ((((((($f7 * $R + $f6) * $R + $f5) * $R + $f4) * $R + $f3) * $R + $f2) * $R + $f1) * $R + 1);
4355 public static function NORMINV($probability,$mean,$stdDev) {
4360 if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
4361 if (($probability < 0) || ($probability > 1)) {
4362 return self::$_errorCodes[
'num'];
4365 return self::$_errorCodes[
'num'];
4367 return (self::_inverse_ncdf($probability) * $stdDev) + $mean;
4369 return self::$_errorCodes[
'value'];
4398 public static function LOGINV($probability, $mean, $stdDev) {
4403 if ((is_numeric($probability)) && (is_numeric($mean)) && (is_numeric($stdDev))) {
4404 if (($probability < 0) || ($probability > 1) || ($stdDev <= 0)) {
4405 return self::$_errorCodes[
'num'];
4407 return exp($mean + $stdDev * self::NORMSINV($probability));
4409 return self::$_errorCodes[
'value'];
4426 public static function HYPGEOMDIST($sampleSuccesses, $sampleNumber, $populationSuccesses, $populationNumber) {
4427 $sampleSuccesses = floor(self::flattenSingleValue($sampleSuccesses));
4428 $sampleNumber = floor(self::flattenSingleValue($sampleNumber));
4429 $populationSuccesses = floor(self::flattenSingleValue($populationSuccesses));
4430 $populationNumber = floor(self::flattenSingleValue($populationNumber));
4432 if ((is_numeric($sampleSuccesses)) && (is_numeric($sampleNumber)) && (is_numeric($populationSuccesses)) && (is_numeric($populationNumber))) {
4433 if (($sampleSuccesses < 0) || ($sampleSuccesses > $sampleNumber) || ($sampleSuccesses > $populationSuccesses)) {
4434 return self::$_errorCodes[
'num'];
4436 if (($sampleNumber <= 0) || ($sampleNumber > $populationNumber)) {
4437 return self::$_errorCodes[
'num'];
4439 if (($populationSuccesses <= 0) || ($populationSuccesses > $populationNumber)) {
4440 return self::$_errorCodes[
'num'];
4442 return self::COMBIN($populationSuccesses,$sampleSuccesses) *
4443 self::COMBIN($populationNumber - $populationSuccesses,$sampleNumber - $sampleSuccesses) /
4446 return self::$_errorCodes[
'value'];
4460 public static function TDIST($value, $degrees, $tails) {
4462 $degrees = floor(self::flattenSingleValue($degrees));
4463 $tails = floor(self::flattenSingleValue($tails));
4465 if ((is_numeric($value)) && (is_numeric($degrees)) && (is_numeric($tails))) {
4466 if (($value < 0) || ($degrees < 1) || ($tails < 1) || ($tails > 2)) {
4467 return self::$_errorCodes[
'num'];
4479 $ta = 0.636619772367581;
4481 $ttheta = atan2($value,sqrt($tterm));
4486 if (($degrees % 2) == 1) {
4495 while ($ti < $degrees) {
4496 $tterm *= $tc * $tc * ($ti - 1) / $ti;
4501 if (($degrees % 2) == 1) { $tsum = $ta * ($tsum + $ttheta); }
4502 $tValue = 0.5 * (1 + $tsum);
4504 return 1 - abs($tValue);
4506 return 1 - abs((1 - $tValue) - $tValue);
4509 return self::$_errorCodes[
'value'];
4522 public static function TINV($probability, $degrees) {
4524 $degrees = floor(self::flattenSingleValue($degrees));
4526 if ((is_numeric($probability)) && (is_numeric($degrees))) {
4529 $maxIteration = 100;
4554 if (($xNew < $xLo) || ($xNew > $xHi) || (
$result == 0.0)) {
4555 $xNew = ($xLo + $xHi) / 2;
4561 return self::$_errorCodes[
'na'];
4563 return round(
$x,12);
4565 return self::$_errorCodes[
'value'];
4583 $size = floor(self::flattenSingleValue(
$size));
4585 if ((is_numeric($alpha)) && (is_numeric($stdDev)) && (is_numeric(
$size))) {
4586 if (($alpha <= 0) || ($alpha >= 1)) {
4587 return self::$_errorCodes[
'num'];
4589 if (($stdDev <= 0) || (
$size < 1)) {
4590 return self::$_errorCodes[
'num'];
4594 return self::$_errorCodes[
'value'];
4611 public static function POISSON($value, $mean, $cumulative) {
4615 if ((is_numeric($value)) && (is_numeric($mean))) {
4616 if (($value <= 0) || ($mean <= 0)) {
4617 return self::$_errorCodes[
'num'];
4619 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
4622 for ($i = 0; $i <= floor($value); ++$i) {
4625 return exp(0-$mean) * $summer;
4627 return (exp(0-$mean) * pow($mean,$value)) /
self::FACT($value);
4631 return self::$_errorCodes[
'value'];
4648 public static function WEIBULL($value, $alpha, $beta, $cumulative) {
4653 if ((is_numeric($value)) && (is_numeric($alpha)) && (is_numeric($beta))) {
4654 if (($value < 0) || ($alpha <= 0) || ($beta <= 0)) {
4655 return self::$_errorCodes[
'num'];
4657 if ((is_numeric($cumulative)) || (is_bool($cumulative))) {
4659 return 1 - exp(0 - pow($value / $beta,$alpha));
4661 return ($alpha / pow($beta,$alpha)) * pow($value,$alpha - 1) * exp(0 - pow($value / $beta,$alpha));
4665 return self::$_errorCodes[
'value'];
4685 $count = $summer = 0;
4687 foreach ($aArgs as $arg) {
4689 if ((is_numeric($arg)) && (!is_string($arg))) {
4690 $summer += pow((($arg - $mean) / $stdDev),3) ;
4697 return $summer * ($count / (($count-1) * ($count-2)));
4699 return self::$_errorCodes[
'divisionbyzero'];
4720 $count = $summer = 0;
4722 foreach ($aArgs as $arg) {
4724 if ((is_numeric($arg)) && (!is_string($arg))) {
4725 $summer += pow((($arg - $mean) / $stdDev),4) ;
4732 return $summer * ($count * ($count+1) / (($count-1) * ($count-2) * ($count-3))) - (3 * pow($count-1,2) / (($count-2) * ($count-3)));
4735 return self::$_errorCodes[
'divisionbyzero'];
4746 public static function RAND($min = 0, $max = 0) {
4750 if ($min == 0 && $max == 0) {
4751 return (rand(0,10000000)) / 10000000;
4753 return rand($min, $max);
4765 public static function MOD($a = 1, $b = 1) {
4782 if (function_exists(
'mb_convert_encoding')) {
4783 return mb_convert_encoding(
'&#'.intval($character).
';',
'UTF-8',
'HTML-ENTITIES');
4785 return chr(intval($character));
4797 if (is_bool($characters)) {
4798 if (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE) {
4799 $characters = (int) $characters;
4802 $characters =
'True';
4804 $characters =
'False';
4809 if ((function_exists(
'mb_strlen')) && (function_exists(
'mb_substr'))) {
4810 if (mb_strlen($characters,
'UTF-8') > 0) {
4811 $character = mb_substr($characters, 0, 1,
'UTF-8');
4812 $byteLength = strlen($character);
4814 for ($i = 0; $i < $byteLength; ++$i) {
4815 $xValue = ($xValue * 256) + ord($character{$i});
4820 if (strlen($characters) > 0) {
4821 return ord(substr($characters, 0, 1));
4824 return self::$_errorCodes[
'value'];
4839 foreach ($aArgs as $arg) {
4840 if (is_bool($arg)) {
4841 if (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE) {
4851 $returnValue .= $arg;
4855 return $returnValue;
4869 if (function_exists(
'mb_strlen')) {
4870 return mb_strlen($value,
'UTF-8');
4872 return strlen($value);
4886 $needle = (string) self::flattenSingleValue($needle);
4887 $haystack = (string) self::flattenSingleValue($haystack);
4890 if (($offset > 0) && (strlen($haystack) > $offset)) {
4891 if (function_exists(
'mb_strpos')) {
4892 $pos = mb_strpos($haystack, $needle, --$offset,
'UTF-8');
4894 $pos = strpos($haystack, $needle, --$offset);
4896 if ($pos !==
false) {
4900 return self::$_errorCodes[
'value'];
4913 $needle = (string) self::flattenSingleValue($needle);
4914 $haystack = (string) self::flattenSingleValue($haystack);
4917 if (($offset > 0) && (strlen($haystack) > $offset)) {
4918 if (function_exists(
'mb_stripos')) {
4919 $pos = mb_stripos($haystack, $needle, --$offset,
'UTF-8');
4921 $pos = stripos($haystack, $needle, --$offset);
4923 if ($pos !==
false) {
4927 return self::$_errorCodes[
'value'];
4938 public static function LEFT($value =
'', $chars = 1) {
4942 if (function_exists(
'mb_substr')) {
4943 return mb_substr($value, 0, $chars,
'UTF-8');
4945 return substr($value, 0, $chars);
4957 public static function RIGHT($value =
'', $chars = 1) {
4961 if ((function_exists(
'mb_substr')) && (function_exists(
'mb_strlen'))) {
4962 return mb_substr($value, mb_strlen($value,
'UTF-8') - $chars, $chars,
'UTF-8');
4964 return substr($value, strlen($value) - $chars);
4977 public static function MID($value =
'',
$start = 1, $chars = null) {
4982 if (function_exists(
'mb_substr')) {
4983 return mb_substr($value, --
$start, $chars,
'UTF-8');
4985 return substr($value, --
$start, $chars);
4998 public static function REPLACE($oldText =
'',
$start = 1, $chars = null, $newText) {
5007 return $left.$newText.$right;
5020 if (is_string($testValue)) {
5033 public static function FIXEDFORMAT($value,$decimals=2,$no_commas=
false) {
5038 $valueResult = round($value,$decimals);
5039 if ($decimals < 0) { $decimals = 0; }
5041 $valueResult = number_format($valueResult,$decimals);
5044 return (
string) $valueResult;
5071 if (is_string($stringValue) || is_numeric($stringValue)) {
5072 return trim(preg_replace(
'/ +/',
' ',$stringValue));
5089 if (self::$_invalidChars == Null) {
5090 self::$_invalidChars = range(chr(0),chr(31));
5093 if (is_string($stringValue) || is_numeric($stringValue)) {
5094 return str_replace(self::$_invalidChars,
'',trim($stringValue,
"\x00..\x1F"));
5110 foreach(self::$_errorCodes as $errorCode) {
5111 if ($value == $errorCode) {
5116 return self::$_errorCodes[
'na'];
5127 if (!is_null($value)) {
5131 return is_null($value);
5157 return in_array($value, array_values(self::$_errorCodes));
5167 public static function IS_NA($value =
'') {
5170 return ($value === self::$_errorCodes[
'na']);
5183 if ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
5184 return self::$_errorCodes[
'value'];
5186 return ($value % 2 == 0);
5196 public static function IS_ODD($value = null) {
5199 if ((is_bool($value)) || ((is_string($value)) && (!is_numeric($value)))) {
5200 return self::$_errorCodes[
'value'];
5202 return (abs($value) % 2 == 1);
5215 if (is_string($value)) {
5218 return is_numeric($value);
5231 return is_bool($value);
5244 return is_string($value);
5265 return 'PHPExcel 1.7.0, 2009-08-10';
5278 public static function DATE($year = 0, $month = 1, $day = 1) {
5279 $year = (integer) self::flattenSingleValue($year);
5280 $month = (integer) self::flattenSingleValue($month);
5281 $day = (integer) self::flattenSingleValue($day);
5285 if ($year < ($baseYear-1900)) {
5286 return self::$_errorCodes[
'num'];
5288 if ((($baseYear-1900) != 0) && ($year < $baseYear) && ($year >= 1900)) {
5289 return self::$_errorCodes[
'num'];
5292 if (($year < $baseYear) && ($year >= ($baseYear-1900))) {
5299 $year += ceil($month / 12) - 1;
5300 $month = 13 - abs($month % 12);
5303 $year += floor($month / 12);
5304 $month = ($month % 12);
5308 if (($year < $baseYear) || ($year >= 10000)) {
5309 return self::$_errorCodes[
'num'];
5314 switch (self::getReturnDateType()) {
5315 case self::RETURNDATE_EXCEL :
return (
float) $excelDateValue;
5334 public static function TIME($hour = 0, $minute = 0, $second = 0) {
5339 if ($hour ==
'') { $hour = 0; }
5340 if ($minute ==
'') { $minute = 0; }
5341 if ($second ==
'') { $second = 0; }
5343 if ((!is_numeric($hour)) || (!is_numeric($minute)) || (!is_numeric($second))) {
5344 return self::$_errorCodes[
'value'];
5346 $hour = (integer) $hour;
5347 $minute = (integer) $minute;
5348 $second = (integer) $second;
5351 $minute += floor($second / 60);
5352 $second = 60 - abs($second % 60);
5353 if ($second == 60) { $second = 0; }
5354 }
elseif ($second >= 60) {
5355 $minute += floor($second / 60);
5356 $second = $second % 60;
5359 $hour += floor($minute / 60);
5360 $minute = 60 - abs($minute % 60);
5361 if ($minute == 60) { $minute = 0; }
5362 }
elseif ($minute >= 60) {
5363 $hour += floor($minute / 60);
5364 $minute = $minute % 60;
5370 return self::$_errorCodes[
'num'];
5374 switch (self::getReturnDateType()) {
5375 case self::RETURNDATE_EXCEL : $date = 0;
5384 case self::RETURNDATE_PHP_OBJECT : $dayAdjust = 0;
5386 $dayAdjust = floor($hour / 24);
5387 $hour = 24 - abs($hour % 24);
5388 if ($hour == 24) { $hour = 0; }
5390 $dayAdjust = floor($hour / 24);
5393 $phpDateObject =
new DateTime(
'1900-01-01 '.$hour.
':'.$minute.
':'.$second);
5394 if ($dayAdjust != 0) {
5395 $phpDateObject->modify($dayAdjust.
' days');
5397 return $phpDateObject;
5411 $dateValue = str_replace(array(
'/',
'.',
' '),array(
'-',
'-',
'-'),self::flattenSingleValue(trim($dateValue,
'"')));
5414 $t1 = explode(
'-',$dateValue);
5415 foreach($t1 as &
$t) {
5416 if ((is_numeric($t)) && (($t > 31) && ($t < 100))) {
5418 return self::$_errorCodes[
'value'];
5426 $dateValue = implode(
'-',$t1);
5428 $PHPDateArray = date_parse($dateValue);
5429 if (($PHPDateArray === False) || ($PHPDateArray[
'error_count'] > 0)) {
5430 $testVal1 = strtok($dateValue,
'- ');
5431 if ($testVal1 !== False) {
5432 $testVal2 = strtok(
'- ');
5433 if ($testVal2 !== False) {
5434 $testVal3 = strtok(
'- ');
5435 if ($testVal3 === False) {
5436 $testVal3 = strftime(
'%Y');
5439 return self::$_errorCodes[
'value'];
5442 return self::$_errorCodes[
'value'];
5444 $PHPDateArray = date_parse($testVal1.
'-'.$testVal2.
'-'.$testVal3);
5445 if (($PHPDateArray === False) || ($PHPDateArray[
'error_count'] > 0)) {
5446 $PHPDateArray = date_parse($testVal2.
'-'.$testVal1.
'-'.$testVal3);
5447 if (($PHPDateArray === False) || ($PHPDateArray[
'error_count'] > 0)) {
5448 return self::$_errorCodes[
'value'];
5453 if (($PHPDateArray !== False) && ($PHPDateArray[
'error_count'] == 0)) {
5455 if ($PHPDateArray[
'year'] ==
'') { $PHPDateArray[
'year'] = strftime(
'%Y'); }
5456 if ($PHPDateArray[
'month'] ==
'') { $PHPDateArray[
'month'] = strftime(
'%m'); }
5457 if ($PHPDateArray[
'day'] ==
'') { $PHPDateArray[
'day'] = strftime(
'%d'); }
5458 $excelDateValue = floor(
PHPExcel_Shared_Date::FormattedPHPToExcel($PHPDateArray[
'year'],$PHPDateArray[
'month'],$PHPDateArray[
'day'],$PHPDateArray[
'hour'],$PHPDateArray[
'minute'],$PHPDateArray[
'second']));
5460 switch (self::getReturnDateType()) {
5461 case self::RETURNDATE_EXCEL :
return (
float) $excelDateValue;
5465 case self::RETURNDATE_PHP_OBJECT :
return new DateTime($PHPDateArray[
'year'].
'-'.$PHPDateArray[
'month'].
'-'.$PHPDateArray[
'day'].
' 00:00:00');
5469 return self::$_errorCodes[
'value'];
5480 if (!is_numeric($dateValue)) {
5481 if ((is_string($dateValue)) && (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC)) {
5482 return self::$_errorCodes[
'value'];
5507 if ((($PHPDateArray = date_parse($timeValue)) !== False) && ($PHPDateArray[
'error_count'] == 0)) {
5508 if (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE) {
5514 switch (self::getReturnDateType()) {
5515 case self::RETURNDATE_EXCEL :
return (
float) $excelDateValue;
5519 case self::RETURNDATE_PHP_OBJECT :
return new DateTime(
'1900-01-01 '.$PHPDateArray[
'hour'].
':'.$PHPDateArray[
'minute'].
':'.$PHPDateArray[
'second']);
5523 return self::$_errorCodes[
'value'];
5549 $saveTimeZone = date_default_timezone_get();
5550 date_default_timezone_set(
'UTC');
5552 switch (self::getReturnDateType()) {
5555 case self::RETURNDATE_PHP_NUMERIC : $retValue = (integer) time();
5557 case self::RETURNDATE_PHP_OBJECT : $retValue =
new DateTime();
5560 date_default_timezone_set($saveTimeZone);
5573 $saveTimeZone = date_default_timezone_get();
5574 date_default_timezone_set(
'UTC');
5577 switch (self::getReturnDateType()) {
5578 case self::RETURNDATE_EXCEL : $retValue = (float) $excelDateTime;
5585 date_default_timezone_set($saveTimeZone);
5592 return ((($year % 4) == 0) && (($year % 100) != 0) || (($year % 400) == 0));
5596 private static function _dateDiff360($startDay, $startMonth, $startYear, $endDay, $endMonth, $endYear, $methodUS) {
5597 if ($startDay == 31) {
5599 }
elseif ($methodUS && ($startMonth == 2 && ($startDay == 29 || ($startDay == 28 && !self::_isLeapYear($startYear))))) {
5602 if ($endDay == 31) {
5603 if ($methodUS && $startDay != 30) {
5605 if ($endMonth == 12) {
5616 return $endDay + $endMonth * 30 + $endYear * 360 - $startDay - $startMonth * 30 - $startYear * 360;
5632 if (is_string($startDate = self::_getDateValue($startDate))) {
5633 return self::$_errorCodes[
'value'];
5635 if (is_string($endDate = self::_getDateValue($endDate))) {
5636 return self::$_errorCodes[
'value'];
5641 $startDay = $PHPStartDateObject->format(
'j');
5642 $startMonth = $PHPStartDateObject->format(
'n');
5643 $startYear = $PHPStartDateObject->format(
'Y');
5646 $endDay = $PHPEndDateObject->format(
'j');
5647 $endMonth = $PHPEndDateObject->format(
'n');
5648 $endYear = $PHPEndDateObject->format(
'Y');
5662 public static function DATEDIF($startDate = 0, $endDate = 0, $unit =
'D') {
5665 $unit = strtoupper(self::flattenSingleValue($unit));
5667 if (is_string($startDate = self::_getDateValue($startDate))) {
5668 return self::$_errorCodes[
'value'];
5670 if (is_string($endDate = self::_getDateValue($endDate))) {
5671 return self::$_errorCodes[
'value'];
5675 if ($startDate >= $endDate) {
5676 return self::$_errorCodes[
'num'];
5680 $difference = $endDate - $startDate;
5683 $startDays = $PHPStartDateObject->format(
'j');
5684 $startMonths = $PHPStartDateObject->format(
'n');
5685 $startYears = $PHPStartDateObject->format(
'Y');
5688 $endDays = $PHPEndDateObject->format(
'j');
5689 $endMonths = $PHPEndDateObject->format(
'n');
5690 $endYears = $PHPEndDateObject->format(
'Y');
5692 $retVal = self::$_errorCodes[
'num'];
5695 $retVal = intval($difference);
5698 $retVal = intval($endMonths - $startMonths) + (intval($endYears - $startYears) * 12);
5700 if ($endDays < $startDays) {
5705 $retVal = intval($endYears - $startYears);
5707 if ($endMonths < $startMonths) {
5709 }
elseif (($endMonths == $startMonths) && ($endDays < $startDays)) {
5714 if ($endDays < $startDays) {
5716 $PHPEndDateObject->modify(
'-'.$endDays.
' days');
5717 $adjustDays = $PHPEndDateObject->format(
'j');
5718 if ($adjustDays > $startDays) {
5719 $retVal += ($adjustDays - $startDays);
5722 $retVal = $endDays - $startDays;
5726 $retVal = abs(intval($endMonths - $startMonths));
5728 if ($endDays < $startDays) {
5733 $retVal = intval($difference);
5734 if ($endYears > $startYears) {
5735 while ($endYears > $startYears) {
5736 $PHPEndDateObject->modify(
'-1 year');
5737 $endYears = $PHPEndDateObject->format(
'Y');
5739 $retVal = abs($PHPEndDateObject->format(
'z') - $PHPStartDateObject->format(
'z'));
5769 if (is_string($startDate = self::_getDateValue($startDate))) {
5770 return self::$_errorCodes[
'value'];
5772 if (is_string($endDate = self::_getDateValue($endDate))) {
5773 return self::$_errorCodes[
'value'];
5785 if (self::_isLeapYear($startYear) || self::_isLeapYear($endYear)) {
5788 return self::DATEDIF($startDate,$endDate) / (365 + $leapDay);
5801 return self::$_errorCodes[
'value'];
5816 array_shift($dateArgs);
5817 array_shift($dateArgs);
5820 if (is_string($startDate = $sDate = self::_getDateValue($startDate))) {
5821 return self::$_errorCodes[
'value'];
5823 if (is_string($endDate = $eDate = self::_getDateValue($endDate))) {
5824 return self::$_errorCodes[
'value'];
5827 if ($sDate > $eDate) {
5828 $startDate = $eDate;
5834 if ($startDoW < 0) { $startDoW = 0; }
5836 if ($endDoW >= 6) { $endDoW = 0; }
5838 $wholeWeekDays = floor(($endDate - $startDate) / 7) * 5;
5839 $partWeekDays = $endDoW + $startDoW;
5840 if ($partWeekDays > 5) {
5845 $holidayCountedArray = array();
5846 foreach ($dateArgs as $holidayDate) {
5847 if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
5848 return self::$_errorCodes[
'value'];
5850 if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
5851 if ((self::DAYOFWEEK($holidayDate,2) < 6) && (!in_array($holidayDate,$holidayCountedArray))) {
5853 $holidayCountedArray[] = $holidayDate;
5858 if ($sDate > $eDate) {
5859 return 0 - ($wholeWeekDays + $partWeekDays);
5861 return $wholeWeekDays + $partWeekDays;
5873 public static function WORKDAY($startDate,$endDays) {
5876 array_shift($dateArgs);
5877 array_shift($dateArgs);
5879 if (is_string($startDate = self::_getDateValue($startDate))) {
5880 return self::$_errorCodes[
'value'];
5882 if (!is_numeric($endDays)) {
5883 return self::$_errorCodes[
'value'];
5885 $endDate = (float) $startDate + (floor($endDays / 5) * 7) + ($endDays % 5);
5892 if ($endDays >= 0) {
5893 $endDate += (7 - $endDoW);
5895 $endDate -= ($endDoW - 5);
5900 if (count($dateArgs) > 0) {
5901 $holidayCountedArray = $holidayDates = array();
5902 foreach ($dateArgs as $holidayDate) {
5903 if (is_string($holidayDate = self::_getDateValue($holidayDate))) {
5904 return self::$_errorCodes[
'value'];
5906 $holidayDates[] = $holidayDate;
5908 if ($endDays >= 0) {
5909 sort($holidayDates, SORT_NUMERIC);
5911 rsort($holidayDates, SORT_NUMERIC);
5913 foreach ($holidayDates as $holidayDate) {
5914 if ($endDays >= 0) {
5915 if (($holidayDate >= $startDate) && ($holidayDate <= $endDate)) {
5916 if ((self::DAYOFWEEK($holidayDate,2) < 6) && (!in_array($holidayDate,$holidayCountedArray))) {
5918 $holidayCountedArray[] = $holidayDate;
5922 if (($holidayDate <= $startDate) && ($holidayDate >= $endDate)) {
5923 if ((self::DAYOFWEEK($holidayDate,2) < 6) && (!in_array($holidayDate,$holidayCountedArray))) {
5925 $holidayCountedArray[] = $holidayDate;
5931 if ($endDays >= 0) {
5932 $endDate += (7 - $endDoW);
5934 $endDate -= ($endDoW - 5);
5940 switch (self::getReturnDateType()) {
5941 case self::RETURNDATE_EXCEL :
return (
float) $endDate;
5960 if (is_string($dateValue = self::_getDateValue($dateValue))) {
5961 return self::$_errorCodes[
'value'];
5967 return (
int) $PHPDateObject->format(
'j');
5977 public static function DAYOFWEEK($dateValue = 1, $style = 1) {
5979 $style = floor(self::flattenSingleValue($style));
5981 if (is_string($dateValue = self::_getDateValue($dateValue))) {
5982 return self::$_errorCodes[
'value'];
5987 $DoW = $PHPDateObject->format(
'w');
5993 case 2:
if ($DoW == 0) { $DoW = 7; }
5995 case 3:
if ($DoW == 0) { $DoW = 7; }
6001 if (self::$compatibilityMode == self::COMPATIBILITY_EXCEL) {
6003 if (($PHPDateObject->format(
'Y') == 1900) && ($PHPDateObject->format(
'n') <= 2)) {
6005 if ($DoW < $firstDay) {
6027 return self::$_errorCodes[
'value'];
6029 return self::$_errorCodes[
'num'];
6032 if (is_string($dateValue = self::_getDateValue($dateValue))) {
6033 return self::$_errorCodes[
'value'];
6038 $dayOfYear = $PHPDateObject->format(
'z');
6039 $dow = $PHPDateObject->format(
'w');
6040 $PHPDateObject->modify(
'-'.$dayOfYear.
' days');
6041 $dow = $PHPDateObject->format(
'w');
6042 $daysInFirstWeek = 7 - (($dow + (2 -
$method)) % 7);
6043 $dayOfYear -= $daysInFirstWeek;
6044 $weekOfYear = ceil($dayOfYear / 7) + 1;
6046 return (
int) $weekOfYear;
6059 if (is_string($dateValue = self::_getDateValue($dateValue))) {
6060 return self::$_errorCodes[
'value'];
6066 return (
int) $PHPDateObject->format(
'n');
6076 public static function YEAR($dateValue = 1) {
6079 if (is_string($dateValue = self::_getDateValue($dateValue))) {
6080 return self::$_errorCodes[
'value'];
6086 return (
int) $PHPDateObject->format(
'Y');
6099 if (!is_numeric($timeValue)) {
6100 if (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC) {
6101 $testVal = strtok($timeValue,
'/-: ');
6102 if (strlen($testVal) < strlen($timeValue)) {
6103 return self::$_errorCodes[
'value'];
6107 if (is_string($timeValue)) {
6108 return self::$_errorCodes[
'value'];
6112 if (is_real($timeValue)) {
6113 if ($timeValue >= 1) {
6114 $timeValue = fmod($timeValue,1);
6115 }
elseif ($timeValue < 0.0) {
6116 return self::$_errorCodes[
'num'];
6120 return (
int) date(
'G',$timeValue);
6133 if (!is_numeric($timeValue)) {
6134 if (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC) {
6135 $testVal = strtok($timeValue,
'/-: ');
6136 if (strlen($testVal) < strlen($timeValue)) {
6137 return self::$_errorCodes[
'value'];
6141 if (is_string($timeValue)) {
6142 return self::$_errorCodes[
'value'];
6146 if (is_real($timeValue)) {
6147 if ($timeValue >= 1) {
6148 $timeValue = fmod($timeValue,1);
6149 }
elseif ($timeValue < 0.0) {
6150 return self::$_errorCodes[
'num'];
6154 return (
int) date(
'i',$timeValue);
6167 if (!is_numeric($timeValue)) {
6168 if (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC) {
6169 $testVal = strtok($timeValue,
'/-: ');
6170 if (strlen($testVal) < strlen($timeValue)) {
6171 return self::$_errorCodes[
'value'];
6175 if (is_string($timeValue)) {
6176 return self::$_errorCodes[
'value'];
6180 if (is_real($timeValue)) {
6181 if ($timeValue >= 1) {
6182 $timeValue = fmod($timeValue,1);
6183 }
elseif ($timeValue < 0.0) {
6184 return self::$_errorCodes[
'num'];
6188 return (
int) date(
's',$timeValue);
6195 $oMonth = (int) $PHPDateObject->format(
'm');
6196 $oYear = (int) $PHPDateObject->format(
'Y');
6198 $adjustmentMonthsString = (string) $adjustmentMonths;
6199 if ($adjustmentMonths > 0) {
6200 $adjustmentMonthsString =
'+'.$adjustmentMonths;
6202 if ($adjustmentMonths != 0) {
6203 $PHPDateObject->modify($adjustmentMonthsString.
' months');
6205 $nMonth = (int) $PHPDateObject->format(
'm');
6206 $nYear = (int) $PHPDateObject->format(
'Y');
6208 $monthDiff = ($nMonth - $oMonth) + (($nYear - $oYear) * 12);
6209 if ($monthDiff != $adjustmentMonths) {
6210 $adjustDays = (int) $PHPDateObject->format(
'd');
6211 $adjustDaysString =
'-'.$adjustDays.
' days';
6212 $PHPDateObject->modify($adjustDaysString);
6214 return $PHPDateObject;
6228 public static function EDATE($dateValue = 1, $adjustmentMonths = 0) {
6230 $adjustmentMonths = floor(self::flattenSingleValue($adjustmentMonths));
6232 if (!is_numeric($adjustmentMonths)) {
6233 return self::$_errorCodes[
'value'];
6236 if (is_string($dateValue = self::_getDateValue($dateValue))) {
6237 return self::$_errorCodes[
'value'];
6243 switch (self::getReturnDateType()) {
6248 case self::RETURNDATE_PHP_OBJECT :
return $PHPDateObject;
6264 public static function EOMONTH($dateValue = 1, $adjustmentMonths = 0) {
6266 $adjustmentMonths = floor(self::flattenSingleValue($adjustmentMonths));
6268 if (!is_numeric($adjustmentMonths)) {
6269 return self::$_errorCodes[
'value'];
6272 if (is_string($dateValue = self::_getDateValue($dateValue))) {
6273 return self::$_errorCodes[
'value'];
6278 $adjustDays = (int) $PHPDateObject->format(
'd');
6279 $adjustDaysString =
'-'.$adjustDays.
' days';
6280 $PHPDateObject->modify($adjustDaysString);
6282 switch (self::getReturnDateType()) {
6287 case self::RETURNDATE_PHP_OBJECT :
return $PHPDateObject;
6302 public static function TRUNC($value = 0, $number_digits = 0) {
6307 if ($number_digits < 0) {
6308 return self::$_errorCodes[
'value'];
6312 if ($number_digits > 0) {
6313 $value = $value * pow(10, $number_digits);
6315 $value = intval($value);
6316 if ($number_digits > 0) {
6317 $value = $value / pow(10, $number_digits);
6338 if (
$x == 0 &&
$y <= 0) {
6339 return self::$_errorCodes[
'divisionbyzero'];
6348 if (!is_null($places)) {
6349 if (strlen($xVal) <= $places) {
6350 return substr(str_pad($xVal,$places,
'0',STR_PAD_LEFT),-10);
6352 return self::$_errorCodes[
'num'];
6356 return substr($xVal,-10);
6372 if (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE) {
6375 return self::$_errorCodes[
'value'];
6378 if (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC) {
6382 if (strlen(
$x) > preg_match_all(
'/[01]/',
$x,
$out)) {
6383 return self::$_errorCodes[
'num'];
6385 if (strlen(
$x) > 10) {
6386 return self::$_errorCodes[
'num'];
6390 return '-'.(512-bindec(
$x));
6405 $x = floor(self::flattenSingleValue(
$x));
6409 if (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE) {
6412 return self::$_errorCodes[
'value'];
6415 if (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC) {
6419 if (strlen(
$x) > preg_match_all(
'/[01]/',
$x,
$out)) {
6420 return self::$_errorCodes[
'num'];
6422 if (strlen(
$x) > 10) {
6423 return self::$_errorCodes[
'num'];
6426 return str_repeat(
'F',8).substr(strtoupper(dechex(bindec(substr(
$x,-9)))),-2);
6428 $hexVal = (string) strtoupper(dechex(bindec(
$x)));
6443 $x = floor(self::flattenSingleValue(
$x));
6447 if (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE) {
6450 return self::$_errorCodes[
'value'];
6453 if (self::$compatibilityMode == self::COMPATIBILITY_GNUMERIC) {
6457 if (strlen(
$x) > preg_match_all(
'/[01]/',
$x,
$out)) {
6458 return self::$_errorCodes[
'num'];
6460 if (strlen(
$x) > 10) {
6461 return self::$_errorCodes[
'num'];
6464 return str_repeat(
'7',7).substr(strtoupper(decoct(bindec(substr(
$x,-9)))),-3);
6466 $octVal = (string) decoct(bindec(
$x));
6485 if (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE) {
6488 return self::$_errorCodes[
'value'];
6492 if (strlen(
$x) > preg_match_all(
'/[-0123456789.]/',
$x,
$out)) {
6493 return self::$_errorCodes[
'value'];
6495 $x = (string) floor(
$x);
6497 if (strlen($r) == 32) {
6499 $r = substr($r,-10);
6500 }
elseif (strlen($r) > 11) {
6501 return self::$_errorCodes[
'num'];
6521 if (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE) {
6524 return self::$_errorCodes[
'value'];
6528 if (strlen(
$x) > preg_match_all(
'/[-0123456789.]/',
$x,
$out)) {
6529 return self::$_errorCodes[
'value'];
6531 $x = (string) floor(
$x);
6533 if (strlen($r) == 11) {
6535 $r = substr($r,-10);
6555 if (self::$compatibilityMode == self::COMPATIBILITY_OPENOFFICE) {
6558 return self::$_errorCodes[
'value'];
6562 if (strlen(
$x) > preg_match_all(
'/[-0123456789.]/',
$x,
$out)) {
6563 return self::$_errorCodes[
'value'];
6565 $x = (string) floor(
$x);
6566 $r = strtoupper(dechex(
$x));
6567 if (strlen($r) == 8) {
6589 return self::$_errorCodes[
'value'];
6592 if (strlen(
$x) > preg_match_all(
'/[0123456789ABCDEF]/',strtoupper(
$x),
$out)) {
6593 return self::$_errorCodes[
'num'];
6595 $binVal = decbin(hexdec(
$x));
6597 return substr(self::_nbrConversionFormat($binVal,$places),-10);
6614 return self::$_errorCodes[
'value'];
6617 if (strlen(
$x) > preg_match_all(
'/[0123456789ABCDEF]/',strtoupper(
$x),
$out)) {
6618 return self::$_errorCodes[
'num'];
6620 $octVal = decoct(hexdec(
$x));
6638 return self::$_errorCodes[
'value'];
6641 if (strlen(
$x) > preg_match_all(
'/[0123456789ABCDEF]/',strtoupper(
$x),
$out)) {
6642 return self::$_errorCodes[
'num'];
6661 return self::$_errorCodes[
'value'];
6664 if (preg_match_all(
'/[01234567]/',
$x,
$out) != strlen(
$x)) {
6665 return self::$_errorCodes[
'num'];
6667 $binVal = decbin(octdec(
$x));
6685 return self::$_errorCodes[
'value'];
6688 if (preg_match_all(
'/[01234567]/',
$x,
$out) != strlen(
$x)) {
6689 return self::$_errorCodes[
'num'];
6708 return self::$_errorCodes[
'value'];
6711 if (preg_match_all(
'/[01234567]/',
$x,
$out) != strlen(
$x)) {
6712 return self::$_errorCodes[
'num'];
6714 $hexVal = strtoupper(dechex(octdec(
$x)));
6721 $workString = (string) $complexNumber;
6723 $realNumber = $imaginary = 0;
6725 $suffix = substr($workString,-1);
6726 if (!is_numeric($suffix)) {
6727 $workString = substr($workString,0,-1);
6734 if (strlen($workString) > 0) {
6735 $leadingSign = (($workString{0} ==
'+') || ($workString{0} ==
'-')) ? 1 : 0;
6738 $realNumber = strtok($workString,
'+-');
6739 if (strtoupper(substr($realNumber,-1)) ==
'E') {
6740 $power = strtok(
'+-');
6744 $realNumber = substr($workString,0,strlen($realNumber)+strlen($power)+$leadingSign);
6746 if ($suffix !=
'') {
6747 $imaginary = substr($workString,strlen($realNumber));
6749 if (($imaginary ==
'') && (($realNumber ==
'') || ($realNumber ==
'+') || ($realNumber ==
'-'))) {
6750 $imaginary = $realNumber.
'1';
6752 }
else if ($imaginary ==
'') {
6753 $imaginary = $realNumber;
6755 }
elseif (($imaginary ==
'+') || ($imaginary ==
'-')) {
6760 $complexArray = array(
'real' => $realNumber,
6761 'imaginary' => $imaginary,
6765 return $complexArray;
6770 if ($complexNumber{0} ==
'+') $complexNumber = substr($complexNumber,1);
6771 if ($complexNumber{0} ==
'0') $complexNumber = substr($complexNumber,1);
6772 if ($complexNumber{0} ==
'.') $complexNumber =
'0'.$complexNumber;
6773 if ($complexNumber{0} ==
'+') $complexNumber = substr($complexNumber,1);
6774 return $complexNumber;
6788 public static function COMPLEX($realNumber=0.0, $imaginary=0.0, $suffix=
'i') {
6793 if (((is_numeric($realNumber)) && (is_numeric($imaginary))) &&
6794 (($suffix ==
'i') || ($suffix ==
'j') || ($suffix ==
''))) {
6795 if ($realNumber == 0.0) {
6796 if ($imaginary == 0.0) {
6797 return (
string)
'0';
6798 }
elseif ($imaginary == 1.0) {
6799 return (
string) $suffix;
6800 }
elseif ($imaginary == -1.0) {
6801 return (
string)
'-'.$suffix;
6803 return (
string) $imaginary.$suffix;
6804 }
elseif ($imaginary == 0.0) {
6805 return (
string) $realNumber;
6806 }
elseif ($imaginary == 1.0) {
6807 return (
string) $realNumber.
'+'.$suffix;
6808 }
elseif ($imaginary == -1.0) {
6809 return (
string) $realNumber.
'-'.$suffix;
6811 if ($imaginary > 0) { $imaginary = (string)
'+'.$imaginary; }
6812 return (
string) $realNumber.$imaginary.$suffix;
6814 return self::$_errorCodes[
'value'];
6830 if (!is_array($parsedComplex)) {
6831 return $parsedComplex;
6833 return $parsedComplex[
'imaginary'];
6845 public static function IMREAL($complexNumber) {
6849 if (!is_array($parsedComplex)) {
6850 return $parsedComplex;
6852 return $parsedComplex[
'real'];
6864 public static function IMABS($complexNumber) {
6868 if (!is_array($parsedComplex)) {
6869 return $parsedComplex;
6871 return sqrt(($parsedComplex[
'real'] * $parsedComplex[
'real']) + ($parsedComplex[
'imaginary'] * $parsedComplex[
'imaginary']));
6887 if (!is_array($parsedComplex)) {
6888 return $parsedComplex;
6891 if ($parsedComplex[
'real'] == 0.0) {
6892 if ($parsedComplex[
'imaginary'] == 0.0) {
6894 }
elseif($parsedComplex[
'imaginary'] < 0.0) {
6899 }
elseif ($parsedComplex[
'real'] > 0.0) {
6900 return atan($parsedComplex[
'imaginary'] / $parsedComplex[
'real']);
6901 }
elseif ($parsedComplex[
'imaginary'] < 0.0) {
6902 return 0 - (pi() - atan(abs($parsedComplex[
'imaginary']) / abs($parsedComplex[
'real'])));
6904 return pi() - atan($parsedComplex[
'imaginary'] / abs($parsedComplex[
'real']));
6922 if (!is_array($parsedComplex)) {
6923 return $parsedComplex;
6926 if ($parsedComplex[
'imaginary'] == 0.0) {
6927 return $parsedComplex[
'real'];
6929 return self::_cleanComplex(self::COMPLEX($parsedComplex[
'real'], 0 - $parsedComplex[
'imaginary'], $parsedComplex[
'suffix']));
6942 public static function IMCOS($complexNumber) {
6946 if (!is_array($parsedComplex)) {
6947 return $parsedComplex;
6950 if ($parsedComplex[
'imaginary'] == 0.0) {
6951 return cos($parsedComplex[
'real']);
6953 return self::IMCONJUGATE(self::COMPLEX(cos($parsedComplex[
'real']) * cosh($parsedComplex[
'imaginary']),sin($parsedComplex[
'real']) * sinh($parsedComplex[
'imaginary']),$parsedComplex[
'suffix']));
6966 public static function IMSIN($complexNumber) {
6970 if (!is_array($parsedComplex)) {
6971 return $parsedComplex;
6974 if ($parsedComplex[
'imaginary'] == 0.0) {
6975 return sin($parsedComplex[
'real']);
6977 return self::COMPLEX(sin($parsedComplex[
'real']) * cosh($parsedComplex[
'imaginary']),cos($parsedComplex[
'real']) * sinh($parsedComplex[
'imaginary']),$parsedComplex[
'suffix']);
6990 public static function IMSQRT($complexNumber) {
6994 if (!is_array($parsedComplex)) {
6995 return $parsedComplex;
6999 $d1 = cos($theta / 2);
7000 $d2 = sin($theta / 2);
7001 $r = sqrt(sqrt(($parsedComplex[
'real'] * $parsedComplex[
'real']) + ($parsedComplex[
'imaginary'] * $parsedComplex[
'imaginary'])));
7003 if ($parsedComplex[
'suffix'] ==
'') {
7006 return self::COMPLEX($d1 * $r,$d2 * $r,$parsedComplex[
'suffix']);
7019 public static function IMLN($complexNumber) {
7023 if (!is_array($parsedComplex)) {
7024 return $parsedComplex;
7027 if (($parsedComplex[
'real'] == 0.0) && ($parsedComplex[
'imaginary'] == 0.0)) {
7028 return self::$_errorCodes[
'num'];
7031 $logR = log(sqrt(($parsedComplex[
'real'] * $parsedComplex[
'real']) + ($parsedComplex[
'imaginary'] * $parsedComplex[
'imaginary'])));
7034 if ($parsedComplex[
'suffix'] ==
'') {
7054 if (!is_array($parsedComplex)) {
7055 return $parsedComplex;
7058 if (($parsedComplex[
'real'] == 0.0) && ($parsedComplex[
'imaginary'] == 0.0)) {
7059 return self::$_errorCodes[
'num'];
7060 }
elseif (($parsedComplex[
'real'] > 0.0) && ($parsedComplex[
'imaginary'] == 0.0)) {
7061 return log10($parsedComplex[
'real']);
7076 public static function IMLOG2($complexNumber) {
7080 if (!is_array($parsedComplex)) {
7081 return $parsedComplex;
7084 if (($parsedComplex[
'real'] == 0.0) && ($parsedComplex[
'imaginary'] == 0.0)) {
7085 return self::$_errorCodes[
'num'];
7086 }
elseif (($parsedComplex[
'real'] > 0.0) && ($parsedComplex[
'imaginary'] == 0.0)) {
7087 return log($parsedComplex[
'real'],2);
7102 public static function IMEXP($complexNumber) {
7106 if (!is_array($parsedComplex)) {
7107 return $parsedComplex;
7110 if (($parsedComplex[
'real'] == 0.0) && ($parsedComplex[
'imaginary'] == 0.0)) {
7114 $e = exp($parsedComplex[
'real']);
7115 $eX = $e * cos($parsedComplex[
'imaginary']);
7116 $eY = $e * sin($parsedComplex[
'imaginary']);
7118 if ($parsedComplex[
'suffix'] ==
'') {
7134 public static function IMPOWER($complexNumber,$realNumber) {
7138 if (!is_numeric($realNumber)) {
7139 return self::$_errorCodes[
'value'];
7143 if (!is_array($parsedComplex)) {
7144 return $parsedComplex;
7147 $r = sqrt(($parsedComplex[
'real'] * $parsedComplex[
'real']) + ($parsedComplex[
'imaginary'] * $parsedComplex[
'imaginary']));
7148 $rPower = pow($r,$realNumber);
7152 }
elseif ($parsedComplex[
'imaginary'] == 0.0) {
7153 return self::COMPLEX($rPower * cos($theta),$rPower * sin($theta),$parsedComplex[
'suffix']);
7155 return self::COMPLEX($rPower * cos($theta),$rPower * sin($theta),$parsedComplex[
'suffix']);
7169 public static function IMDIV($complexDividend,$complexDivisor) {
7174 if (!is_array($parsedComplexDividend)) {
7175 return $parsedComplexDividend;
7179 if (!is_array($parsedComplexDivisor)) {
7180 return $parsedComplexDividend;
7183 if (($parsedComplexDividend[
'suffix'] !=
'') && ($parsedComplexDivisor[
'suffix'] !=
'') &&
7184 ($parsedComplexDividend[
'suffix'] != $parsedComplexDivisor[
'suffix'])) {
7185 return self::$_errorCodes[
'num'];
7187 if (($parsedComplexDividend[
'suffix'] !=
'') && ($parsedComplexDivisor[
'suffix'] ==
'')) {
7188 $parsedComplexDivisor[
'suffix'] = $parsedComplexDividend[
'suffix'];
7191 $d1 = ($parsedComplexDividend[
'real'] * $parsedComplexDivisor[
'real']) + ($parsedComplexDividend[
'imaginary'] * $parsedComplexDivisor[
'imaginary']);
7192 $d2 = ($parsedComplexDividend[
'imaginary'] * $parsedComplexDivisor[
'real']) - ($parsedComplexDividend[
'real'] * $parsedComplexDivisor[
'imaginary']);
7193 $d3 = ($parsedComplexDivisor[
'real'] * $parsedComplexDivisor[
'real']) + ($parsedComplexDivisor[
'imaginary'] * $parsedComplexDivisor[
'imaginary']);
7217 public static function IMSUB($complexNumber1,$complexNumber2) {
7222 if (!is_array($parsedComplex1)) {
7223 return $parsedComplex1;
7227 if (!is_array($parsedComplex2)) {
7228 return $parsedComplex2;
7231 if ((($parsedComplex1[
'suffix'] !=
'') && ($parsedComplex2[
'suffix'] !=
'')) &&
7232 ($parsedComplex1[
'suffix'] != $parsedComplex2[
'suffix'])) {
7233 return self::$_errorCodes[
'num'];
7234 }
elseif (($parsedComplex1[
'suffix'] ==
'') && ($parsedComplex2[
'suffix'] !=
'')) {
7235 $parsedComplex1[
'suffix'] = $parsedComplex2[
'suffix'];
7238 $d1 = $parsedComplex1[
'real'] - $parsedComplex2[
'real'];
7239 $d2 = $parsedComplex1[
'imaginary'] - $parsedComplex2[
'imaginary'];
7260 foreach ($aArgs as $arg) {
7262 if (!is_array($parsedComplex)) {
7263 return $parsedComplex;
7266 if ($activeSuffix ==
'') {
7267 $activeSuffix = $parsedComplex[
'suffix'];
7268 }
elseif (($parsedComplex[
'suffix'] !=
'') && ($activeSuffix != $parsedComplex[
'suffix'])) {
7269 return self::$_errorCodes[
'value'];
7272 $returnValue[
'real'] += $parsedComplex[
'real'];
7273 $returnValue[
'imaginary'] += $parsedComplex[
'imaginary'];
7276 if ($returnValue[
'imaginary'] == 0.0) { $activeSuffix =
''; }
7277 return self::COMPLEX($returnValue[
'real'],$returnValue[
'imaginary'],$activeSuffix);
7296 foreach ($aArgs as $arg) {
7298 if (!is_array($parsedComplex)) {
7299 return $parsedComplex;
7301 $workValue = $returnValue;
7302 if (($parsedComplex[
'suffix'] !=
'') && ($activeSuffix ==
'')) {
7303 $activeSuffix = $parsedComplex[
'suffix'];
7304 }
elseif (($parsedComplex[
'suffix'] !=
'') && ($activeSuffix != $parsedComplex[
'suffix'])) {
7305 return self::$_errorCodes[
'num'];
7307 $returnValue[
'real'] = ($workValue[
'real'] * $parsedComplex[
'real']) - ($workValue[
'imaginary'] * $parsedComplex[
'imaginary']);
7308 $returnValue[
'imaginary'] = ($workValue[
'real'] * $parsedComplex[
'imaginary']) + ($workValue[
'imaginary'] * $parsedComplex[
'real']);
7311 if ($returnValue[
'imaginary'] == 0.0) { $activeSuffix =
''; }
7312 return self::COMPLEX($returnValue[
'real'],$returnValue[
'imaginary'],$activeSuffix);
7316 private static $_conversionUnits = array(
'g' => array(
'Group' =>
'Mass',
'Unit Name' =>
'Gram',
'AllowPrefix' => True ),
7317 'sg' => array(
'Group' =>
'Mass',
'Unit Name' =>
'Slug',
'AllowPrefix' => False ),
7318 'lbm' => array(
'Group' =>
'Mass',
'Unit Name' =>
'Pound mass (avoirdupois)',
'AllowPrefix' => False ),
7319 'u' => array(
'Group' =>
'Mass',
'Unit Name' =>
'U (atomic mass unit)',
'AllowPrefix' => True ),
7320 'ozm' => array(
'Group' =>
'Mass',
'Unit Name' =>
'Ounce mass (avoirdupois)',
'AllowPrefix' => False ),
7321 'm' => array(
'Group' =>
'Distance',
'Unit Name' =>
'Meter',
'AllowPrefix' => True ),
7322 'mi' => array(
'Group' =>
'Distance',
'Unit Name' =>
'Statute mile',
'AllowPrefix' => False ),
7323 'Nmi' => array(
'Group' =>
'Distance',
'Unit Name' =>
'Nautical mile',
'AllowPrefix' => False ),
7324 'in' => array(
'Group' =>
'Distance',
'Unit Name' =>
'Inch',
'AllowPrefix' => False ),
7325 'ft' => array(
'Group' =>
'Distance',
'Unit Name' =>
'Foot',
'AllowPrefix' => False ),
7326 'yd' => array(
'Group' =>
'Distance',
'Unit Name' =>
'Yard',
'AllowPrefix' => False ),
7327 'ang' => array(
'Group' =>
'Distance',
'Unit Name' =>
'Angstrom',
'AllowPrefix' => True ),
7328 'Pica' => array(
'Group' =>
'Distance',
'Unit Name' =>
'Pica (1/72 in)',
'AllowPrefix' => False ),
7329 'yr' => array(
'Group' =>
'Time',
'Unit Name' =>
'Year',
'AllowPrefix' => False ),
7330 'day' => array(
'Group' =>
'Time',
'Unit Name' =>
'Day',
'AllowPrefix' => False ),
7331 'hr' => array(
'Group' =>
'Time',
'Unit Name' =>
'Hour',
'AllowPrefix' => False ),
7332 'mn' => array(
'Group' =>
'Time',
'Unit Name' =>
'Minute',
'AllowPrefix' => False ),
7333 'sec' => array(
'Group' =>
'Time',
'Unit Name' =>
'Second',
'AllowPrefix' => True ),
7334 'Pa' => array(
'Group' =>
'Pressure',
'Unit Name' =>
'Pascal',
'AllowPrefix' => True ),
7335 'p' => array(
'Group' =>
'Pressure',
'Unit Name' =>
'Pascal',
'AllowPrefix' => True ),
7336 'atm' => array(
'Group' =>
'Pressure',
'Unit Name' =>
'Atmosphere',
'AllowPrefix' => True ),
7337 'at' => array(
'Group' =>
'Pressure',
'Unit Name' =>
'Atmosphere',
'AllowPrefix' => True ),
7338 'mmHg' => array(
'Group' =>
'Pressure',
'Unit Name' =>
'mm of Mercury',
'AllowPrefix' => True ),
7339 'N' => array(
'Group' =>
'Force',
'Unit Name' =>
'Newton',
'AllowPrefix' => True ),
7340 'dyn' => array(
'Group' =>
'Force',
'Unit Name' =>
'Dyne',
'AllowPrefix' => True ),
7341 'dy' => array(
'Group' =>
'Force',
'Unit Name' =>
'Dyne',
'AllowPrefix' => True ),
7342 'lbf' => array(
'Group' =>
'Force',
'Unit Name' =>
'Pound force',
'AllowPrefix' => False ),
7343 'J' => array(
'Group' =>
'Energy',
'Unit Name' =>
'Joule',
'AllowPrefix' => True ),
7344 'e' => array(
'Group' =>
'Energy',
'Unit Name' =>
'Erg',
'AllowPrefix' => True ),
7345 'c' => array(
'Group' =>
'Energy',
'Unit Name' =>
'Thermodynamic calorie',
'AllowPrefix' => True ),
7346 'cal' => array(
'Group' =>
'Energy',
'Unit Name' =>
'IT calorie',
'AllowPrefix' => True ),
7347 'eV' => array(
'Group' =>
'Energy',
'Unit Name' =>
'Electron volt',
'AllowPrefix' => True ),
7348 'ev' => array(
'Group' =>
'Energy',
'Unit Name' =>
'Electron volt',
'AllowPrefix' => True ),
7349 'HPh' => array(
'Group' =>
'Energy',
'Unit Name' =>
'Horsepower-hour',
'AllowPrefix' => False ),
7350 'hh' => array(
'Group' =>
'Energy',
'Unit Name' =>
'Horsepower-hour',
'AllowPrefix' => False ),
7351 'Wh' => array(
'Group' =>
'Energy',
'Unit Name' =>
'Watt-hour',
'AllowPrefix' => True ),
7352 'wh' => array(
'Group' =>
'Energy',
'Unit Name' =>
'Watt-hour',
'AllowPrefix' => True ),
7353 'flb' => array(
'Group' =>
'Energy',
'Unit Name' =>
'Foot-pound',
'AllowPrefix' => False ),
7354 'BTU' => array(
'Group' =>
'Energy',
'Unit Name' =>
'BTU',
'AllowPrefix' => False ),
7355 'btu' => array(
'Group' =>
'Energy',
'Unit Name' =>
'BTU',
'AllowPrefix' => False ),
7356 'HP' => array(
'Group' =>
'Power',
'Unit Name' =>
'Horsepower',
'AllowPrefix' => False ),
7357 'h' => array(
'Group' =>
'Power',
'Unit Name' =>
'Horsepower',
'AllowPrefix' => False ),
7358 'W' => array(
'Group' =>
'Power',
'Unit Name' =>
'Watt',
'AllowPrefix' => True ),
7359 'w' => array(
'Group' =>
'Power',
'Unit Name' =>
'Watt',
'AllowPrefix' => True ),
7360 'T' => array(
'Group' =>
'Magnetism',
'Unit Name' =>
'Tesla',
'AllowPrefix' => True ),
7361 'ga' => array(
'Group' =>
'Magnetism',
'Unit Name' =>
'Gauss',
'AllowPrefix' => True ),
7362 'C' => array(
'Group' =>
'Temperature',
'Unit Name' =>
'Celsius',
'AllowPrefix' => False ),
7363 'cel' => array(
'Group' =>
'Temperature',
'Unit Name' =>
'Celsius',
'AllowPrefix' => False ),
7364 'F' => array(
'Group' =>
'Temperature',
'Unit Name' =>
'Fahrenheit',
'AllowPrefix' => False ),
7365 'fah' => array(
'Group' =>
'Temperature',
'Unit Name' =>
'Fahrenheit',
'AllowPrefix' => False ),
7366 'K' => array(
'Group' =>
'Temperature',
'Unit Name' =>
'Kelvin',
'AllowPrefix' => False ),
7367 'kel' => array(
'Group' =>
'Temperature',
'Unit Name' =>
'Kelvin',
'AllowPrefix' => False ),
7368 'tsp' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'Teaspoon',
'AllowPrefix' => False ),
7369 'tbs' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'Tablespoon',
'AllowPrefix' => False ),
7370 'oz' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'Fluid Ounce',
'AllowPrefix' => False ),
7371 'cup' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'Cup',
'AllowPrefix' => False ),
7372 'pt' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'U.S. Pint',
'AllowPrefix' => False ),
7373 'us_pt' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'U.S. Pint',
'AllowPrefix' => False ),
7374 'uk_pt' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'U.K. Pint',
'AllowPrefix' => False ),
7375 'qt' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'Quart',
'AllowPrefix' => False ),
7376 'gal' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'Gallon',
'AllowPrefix' => False ),
7377 'l' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'Litre',
'AllowPrefix' => True ),
7378 'lt' => array(
'Group' =>
'Liquid',
'Unit Name' =>
'Litre',
'AllowPrefix' => True )
7382 'Z' => array(
'multiplier' => 1E21,
'name' =>
'zetta' ),
7383 'E' => array(
'multiplier' => 1E18,
'name' =>
'exa' ),
7384 'P' => array(
'multiplier' => 1E15,
'name' =>
'peta' ),
7385 'T' => array(
'multiplier' => 1E12,
'name' =>
'tera' ),
7386 'G' => array(
'multiplier' => 1E9,
'name' =>
'giga' ),
7387 'M' => array(
'multiplier' => 1E6,
'name' =>
'mega' ),
7388 'k' => array(
'multiplier' => 1E3,
'name' =>
'kilo' ),
7389 'h' => array(
'multiplier' => 1E2,
'name' =>
'hecto' ),
7390 'e' => array(
'multiplier' => 1E1,
'name' =>
'deka' ),
7391 'd' => array(
'multiplier' => 1E-1,
'name' =>
'deci' ),
7392 'c' => array(
'multiplier' => 1E-2,
'name' =>
'centi' ),
7393 'm' => array(
'multiplier' => 1E-3,
'name' =>
'milli' ),
7394 'u' => array(
'multiplier' => 1E-6,
'name' =>
'micro' ),
7395 'n' => array(
'multiplier' => 1E-9,
'name' =>
'nano' ),
7396 'p' => array(
'multiplier' => 1E-12,
'name' =>
'pico' ),
7397 'f' => array(
'multiplier' => 1E-15,
'name' =>
'femto' ),
7398 'a' => array(
'multiplier' => 1E-18,
'name' =>
'atto' ),
7399 'z' => array(
'multiplier' => 1E-21,
'name' =>
'zepto' ),
7400 'y' => array(
'multiplier' => 1E-24,
'name' =>
'yocto' )
7404 'sg' => 6.85220500053478E-05,
7405 'lbm' => 2.20462291469134E-03,
7406 'u' => 6.02217000000000E+23,
7407 'ozm' => 3.52739718003627E-02
7409 'sg' => array(
'g' => 1.45938424189287E+04,
7411 'lbm' => 3.21739194101647E+01,
7412 'u' => 8.78866000000000E+27,
7413 'ozm' => 5.14782785944229E+02
7415 'lbm' => array(
'g' => 4.5359230974881148E+02,
7416 'sg' => 3.10810749306493E-02,
7418 'u' => 2.73161000000000E+26,
7419 'ozm' => 1.60000023429410E+01
7421 'u' => array(
'g' => 1.66053100460465E-24,
7422 'sg' => 1.13782988532950E-28,
7423 'lbm' => 3.66084470330684E-27,
7425 'ozm' => 5.85735238300524E-26
7427 'ozm' => array(
'g' => 2.83495152079732E+01,
7428 'sg' => 1.94256689870811E-03,
7429 'lbm' => 6.24999908478882E-02,
7430 'u' => 1.70725600000000E+25,
7434 'Distance' => array(
'm' => array(
'm' => 1.0,
7435 'mi' => 6.21371192237334E-04,
7436 'Nmi' => 5.39956803455724E-04,
7437 'in' => 3.93700787401575E+01,
7438 'ft' => 3.28083989501312E+00,
7439 'yd' => 1.09361329797891E+00,
7440 'ang' => 1.00000000000000E+10,
7441 'Pica' => 2.83464566929116E+03
7443 'mi' => array(
'm' => 1.60934400000000E+03,
7445 'Nmi' => 8.68976241900648E-01,
7446 'in' => 6.33600000000000E+04,
7447 'ft' => 5.28000000000000E+03,
7448 'yd' => 1.76000000000000E+03,
7449 'ang' => 1.60934400000000E+13,
7450 'Pica' => 4.56191999999971E+06
7452 'Nmi' => array(
'm' => 1.85200000000000E+03,
7453 'mi' => 1.15077944802354E+00,
7455 'in' => 7.29133858267717E+04,
7456 'ft' => 6.07611548556430E+03,
7457 'yd' => 2.02537182785694E+03,
7458 'ang' => 1.85200000000000E+13,
7459 'Pica' => 5.24976377952723E+06
7461 'in' => array(
'm' => 2.54000000000000E-02,
7462 'mi' => 1.57828282828283E-05,
7463 'Nmi' => 1.37149028077754E-05,
7465 'ft' => 8.33333333333333E-02,
7466 'yd' => 2.77777777686643E-02,
7467 'ang' => 2.54000000000000E+08,
7468 'Pica' => 7.19999999999955E+01
7470 'ft' => array(
'm' => 3.04800000000000E-01,
7471 'mi' => 1.89393939393939E-04,
7472 'Nmi' => 1.64578833693305E-04,
7473 'in' => 1.20000000000000E+01,
7475 'yd' => 3.33333333223972E-01,
7476 'ang' => 3.04800000000000E+09,
7477 'Pica' => 8.63999999999946E+02
7479 'yd' => array(
'm' => 9.14400000300000E-01,
7480 'mi' => 5.68181818368230E-04,
7481 'Nmi' => 4.93736501241901E-04,
7482 'in' => 3.60000000118110E+01,
7483 'ft' => 3.00000000000000E+00,
7485 'ang' => 9.14400000300000E+09,
7486 'Pica' => 2.59200000085023E+03
7488 'ang' => array(
'm' => 1.00000000000000E-10,
7489 'mi' => 6.21371192237334E-14,
7490 'Nmi' => 5.39956803455724E-14,
7491 'in' => 3.93700787401575E-09,
7492 'ft' => 3.28083989501312E-10,
7493 'yd' => 1.09361329797891E-10,
7495 'Pica' => 2.83464566929116E-07
7497 'Pica' => array(
'm' => 3.52777777777800E-04,
7498 'mi' => 2.19205948372629E-07,
7499 'Nmi' => 1.90484761219114E-07,
7500 'in' => 1.38888888888898E-02,
7501 'ft' => 1.15740740740748E-03,
7502 'yd' => 3.85802469009251E-04,
7503 'ang' => 3.52777777777800E+06,
7507 'Time' => array(
'yr' => array(
'yr' => 1.0,
7513 'day' => array(
'yr' => 2.73785078713210E-03,
7519 'hr' => array(
'yr' => 1.14077116130504E-04,
7520 'day' => 4.16666666666667E-02,
7525 'mn' => array(
'yr' => 1.90128526884174E-06,
7526 'day' => 6.94444444444444E-04,
7527 'hr' => 1.66666666666667E-02,
7531 'sec' => array(
'yr' => 3.16880878140289E-08,
7532 'day' => 1.15740740740741E-05,
7533 'hr' => 2.77777777777778E-04,
7534 'mn' => 1.66666666666667E-02,
7538 'Pressure' => array(
'Pa' => array(
'Pa' => 1.0,
7540 'atm' => 9.86923299998193E-06,
7541 'at' => 9.86923299998193E-06,
7542 'mmHg' => 7.50061707998627E-03
7544 'p' => array(
'Pa' => 1.0,
7546 'atm' => 9.86923299998193E-06,
7547 'at' => 9.86923299998193E-06,
7548 'mmHg' => 7.50061707998627E-03
7550 'atm' => array(
'Pa' => 1.01324996583000E+05,
7551 'p' => 1.01324996583000E+05,
7556 'at' => array(
'Pa' => 1.01324996583000E+05,
7557 'p' => 1.01324996583000E+05,
7562 'mmHg' => array(
'Pa' => 1.33322363925000E+02,
7563 'p' => 1.33322363925000E+02,
7564 'atm' => 1.31578947368421E-03,
7565 'at' => 1.31578947368421E-03,
7569 'Force' => array(
'N' => array(
'N' => 1.0,
7572 'lbf' => 2.24808923655339E-01
7574 'dyn' => array(
'N' => 1.0E-5,
7577 'lbf' => 2.24808923655339E-06
7579 'dy' => array(
'N' => 1.0E-5,
7582 'lbf' => 2.24808923655339E-06
7584 'lbf' => array(
'N' => 4.448222,
7585 'dyn' => 4.448222E+5,
7586 'dy' => 4.448222E+5,
7590 'Energy' => array(
'J' => array(
'J' => 1.0,
7591 'e' => 9.99999519343231E+06,
7592 'c' => 2.39006249473467E-01,
7593 'cal' => 2.38846190642017E-01,
7594 'eV' => 6.24145700000000E+18,
7595 'ev' => 6.24145700000000E+18,
7596 'HPh' => 3.72506430801000E-07,
7597 'hh' => 3.72506430801000E-07,
7598 'Wh' => 2.77777916238711E-04,
7599 'wh' => 2.77777916238711E-04,
7600 'flb' => 2.37304222192651E+01,
7601 'BTU' => 9.47815067349015E-04,
7602 'btu' => 9.47815067349015E-04
7604 'e' => array(
'J' => 1.00000048065700E-07,
7606 'c' => 2.39006364353494E-08,
7607 'cal' => 2.38846305445111E-08,
7608 'eV' => 6.24146000000000E+11,
7609 'ev' => 6.24146000000000E+11,
7610 'HPh' => 3.72506609848824E-14,
7611 'hh' => 3.72506609848824E-14,
7612 'Wh' => 2.77778049754611E-11,
7613 'wh' => 2.77778049754611E-11,
7614 'flb' => 2.37304336254586E-06,
7615 'BTU' => 9.47815522922962E-11,
7616 'btu' => 9.47815522922962E-11
7618 'c' => array(
'J' => 4.18399101363672E+00,
7619 'e' => 4.18398900257312E+07,
7621 'cal' => 9.99330315287563E-01,
7622 'eV' => 2.61142000000000E+19,
7623 'ev' => 2.61142000000000E+19,
7624 'HPh' => 1.55856355899327E-06,
7625 'hh' => 1.55856355899327E-06,
7626 'Wh' => 1.16222030532950E-03,
7627 'wh' => 1.16222030532950E-03,
7628 'flb' => 9.92878733152102E+01,
7629 'BTU' => 3.96564972437776E-03,
7630 'btu' => 3.96564972437776E-03
7632 'cal' => array(
'J' => 4.18679484613929E+00,
7633 'e' => 4.18679283372801E+07,
7634 'c' => 1.00067013349059E+00,
7636 'eV' => 2.61317000000000E+19,
7637 'ev' => 2.61317000000000E+19,
7638 'HPh' => 1.55960800463137E-06,
7639 'hh' => 1.55960800463137E-06,
7640 'Wh' => 1.16299914807955E-03,
7641 'wh' => 1.16299914807955E-03,
7642 'flb' => 9.93544094443283E+01,
7643 'BTU' => 3.96830723907002E-03,
7644 'btu' => 3.96830723907002E-03
7646 'eV' => array(
'J' => 1.60219000146921E-19,
7647 'e' => 1.60218923136574E-12,
7648 'c' => 3.82933423195043E-20,
7649 'cal' => 3.82676978535648E-20,
7652 'HPh' => 5.96826078912344E-26,
7653 'hh' => 5.96826078912344E-26,
7654 'Wh' => 4.45053000026614E-23,
7655 'wh' => 4.45053000026614E-23,
7656 'flb' => 3.80206452103492E-18,
7657 'BTU' => 1.51857982414846E-22,
7658 'btu' => 1.51857982414846E-22
7660 'ev' => array(
'J' => 1.60219000146921E-19,
7661 'e' => 1.60218923136574E-12,
7662 'c' => 3.82933423195043E-20,
7663 'cal' => 3.82676978535648E-20,
7666 'HPh' => 5.96826078912344E-26,
7667 'hh' => 5.96826078912344E-26,
7668 'Wh' => 4.45053000026614E-23,
7669 'wh' => 4.45053000026614E-23,
7670 'flb' => 3.80206452103492E-18,
7671 'BTU' => 1.51857982414846E-22,
7672 'btu' => 1.51857982414846E-22
7674 'HPh' => array(
'J' => 2.68451741316170E+06,
7675 'e' => 2.68451612283024E+13,
7676 'c' => 6.41616438565991E+05,
7677 'cal' => 6.41186757845835E+05,
7678 'eV' => 1.67553000000000E+25,
7679 'ev' => 1.67553000000000E+25,
7682 'Wh' => 7.45699653134593E+02,
7683 'wh' => 7.45699653134593E+02,
7684 'flb' => 6.37047316692964E+07,
7685 'BTU' => 2.54442605275546E+03,
7686 'btu' => 2.54442605275546E+03
7688 'hh' => array(
'J' => 2.68451741316170E+06,
7689 'e' => 2.68451612283024E+13,
7690 'c' => 6.41616438565991E+05,
7691 'cal' => 6.41186757845835E+05,
7692 'eV' => 1.67553000000000E+25,
7693 'ev' => 1.67553000000000E+25,
7696 'Wh' => 7.45699653134593E+02,
7697 'wh' => 7.45699653134593E+02,
7698 'flb' => 6.37047316692964E+07,
7699 'BTU' => 2.54442605275546E+03,
7700 'btu' => 2.54442605275546E+03
7702 'Wh' => array(
'J' => 3.59999820554720E+03,
7703 'e' => 3.59999647518369E+10,
7704 'c' => 8.60422069219046E+02,
7705 'cal' => 8.59845857713046E+02,
7706 'eV' => 2.24692340000000E+22,
7707 'ev' => 2.24692340000000E+22,
7708 'HPh' => 1.34102248243839E-03,
7709 'hh' => 1.34102248243839E-03,
7712 'flb' => 8.54294774062316E+04,
7713 'BTU' => 3.41213254164705E+00,
7714 'btu' => 3.41213254164705E+00
7716 'wh' => array(
'J' => 3.59999820554720E+03,
7717 'e' => 3.59999647518369E+10,
7718 'c' => 8.60422069219046E+02,
7719 'cal' => 8.59845857713046E+02,
7720 'eV' => 2.24692340000000E+22,
7721 'ev' => 2.24692340000000E+22,
7722 'HPh' => 1.34102248243839E-03,
7723 'hh' => 1.34102248243839E-03,
7726 'flb' => 8.54294774062316E+04,
7727 'BTU' => 3.41213254164705E+00,
7728 'btu' => 3.41213254164705E+00
7730 'flb' => array(
'J' => 4.21400003236424E-02,
7731 'e' => 4.21399800687660E+05,
7732 'c' => 1.00717234301644E-02,
7733 'cal' => 1.00649785509554E-02,
7734 'eV' => 2.63015000000000E+17,
7735 'ev' => 2.63015000000000E+17,
7736 'HPh' => 1.56974211145130E-08,
7737 'hh' => 1.56974211145130E-08,
7738 'Wh' => 1.17055614802000E-05,
7739 'wh' => 1.17055614802000E-05,
7741 'BTU' => 3.99409272448406E-05,
7742 'btu' => 3.99409272448406E-05
7744 'BTU' => array(
'J' => 1.05505813786749E+03,
7745 'e' => 1.05505763074665E+10,
7746 'c' => 2.52165488508168E+02,
7747 'cal' => 2.51996617135510E+02,
7748 'eV' => 6.58510000000000E+21,
7749 'ev' => 6.58510000000000E+21,
7750 'HPh' => 3.93015941224568E-04,
7751 'hh' => 3.93015941224568E-04,
7752 'Wh' => 2.93071851047526E-01,
7753 'wh' => 2.93071851047526E-01,
7754 'flb' => 2.50369750774671E+04,
7758 'btu' => array(
'J' => 1.05505813786749E+03,
7759 'e' => 1.05505763074665E+10,
7760 'c' => 2.52165488508168E+02,
7761 'cal' => 2.51996617135510E+02,
7762 'eV' => 6.58510000000000E+21,
7763 'ev' => 6.58510000000000E+21,
7764 'HPh' => 3.93015941224568E-04,
7765 'hh' => 3.93015941224568E-04,
7766 'Wh' => 2.93071851047526E-01,
7767 'wh' => 2.93071851047526E-01,
7768 'flb' => 2.50369750774671E+04,
7773 'Power' => array(
'HP' => array(
'HP' => 1.0,
7775 'W' => 7.45701000000000E+02,
7776 'w' => 7.45701000000000E+02
7778 'h' => array(
'HP' => 1.0,
7780 'W' => 7.45701000000000E+02,
7781 'w' => 7.45701000000000E+02
7783 'W' => array(
'HP' => 1.34102006031908E-03,
7784 'h' => 1.34102006031908E-03,
7788 'w' => array(
'HP' => 1.34102006031908E-03,
7789 'h' => 1.34102006031908E-03,
7794 'Magnetism' => array(
'T' => array(
'T' => 1.0,
7797 'ga' => array(
'T' => 0.0001,
7801 'Liquid' => array(
'tsp' => array(
'tsp' => 1.0,
7802 'tbs' => 3.33333333333333E-01,
7803 'oz' => 1.66666666666667E-01,
7804 'cup' => 2.08333333333333E-02,
7805 'pt' => 1.04166666666667E-02,
7806 'us_pt' => 1.04166666666667E-02,
7807 'uk_pt' => 8.67558516821960E-03,
7808 'qt' => 5.20833333333333E-03,
7809 'gal' => 1.30208333333333E-03,
7810 'l' => 4.92999408400710E-03,
7811 'lt' => 4.92999408400710E-03
7813 'tbs' => array(
'tsp' => 3.00000000000000E+00,
7815 'oz' => 5.00000000000000E-01,
7816 'cup' => 6.25000000000000E-02,
7817 'pt' => 3.12500000000000E-02,
7818 'us_pt' => 3.12500000000000E-02,
7819 'uk_pt' => 2.60267555046588E-02,
7820 'qt' => 1.56250000000000E-02,
7821 'gal' => 3.90625000000000E-03,
7822 'l' => 1.47899822520213E-02,
7823 'lt' => 1.47899822520213E-02
7825 'oz' => array(
'tsp' => 6.00000000000000E+00,
7826 'tbs' => 2.00000000000000E+00,
7828 'cup' => 1.25000000000000E-01,
7829 'pt' => 6.25000000000000E-02,
7830 'us_pt' => 6.25000000000000E-02,
7831 'uk_pt' => 5.20535110093176E-02,
7832 'qt' => 3.12500000000000E-02,
7833 'gal' => 7.81250000000000E-03,
7834 'l' => 2.95799645040426E-02,
7835 'lt' => 2.95799645040426E-02
7837 'cup' => array(
'tsp' => 4.80000000000000E+01,
7838 'tbs' => 1.60000000000000E+01,
7839 'oz' => 8.00000000000000E+00,
7841 'pt' => 5.00000000000000E-01,
7842 'us_pt' => 5.00000000000000E-01,
7843 'uk_pt' => 4.16428088074541E-01,
7844 'qt' => 2.50000000000000E-01,
7845 'gal' => 6.25000000000000E-02,
7846 'l' => 2.36639716032341E-01,
7847 'lt' => 2.36639716032341E-01
7849 'pt' => array(
'tsp' => 9.60000000000000E+01,
7850 'tbs' => 3.20000000000000E+01,
7851 'oz' => 1.60000000000000E+01,
7852 'cup' => 2.00000000000000E+00,
7855 'uk_pt' => 8.32856176149081E-01,
7856 'qt' => 5.00000000000000E-01,
7857 'gal' => 1.25000000000000E-01,
7858 'l' => 4.73279432064682E-01,
7859 'lt' => 4.73279432064682E-01
7861 'us_pt' => array(
'tsp' => 9.60000000000000E+01,
7862 'tbs' => 3.20000000000000E+01,
7863 'oz' => 1.60000000000000E+01,
7864 'cup' => 2.00000000000000E+00,
7867 'uk_pt' => 8.32856176149081E-01,
7868 'qt' => 5.00000000000000E-01,
7869 'gal' => 1.25000000000000E-01,
7870 'l' => 4.73279432064682E-01,
7871 'lt' => 4.73279432064682E-01
7873 'uk_pt' => array(
'tsp' => 1.15266000000000E+02,
7874 'tbs' => 3.84220000000000E+01,
7875 'oz' => 1.92110000000000E+01,
7876 'cup' => 2.40137500000000E+00,
7877 'pt' => 1.20068750000000E+00,
7878 'us_pt' => 1.20068750000000E+00,
7880 'qt' => 6.00343750000000E-01,
7881 'gal' => 1.50085937500000E-01,
7882 'l' => 5.68260698087162E-01,
7883 'lt' => 5.68260698087162E-01
7885 'qt' => array(
'tsp' => 1.92000000000000E+02,
7886 'tbs' => 6.40000000000000E+01,
7887 'oz' => 3.20000000000000E+01,
7888 'cup' => 4.00000000000000E+00,
7889 'pt' => 2.00000000000000E+00,
7890 'us_pt' => 2.00000000000000E+00,
7891 'uk_pt' => 1.66571235229816E+00,
7893 'gal' => 2.50000000000000E-01,
7894 'l' => 9.46558864129363E-01,
7895 'lt' => 9.46558864129363E-01
7897 'gal' => array(
'tsp' => 7.68000000000000E+02,
7898 'tbs' => 2.56000000000000E+02,
7899 'oz' => 1.28000000000000E+02,
7900 'cup' => 1.60000000000000E+01,
7901 'pt' => 8.00000000000000E+00,
7902 'us_pt' => 8.00000000000000E+00,
7903 'uk_pt' => 6.66284940919265E+00,
7904 'qt' => 4.00000000000000E+00,
7906 'l' => 3.78623545651745E+00,
7907 'lt' => 3.78623545651745E+00
7909 'l' => array(
'tsp' => 2.02840000000000E+02,
7910 'tbs' => 6.76133333333333E+01,
7911 'oz' => 3.38066666666667E+01,
7912 'cup' => 4.22583333333333E+00,
7913 'pt' => 2.11291666666667E+00,
7914 'us_pt' => 2.11291666666667E+00,
7915 'uk_pt' => 1.75975569552166E+00,
7916 'qt' => 1.05645833333333E+00,
7917 'gal' => 2.64114583333333E-01,
7921 'lt' => array(
'tsp' => 2.02840000000000E+02,
7922 'tbs' => 6.76133333333333E+01,
7923 'oz' => 3.38066666666667E+01,
7924 'cup' => 4.22583333333333E+00,
7925 'pt' => 2.11291666666667E+00,
7926 'us_pt' => 2.11291666666667E+00,
7927 'uk_pt' => 1.75975569552166E+00,
7928 'qt' => 1.05645833333333E+00,
7929 'gal' => 2.64114583333333E-01,
7943 $conversionGroups = array();
7944 foreach(self::$_conversionUnits as $conversionUnit) {
7945 $conversionGroups[] = $conversionUnit[
'Group'];
7947 return array_merge(array_unique($conversionGroups));
7957 $conversionGroups = array();
7958 foreach(self::$_conversionUnits as $conversionUnit => $conversionGroup) {
7959 if ((is_null($group)) || ($conversionGroup[
'Group'] == $group)) {
7960 $conversionGroups[$conversionGroup[
'Group']][] = $conversionUnit;
7963 return $conversionGroups;
7973 $conversionGroups = array();
7974 foreach(self::$_conversionUnits as $conversionUnit => $conversionGroup) {
7975 if ((is_null($group)) || ($conversionGroup[
'Group'] == $group)) {
7976 $conversionGroups[$conversionGroup[
'Group']][] = array(
'unit' => $conversionUnit,
7977 'description' => $conversionGroup[
'Unit Name']
7981 return $conversionGroups;
8008 if (!is_numeric($value)) {
8009 return self::$_errorCodes[
'value'];
8011 $fromMultiplier = 1;
8012 if (isset(self::$_conversionUnits[$fromUOM])) {
8013 $unitGroup1 = self::$_conversionUnits[$fromUOM][
'Group'];
8015 $fromMultiplier = substr($fromUOM,0,1);
8016 $fromUOM = substr($fromUOM,1);
8017 if (isset(self::$_conversionMultipliers[$fromMultiplier])) {
8018 $fromMultiplier = self::$_conversionMultipliers[$fromMultiplier][
'multiplier'];
8020 return self::$_errorCodes[
'na'];
8022 if ((isset(self::$_conversionUnits[$fromUOM])) && (self::$_conversionUnits[$fromUOM][
'AllowPrefix'])) {
8023 $unitGroup1 = self::$_conversionUnits[$fromUOM][
'Group'];
8025 return self::$_errorCodes[
'na'];
8028 $value *= $fromMultiplier;
8031 if (isset(self::$_conversionUnits[$toUOM])) {
8032 $unitGroup2 = self::$_conversionUnits[$toUOM][
'Group'];
8034 $toMultiplier = substr($toUOM,0,1);
8035 $toUOM = substr($toUOM,1);
8036 if (isset(self::$_conversionMultipliers[$toMultiplier])) {
8037 $toMultiplier = self::$_conversionMultipliers[$toMultiplier][
'multiplier'];
8039 return self::$_errorCodes[
'na'];
8041 if ((isset(self::$_conversionUnits[$toUOM])) && (self::$_conversionUnits[$toUOM][
'AllowPrefix'])) {
8042 $unitGroup2 = self::$_conversionUnits[$toUOM][
'Group'];
8044 return self::$_errorCodes[
'na'];
8047 if ($unitGroup1 != $unitGroup2) {
8048 return self::$_errorCodes[
'na'];
8051 if ($fromUOM == $toUOM) {
8053 }
elseif ($unitGroup1 ==
'Temperature') {
8054 if (($fromUOM ==
'F') || ($fromUOM ==
'fah')) {
8055 if (($toUOM ==
'F') || ($toUOM ==
'fah')) {
8058 $value = (($value - 32) / 1.8);
8059 if (($toUOM ==
'K') || ($toUOM ==
'kel')) {
8064 }
elseif ((($fromUOM ==
'K') || ($fromUOM ==
'kel')) &&
8065 (($toUOM ==
'K') || ($toUOM ==
'kel'))) {
8067 }
elseif ((($fromUOM ==
'C') || ($fromUOM ==
'cel')) &&
8068 (($toUOM ==
'C') || ($toUOM ==
'cel'))) {
8071 if (($toUOM ==
'F') || ($toUOM ==
'fah')) {
8072 if (($fromUOM ==
'K') || ($fromUOM ==
'kel')) {
8075 return ($value * 1.8) + 32;
8077 if (($toUOM ==
'C') || ($toUOM ==
'cel')) {
8078 return $value - 273.15;
8080 return $value + 273.15;
8082 return ($value * self::$_unitConversions[$unitGroup1][$fromUOM][$toUOM]) / $toMultiplier;
8097 $n = floor(self::flattenSingleValue(
$n));
8099 if ((is_numeric(
$x)) && (is_numeric(
$n))) {
8101 return self::$_errorCodes[
'num'];
8105 if (abs(
$x) <= 30) {
8109 $fSqrX = pow(
$x,2) / 4;
8112 $fTerm /= ($nK * ($nK +
$n));
8114 }
while ((abs($fTerm) > 1e-10) && (++$nK < 100));
8117 $fResult = exp($fXAbs) / sqrt($f_2_PI * $fXAbs);
8118 if ((
$n && 1) && (
$x < 0)) {
8119 $fResult = -$fResult;
8124 return self::$_errorCodes[
'value'];
8139 $n = floor(self::flattenSingleValue(
$n));
8141 if ((is_numeric(
$x)) && (is_numeric(
$n))) {
8143 return self::$_errorCodes[
'num'];
8145 $f_2_DIV_PI = 2 / pi();
8146 $f_PI_DIV_2 = pi() / 2;
8147 $f_PI_DIV_4 = pi() / 4;
8150 if (abs(
$x) <= 30) {
8154 $fSqrX = pow(
$x,2) / -4;
8157 $fTerm /= ($nK * ($nK +
$n));
8159 }
while ((abs($fTerm) > 1e-10) && (++$nK < 100));
8162 $fResult = sqrt($f_2_DIV_PI / $fXAbs) * cos($fXAbs -
$n * $f_PI_DIV_2 - $f_PI_DIV_4);
8163 if ((
$n && 1) && (
$x < 0)) {
8164 $fResult = -$fResult;
8169 return self::$_errorCodes[
'value'];
8175 $fNum2 = $fNum * 0.5;
8178 (-0.57721566 +
$y * (0.42278420 +
$y * (0.23069756 +
$y * (0.3488590e-1 +
$y * (0.262698e-2 +
$y *
8179 (0.10750e-3 +
$y * 0.74e-5))))));
8182 $fRet = exp(-$fNum) / sqrt($fNum) *
8183 (1.25331414 +
$y * (-0.7832358e-1 +
$y * (0.2189568e-1 +
$y * (-0.1062446e-1 +
$y *
8184 (0.587872e-2 +
$y * (-0.251540e-2 +
$y * 0.53208e-3))))));
8192 $fNum2 = $fNum * 0.5;
8195 (1 +
$y * (0.15443144 +
$y * (-0.67278579 +
$y * (-0.18156897 +
$y * (-0.1919402e-1 +
$y *
8196 (-0.110404e-2 +
$y * (-0.4686e-4))))))) / $fNum;
8199 $fRet = exp(-$fNum) / sqrt($fNum) *
8200 (1.25331414 +
$y * (0.23498619 +
$y * (-0.3655620e-1 +
$y * (0.1504268e-1 +
$y * (-0.780353e-2 +
$y *
8201 (0.325614e-2 +
$y * (-0.68245e-3)))))));
8218 $ord = floor(self::flattenSingleValue($ord));
8220 if ((is_numeric(
$x)) && (is_numeric($ord))) {
8222 return self::$_errorCodes[
'num'];
8230 default : $fTox = 2 /
$x;
8233 for (
$n = 1;
$n < $ord; ++
$n) {
8234 $fBkp = $fBkm +
$n * $fTox * $fBk;
8241 return self::$_errorCodes[
'value'];
8248 $f1 = -2957821389.0 +
$y * (7062834065.0 +
$y * (-512359803.6 +
$y * (10879881.29 +
$y * (-86327.92757 +
$y * 228.4622733))));
8249 $f2 = 40076544269.0 +
$y * (745249964.8 +
$y * (7189466.438 +
$y * (47447.26470 +
$y * (226.1030244 +
$y))));
8250 $fRet = $f1 / $f2 + 0.636619772 *
self::BESSELJ($fNum, 0) * log($fNum);
8254 $xx = $fNum - 0.785398164;
8255 $f1 = 1 +
$y * (-0.1098628627e-2 +
$y * (0.2734510407e-4 +
$y * (-0.2073370639e-5 +
$y * 0.2093887211e-6)));
8256 $f2 = -0.1562499995e-1 +
$y * (0.1430488765e-3 +
$y * (-0.6911147651e-5 +
$y * (0.7621095161e-6 +
$y * (-0.934945152e-7))));
8257 $fRet = sqrt(0.636619772 / $fNum) * (sin($xx) * $f1 + $z * cos($xx) * $f2);
8266 $f1 = $fNum * (-0.4900604943e13 +
$y * (0.1275274390e13 +
$y * (-0.5153438139e11 +
$y * (0.7349264551e9 +
$y *
8267 (-0.4237922726e7 +
$y * 0.8511937935e4)))));
8268 $f2 = 0.2499580570e14 +
$y * (0.4244419664e12 +
$y * (0.3733650367e10 +
$y * (0.2245904002e8 +
$y *
8269 (0.1020426050e6 +
$y * (0.3549632885e3 +
$y)))));
8270 $fRet = $f1 / $f2 + 0.636619772 * (
self::BESSELJ($fNum, 1) * log($fNum) - 1 / $fNum);
8274 $xx = $fNum - 2.356194491;
8275 $f1 = 1 +
$y * (0.183105e-2 +
$y * (-0.3516396496e-4 +
$y * (0.2457520174e-5 +
$y * (-0.240337019e6))));
8276 $f2 = 0.04687499995 +
$y * (-0.2002690873e-3 +
$y * (0.8449199096e-5 +
$y * (-0.88228987e-6 +
$y * 0.105787412e-6)));
8277 $fRet = sqrt(0.636619772 / $fNum) * (sin($xx) * $f1 + $z * cos($xx) * $f2);
8278 #i12430# ...but this seems to work much better.
8296 $ord = floor(self::flattenSingleValue($ord));
8298 if ((is_numeric(
$x)) && (is_numeric($ord))) {
8300 return self::$_errorCodes[
'num'];
8308 default: $fTox = 2 /
$x;
8311 for (
$n = 1;
$n < $ord; ++
$n) {
8312 $fByp =
$n * $fTox * $fBy - $fBym;
8319 return self::$_errorCodes[
'value'];
8332 public static function DELTA($a, $b=0) {
8336 return (
int) ($a == $b);
8349 public static function GESTEP($number, $step=0) {
8353 return (
int) ($number >= $step);
8364 if (abs(
$x) > 2.2) {
8371 $term *= $xsqr / $j;
8372 $sum -= $term / (2 * $j + 1);
8374 $term *= $xsqr / $j;
8375 $sum += $term / (2 * $j + 1);
8381 return self::$_two_sqrtpi * $sum;
8395 public static function ERF($lower, $upper = null) {
8399 if (is_numeric($lower)) {
8401 return self::$_errorCodes[
'num'];
8403 if (is_null($upper)) {
8406 if (is_numeric($upper)) {
8408 return self::$_errorCodes[
'num'];
8413 return self::$_errorCodes[
'value'];
8423 if (abs(
$x) < 2.2) {
8427 return 2 - self::erfc(-
$x);
8431 $d = pow(
$x,2) + 0.5;
8432 $q1 = $q2 = $b /
$d;
8444 }
while ((abs($q1 - $q2) / $q2) > self::$_rel_error);
8445 return self::$_one_sqrtpi * exp(-
$x *
$x) * $q2;
8460 if (is_numeric(
$x)) {
8462 return self::$_errorCodes[
'num'];
8466 return self::$_errorCodes[
'value'];
8481 if (function_exists(
'mb_convert_case')) {
8482 return mb_convert_case($mixedCaseString, MB_CASE_LOWER,
'UTF-8');
8484 return strtoupper($mixedCaseString);
8500 if (function_exists(
'mb_convert_case')) {
8501 return mb_convert_case($mixedCaseString, MB_CASE_UPPER,
'UTF-8');
8503 return strtoupper($mixedCaseString);
8519 if (function_exists(
'mb_convert_case')) {
8520 return mb_convert_case($mixedCaseString, MB_CASE_TITLE,
'UTF-8');
8522 return ucwords($mixedCaseString);
8539 public static function DOLLAR($value = 0, $decimals = 2) {
8544 if (!is_numeric($value) || !is_numeric($decimals)) {
8545 return self::$_errorCodes[
'num'];
8547 $decimals = floor($decimals);
8549 if ($decimals > 0) {
8550 return money_format(
'%.'.$decimals.
'n',$value);
8552 $round = pow(10,abs($decimals));
8553 if ($value < 0) { $round = 0-$round; }
8557 return substr(money_format(
'%.1n',$value),0,-2);
8572 public static function DOLLARDE($fractional_dollar = Null, $fraction = 0) {
8574 $fraction = (int)self::flattenSingleValue($fraction);
8577 if (is_null($fractional_dollar) || $fraction < 0) {
8578 return self::$_errorCodes[
'num'];
8580 if ($fraction == 0) {
8581 return self::$_errorCodes[
'divisionbyzero'];
8584 $dollars = floor($fractional_dollar);
8585 $cents = fmod($fractional_dollar,1);
8586 $cents /= $fraction;
8587 $cents *= pow(10,ceil(log10($fraction)));
8588 return $dollars + $cents;
8602 public static function DOLLARFR($decimal_dollar = Null, $fraction = 0) {
8604 $fraction = (int)self::flattenSingleValue($fraction);
8607 if (is_null($decimal_dollar) || $fraction < 0) {
8608 return self::$_errorCodes[
'num'];
8610 if ($fraction == 0) {
8611 return self::$_errorCodes[
'divisionbyzero'];
8614 $dollars = floor($decimal_dollar);
8615 $cents = fmod($decimal_dollar,1);
8616 $cents *= $fraction;
8617 $cents *= pow(10,-ceil(log10($fraction)));
8618 return $dollars + $cents;
8631 public static function EFFECT($nominal_rate = 0, $npery = 0) {
8633 $npery = (int)self::flattenSingleValue($npery);
8636 if ($nominal_rate <= 0 || $npery < 1) {
8637 return self::$_errorCodes[
'num'];
8640 return pow((1 + $nominal_rate / $npery), $npery) - 1;
8653 public static function NOMINAL($effect_rate = 0, $npery = 0) {
8655 $npery = (int)self::flattenSingleValue($npery);
8658 if ($effect_rate <= 0 || $npery < 1) {
8659 return self::$_errorCodes[
'num'];
8663 return $npery * (pow($effect_rate + 1, 1 / $npery) - 1);
8679 public static function PV($rate = 0, $nper = 0, $pmt = 0, $fv = 0,
$type = 0) {
8688 return self::$_errorCodes[
'num'];
8692 if (!is_null($rate) && $rate != 0) {
8693 return (-$pmt * (1 + $rate *
$type) * ((pow(1 + $rate, $nper) - 1) / $rate) - $fv) / pow(1 + $rate, $nper);
8695 return -$fv - $pmt * $nper;
8712 public static function FV($rate = 0, $nper = 0, $pmt = 0, $pv = 0,
$type = 0) {
8721 return self::$_errorCodes[
'num'];
8725 if (!is_null($rate) && $rate != 0) {
8726 return -$pv * pow(1 + $rate, $nper) - $pmt * (1 + $rate *
$type) * (pow(1 + $rate, $nper) - 1) / $rate;
8728 return -$pv - $pmt * $nper;
8745 public static function PMT($rate = 0, $nper = 0, $pv = 0, $fv = 0,
$type = 0) {
8754 return self::$_errorCodes[
'num'];
8758 if (!is_null($rate) && $rate != 0) {
8759 return (-$fv - $pv * pow(1 + $rate, $nper)) / (1 + $rate *
$type) / ((pow(1 + $rate, $nper) - 1) / $rate);
8761 return (-$pv - $fv) / $nper;
8778 public static function NPER($rate = 0, $pmt = 0, $pv = 0, $fv = 0,
$type = 0) {
8787 return self::$_errorCodes[
'num'];
8791 if (!is_null($rate) && $rate != 0) {
8792 if ($pmt == 0 && $pv == 0) {
8793 return self::$_errorCodes[
'num'];
8795 return log(($pmt * (1 + $rate *
$type) / $rate - $fv) / ($pv + $pmt * (1 + $rate * $type) / $rate)) / log(1 + $rate);
8798 return self::$_errorCodes[
'num'];
8800 return (-$pv -$fv) / $pmt;
8808 for ($i = 1; $i<= $per; ++$i) {
8809 $interest = (
$type && $i == 1)? 0 : -$capital * $rate;
8810 $principal = $pmt - $interest;
8811 $capital += $principal;
8813 return array($interest, $principal);
8830 public static function IPMT($rate, $per, $nper, $pv, $fv = 0,
$type = 0) {
8832 $per = (int) self::flattenSingleValue($per);
8833 $nper = (int) self::flattenSingleValue($nper);
8836 $type = (int) self::flattenSingleValue(
$type);
8840 return self::$_errorCodes[
'num'];
8842 if ($per <= 0 || $per > $nper) {
8843 return self::$_errorCodes[
'value'];
8848 return $interestAndPrincipal[0];
8868 $nper = (int) self::flattenSingleValue($nper);
8871 $end = (int) self::flattenSingleValue($end);
8872 $type = (int) self::flattenSingleValue(
$type);
8876 return self::$_errorCodes[
'num'];
8878 if ($start < 1 || $start > $end) {
8879 return self::$_errorCodes[
'value'];
8884 for ($per =
$start; $per <= $end; ++$per) {
8905 public static function PPMT($rate, $per, $nper, $pv, $fv = 0,
$type = 0) {
8907 $per = (int) self::flattenSingleValue($per);
8908 $nper = (int) self::flattenSingleValue($nper);
8911 $type = (int) self::flattenSingleValue(
$type);
8915 return self::$_errorCodes[
'num'];
8917 if ($per <= 0 || $per > $nper) {
8918 return self::$_errorCodes[
'value'];
8923 return $interestAndPrincipal[1];
8943 $nper = (int) self::flattenSingleValue($nper);
8946 $end = (int) self::flattenSingleValue($end);
8947 $type = (int) self::flattenSingleValue(
$type);
8951 return self::$_errorCodes[
'num'];
8953 if ($start < 1 || $start > $end) {
8954 return self::$_errorCodes[
'value'];
8959 for ($per =
$start; $per <= $end; ++$per) {
8976 public static function NPV() {
8984 $rate = array_shift($aArgs);
8985 for ($i = 1; $i <= count($aArgs); ++$i) {
8987 if (is_numeric($aArgs[$i - 1])) {
8988 $returnValue += $aArgs[$i - 1] / pow(1 + $rate, $i);
8993 return $returnValue;
9012 public static function DB($cost, $salvage, $life, $period, $month=12) {
9013 $cost = (float) self::flattenSingleValue($cost);
9014 $salvage = (float) self::flattenSingleValue($salvage);
9015 $life = (int) self::flattenSingleValue($life);
9016 $period = (int) self::flattenSingleValue($period);
9017 $month = (int) self::flattenSingleValue($month);
9020 if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period)) && (is_numeric($month))) {
9023 }
elseif (($cost < 0) || (($salvage / $cost) < 0) || ($life <= 0) || ($period < 1) || ($month < 1)) {
9024 return self::$_errorCodes[
'num'];
9027 $fixedDepreciationRate = 1 - pow(($salvage / $cost), (1 / $life));
9028 $fixedDepreciationRate = round($fixedDepreciationRate, 3);
9031 $previousDepreciation = 0;
9032 for ($per = 1; $per <= $period; ++$per) {
9034 $depreciation = $cost * $fixedDepreciationRate * $month / 12;
9035 }
elseif ($per == ($life + 1)) {
9036 $depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate * (12 - $month) / 12;
9038 $depreciation = ($cost - $previousDepreciation) * $fixedDepreciationRate;
9040 $previousDepreciation += $depreciation;
9042 return $depreciation;
9044 return self::$_errorCodes[
'value'];
9061 public static function DDB($cost, $salvage, $life, $period, $factor=2.0) {
9062 $cost = (float) self::flattenSingleValue($cost);
9063 $salvage = (float) self::flattenSingleValue($salvage);
9064 $life = (int) self::flattenSingleValue($life);
9065 $period = (int) self::flattenSingleValue($period);
9066 $factor = (float) self::flattenSingleValue($factor);
9069 if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period)) && (is_numeric($factor))) {
9070 if (($cost <= 0) || (($salvage / $cost) < 0) || ($life <= 0) || ($period < 1) || ($factor <= 0.0) || ($period > $life)) {
9071 return self::$_errorCodes[
'num'];
9074 $fixedDepreciationRate = 1 - pow(($salvage / $cost), (1 / $life));
9075 $fixedDepreciationRate = round($fixedDepreciationRate, 3);
9078 $previousDepreciation = 0;
9079 for ($per = 1; $per <= $period; ++$per) {
9080 $depreciation = min( ($cost - $previousDepreciation) * ($factor / $life), ($cost - $salvage - $previousDepreciation) );
9081 $previousDepreciation += $depreciation;
9083 return $depreciation;
9085 return self::$_errorCodes[
'value'];
9100 if (self::_isLeapYear(self::YEAR($year))) {
9107 return self::$_errorCodes[
'num'];
9109 return $daysPerYear;
9131 public static function ACCRINT($issue, $firstinter, $settlement, $rate, $par=1000, $frequency=1, $basis=0) {
9135 $rate = (float) self::flattenSingleValue($rate);
9141 if ((is_numeric($rate)) && (is_numeric($par))) {
9142 if (($rate <= 0) || ($par <= 0)) {
9143 return self::$_errorCodes[
'num'];
9145 $daysBetweenIssueAndSettlement =
self::YEARFRAC($issue, $settlement, $basis);
9146 if (!is_numeric($daysBetweenIssueAndSettlement)) {
9147 return $daysBetweenIssueAndSettlement;
9150 if (!is_numeric($daysPerYear)) {
9151 return $daysPerYear;
9153 $daysBetweenIssueAndSettlement *= $daysPerYear;
9155 return $par * $rate * ($daysBetweenIssueAndSettlement / $daysPerYear);
9157 return self::$_errorCodes[
'value'];
9178 public static function ACCRINTM($issue, $settlement, $rate, $par=1000, $basis=0) {
9181 $rate = (float) self::flattenSingleValue($rate);
9186 if ((is_numeric($rate)) && (is_numeric($par))) {
9187 if (($rate <= 0) || ($par <= 0)) {
9188 return self::$_errorCodes[
'num'];
9190 $daysBetweenIssueAndSettlement =
self::YEARFRAC($issue, $settlement, $basis);
9191 if (!is_numeric($daysBetweenIssueAndSettlement)) {
9192 return $daysBetweenIssueAndSettlement;
9195 if (!is_numeric($daysPerYear)) {
9196 return $daysPerYear;
9198 $daysBetweenIssueAndSettlement *= $daysPerYear;
9200 return $par * $rate * ($daysBetweenIssueAndSettlement / $daysPerYear);
9202 return self::$_errorCodes[
'value'];
9206 public static function AMORDEGRC($cost, $purchased, $firstPeriod, $salvage, $period, $rate, $basis=0) {
9210 public static function AMORLINC($cost, $purchased, $firstPeriod, $salvage, $period, $rate, $basis=0) {
9233 public static function DISC($settlement, $maturity, $price, $redemption, $basis=0) {
9236 $price = (float) self::flattenSingleValue($price);
9237 $redemption = (float) self::flattenSingleValue($redemption);
9238 $basis = (int) self::flattenSingleValue($basis);
9241 if ((is_numeric($price)) && (is_numeric($redemption)) && (is_numeric($basis))) {
9242 if (($price <= 0) || ($redemption <= 0)) {
9243 return self::$_errorCodes[
'num'];
9245 $daysBetweenSettlementAndMaturity =
self::YEARFRAC($settlement, $maturity, $basis);
9246 if (!is_numeric($daysBetweenSettlementAndMaturity)) {
9247 return $daysBetweenSettlementAndMaturity;
9250 return ((1 - $price / $redemption) / $daysBetweenSettlementAndMaturity);
9252 return self::$_errorCodes[
'value'];
9275 public static function PRICEDISC($settlement, $maturity, $discount, $redemption, $basis=0) {
9278 $discount = (float) self::flattenSingleValue($discount);
9279 $redemption = (float) self::flattenSingleValue($redemption);
9280 $basis = (int) self::flattenSingleValue($basis);
9283 if ((is_numeric($discount)) && (is_numeric($redemption)) && (is_numeric($basis))) {
9284 if (($discount <= 0) || ($redemption <= 0)) {
9285 return self::$_errorCodes[
'num'];
9287 $daysBetweenSettlementAndMaturity =
self::YEARFRAC($settlement, $maturity, $basis);
9288 if (!is_numeric($daysBetweenSettlementAndMaturity)) {
9289 return $daysBetweenSettlementAndMaturity;
9292 return $redemption * (1 - $discount * $daysBetweenSettlementAndMaturity);
9294 return self::$_errorCodes[
'value'];
9318 public static function PRICEMAT($settlement, $maturity, $issue, $rate, $yield, $basis=0) {
9324 $basis = (int) self::flattenSingleValue($basis);
9327 if (is_numeric($rate) && is_numeric($yield)) {
9328 if (($rate <= 0) || ($yield <= 0)) {
9329 return self::$_errorCodes[
'num'];
9332 if (!is_numeric($daysPerYear)) {
9333 return $daysPerYear;
9335 $daysBetweenIssueAndSettlement =
self::YEARFRAC($issue, $settlement, $basis);
9336 if (!is_numeric($daysBetweenIssueAndSettlement)) {
9337 return $daysBetweenIssueAndSettlement;
9339 $daysBetweenIssueAndSettlement *= $daysPerYear;
9340 $daysBetweenIssueAndMaturity =
self::YEARFRAC($issue, $maturity, $basis);
9341 if (!is_numeric($daysBetweenIssueAndMaturity)) {
9342 return $daysBetweenIssueAndMaturity;
9344 $daysBetweenIssueAndMaturity *= $daysPerYear;
9345 $daysBetweenSettlementAndMaturity =
self::YEARFRAC($settlement, $maturity, $basis);
9346 if (!is_numeric($daysBetweenSettlementAndMaturity)) {
9347 return $daysBetweenSettlementAndMaturity;
9349 $daysBetweenSettlementAndMaturity *= $daysPerYear;
9351 return ((100 + (($daysBetweenIssueAndMaturity / $daysPerYear) * $rate * 100)) /
9352 (1 + (($daysBetweenSettlementAndMaturity / $daysPerYear) * $yield)) -
9353 (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate * 100));
9355 return self::$_errorCodes[
'value'];
9378 public static function RECEIVED($settlement, $maturity, $investment, $discount, $basis=0) {
9381 $investment = (float) self::flattenSingleValue($investment);
9382 $discount = (float) self::flattenSingleValue($discount);
9383 $basis = (int) self::flattenSingleValue($basis);
9386 if ((is_numeric($investment)) && (is_numeric($discount)) && (is_numeric($basis))) {
9387 if (($investment <= 0) || ($discount <= 0)) {
9388 return self::$_errorCodes[
'num'];
9390 $daysBetweenSettlementAndMaturity =
self::YEARFRAC($settlement, $maturity, $basis);
9391 if (!is_numeric($daysBetweenSettlementAndMaturity)) {
9392 return $daysBetweenSettlementAndMaturity;
9395 return $investment / ( 1 - ($discount * $daysBetweenSettlementAndMaturity));
9397 return self::$_errorCodes[
'value'];
9420 public static function INTRATE($settlement, $maturity, $investment, $redemption, $basis=0) {
9423 $investment = (float) self::flattenSingleValue($investment);
9424 $redemption = (float) self::flattenSingleValue($redemption);
9425 $basis = (int) self::flattenSingleValue($basis);
9428 if ((is_numeric($investment)) && (is_numeric($redemption)) && (is_numeric($basis))) {
9429 if (($investment <= 0) || ($redemption <= 0)) {
9430 return self::$_errorCodes[
'num'];
9432 $daysBetweenSettlementAndMaturity =
self::YEARFRAC($settlement, $maturity, $basis);
9433 if (!is_numeric($daysBetweenSettlementAndMaturity)) {
9434 return $daysBetweenSettlementAndMaturity;
9437 return (($redemption / $investment) - 1) / ($daysBetweenSettlementAndMaturity);
9439 return self::$_errorCodes[
'value'];
9455 public static function TBILLEQ($settlement, $maturity, $discount) {
9462 if (is_string($testValue)) {
9466 if (is_string($maturity = self::_getDateValue($maturity))) {
9467 return self::$_errorCodes[
'value'];
9471 $daysBetweenSettlementAndMaturity =
self::YEARFRAC($settlement, $maturity);
9472 $daysBetweenSettlementAndMaturity *= 360;
9474 return (365 * $discount) / (360 - ($discount * ($daysBetweenSettlementAndMaturity)));
9490 public static function TBILLPRICE($settlement, $maturity, $discount) {
9495 if (is_string($maturity = self::_getDateValue($maturity))) {
9496 return self::$_errorCodes[
'value'];
9501 if (is_numeric($discount)) {
9502 if ($discount <= 0) {
9503 return self::$_errorCodes[
'num'];
9506 $daysBetweenSettlementAndMaturity =
self::YEARFRAC($settlement, $maturity);
9507 if (!is_numeric($daysBetweenSettlementAndMaturity)) {
9508 return $daysBetweenSettlementAndMaturity;
9510 $daysBetweenSettlementAndMaturity *= 360;
9511 if ($daysBetweenSettlementAndMaturity > 360) {
9512 return self::$_errorCodes[
'num'];
9515 $price = 100 * (1 - (($discount * $daysBetweenSettlementAndMaturity) / 360));
9517 return self::$_errorCodes[
'num'];
9521 return self::$_errorCodes[
'value'];
9537 public static function TBILLYIELD($settlement, $maturity, $price) {
9543 if (is_numeric($price)) {
9545 return self::$_errorCodes[
'num'];
9548 $daysBetweenSettlementAndMaturity =
self::YEARFRAC($settlement, $maturity);
9549 if (!is_numeric($daysBetweenSettlementAndMaturity)) {
9550 return $daysBetweenSettlementAndMaturity;
9553 $daysBetweenSettlementAndMaturity *= 360;
9555 ++$daysBetweenSettlementAndMaturity;
9556 if ($daysBetweenSettlementAndMaturity > 360) {
9557 return self::$_errorCodes[
'num'];
9560 return ((100 - $price) / $price) * (360 / $daysBetweenSettlementAndMaturity);
9562 return self::$_errorCodes[
'value'];
9576 public static function SLN($cost, $salvage, $life) {
9582 if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life))) {
9584 return self::$_errorCodes[
'num'];
9586 return ($cost - $salvage) / $life;
9588 return self::$_errorCodes[
'value'];
9612 public static function YIELDMAT($settlement, $maturity, $issue, $rate, $price, $basis=0) {
9618 $basis = (int) self::flattenSingleValue($basis);
9621 if (is_numeric($rate) && is_numeric($price)) {
9622 if (($rate <= 0) || ($price <= 0)) {
9623 return self::$_errorCodes[
'num'];
9626 if (!is_numeric($daysPerYear)) {
9627 return $daysPerYear;
9629 $daysBetweenIssueAndSettlement =
self::YEARFRAC($issue, $settlement, $basis);
9630 if (!is_numeric($daysBetweenIssueAndSettlement)) {
9631 return $daysBetweenIssueAndSettlement;
9633 $daysBetweenIssueAndSettlement *= $daysPerYear;
9634 $daysBetweenIssueAndMaturity =
self::YEARFRAC($issue, $maturity, $basis);
9635 if (!is_numeric($daysBetweenIssueAndMaturity)) {
9636 return $daysBetweenIssueAndMaturity;
9638 $daysBetweenIssueAndMaturity *= $daysPerYear;
9639 $daysBetweenSettlementAndMaturity =
self::YEARFRAC($settlement, $maturity, $basis);
9640 if (!is_numeric($daysBetweenSettlementAndMaturity)) {
9641 return $daysBetweenSettlementAndMaturity;
9643 $daysBetweenSettlementAndMaturity *= $daysPerYear;
9645 return ((1 + (($daysBetweenIssueAndMaturity / $daysPerYear) * $rate) - (($price / 100) + (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate))) /
9646 (($price / 100) + (($daysBetweenIssueAndSettlement / $daysPerYear) * $rate))) *
9647 ($daysPerYear / $daysBetweenSettlementAndMaturity);
9649 return self::$_errorCodes[
'value'];
9672 public static function YIELDDISC($settlement, $maturity, $price, $redemption, $basis=0) {
9677 $basis = (int) self::flattenSingleValue($basis);
9680 if (is_numeric($price) && is_numeric($redemption)) {
9681 if (($price <= 0) || ($redemption <= 0)) {
9682 return self::$_errorCodes[
'num'];
9685 if (!is_numeric($daysPerYear)) {
9686 return $daysPerYear;
9688 $daysBetweenSettlementAndMaturity =
self::YEARFRAC($settlement, $maturity,$basis);
9689 if (!is_numeric($daysBetweenSettlementAndMaturity)) {
9690 return $daysBetweenSettlementAndMaturity;
9692 $daysBetweenSettlementAndMaturity *= $daysPerYear;
9694 return (($redemption - $price) / $price) * ($daysPerYear / $daysBetweenSettlementAndMaturity);
9696 return self::$_errorCodes[
'value'];
9711 public static function CELL_ADDRESS(
$row, $column, $relativity=1, $referenceStyle=True, $sheetText=
'') {
9717 if ($sheetText >
'') {
9718 if (strpos($sheetText,
' ') !== False) { $sheetText =
"'".$sheetText.
"'"; }
9721 if ((!is_bool($referenceStyle)) || $referenceStyle) {
9722 $rowRelative = $columnRelative =
'$';
9724 if (($relativity == 2) || ($relativity == 4)) { $columnRelative =
''; }
9725 if (($relativity == 3) || ($relativity == 4)) { $rowRelative =
''; }
9726 return $sheetText.$columnRelative.$column.$rowRelative.$row;
9728 if (($relativity == 2) || ($relativity == 4)) { $column =
'['.$column.
']'; }
9729 if (($relativity == 3) || ($relativity == 4)) {
$row =
'['.$row.
']'; }
9730 return $sheetText.
'R'.
$row.
'C'.$column;
9735 public static function COLUMN($cellAddress=Null) {
9736 if (is_null($cellAddress) || $cellAddress ===
'') {
9740 foreach($cellAddress as $columnKey => $value) {
9746 public static function ROW($cellAddress=Null) {
9747 if ($cellAddress === Null) {
9751 foreach($cellAddress as $columnKey => $rowValue) {
9752 foreach($rowValue as $rowKey => $cellValue) {
9759 public static function OFFSET($cellAddress=Null,$rows=0,
$columns=0,$height=null,$width=null) {
9760 if ($cellAddress == Null) {
9764 foreach($cellAddress as $startColumnKey => $rowValue) {
9766 foreach($rowValue as $startRowKey => $cellValue) {
9771 foreach($cellAddress as $endColumnKey => $rowValue) {
9772 foreach($rowValue as $endRowKey => $cellValue) {
9778 $startRowKey += $rows;
9780 if ($width == null) {
9783 $endColumnIndex = $startColumnIndex + $width;
9785 if ($height == null) {
9786 $endRowKey += $rows;
9788 $endRowKey = $startRowKey + $height -1;
9791 if (($startColumnIndex < 0) || ($startRowKey <= 0)) {
9792 return self::$_errorCodes[
'reference'];
9798 $startCell = $startColumnKey.$startRowKey;
9799 $endCell = $endColumnKey.$endRowKey;
9801 if ($startCell == $endCell) {
9802 return $startColumnKey.$startRowKey;
9804 return $startColumnKey.$startRowKey.
':'.$endColumnKey.$endRowKey;
9810 $chooseArgs = func_get_args();
9812 $entryCount = count($chooseArgs) - 1;
9814 if ((is_numeric($chosenEntry)) && (!is_bool($chosenEntry))) {
9817 return self::$_errorCodes[
'value'];
9819 $chosenEntry = floor($chosenEntry);
9820 if (($chosenEntry <= 0) || ($chosenEntry > $entryCount)) {
9821 return self::$_errorCodes[
'value'];
9824 if (is_array($chooseArgs[$chosenEntry])) {
9827 return $chooseArgs[$chosenEntry];
9840 public static function MATCH($lookup_value, $lookup_array, $match_type=1) {
9849 $lookup_value = strtolower($lookup_value);
9862 if (!is_numeric($lookup_value) && !is_string($lookup_value) && !is_bool($lookup_value)){
9865 return self::$_errorCodes[
'na'];
9869 if ($match_type!==0 && $match_type!==-1 && $match_type!==1){
9872 return self::$_errorCodes[
'na'];
9876 if (
sizeof($lookup_array)<=0){
9879 return self::$_errorCodes[
'na'];
9883 for ($i=0;$i<
sizeof($lookup_array);++$i){
9885 if (!is_numeric($lookup_array[$i]) && !is_string($lookup_array[$i]) && !is_bool($lookup_array[$i])){
9888 return self::$_errorCodes[
'na'];
9891 if (is_string($lookup_array[$i]))
9892 $lookup_array[$i] = strtolower($lookup_array[$i]);
9896 if($match_type==1 || $match_type==-1){
9900 $iLastValue=$lookup_array[0];
9903 for ($i=0;$i<
sizeof($lookup_array);++$i){
9905 if(($match_type==1 && $lookup_array[$i]<$iLastValue)
9907 || ($match_type==-1 && $lookup_array[$i]>$iLastValue)){
9910 return self::$_errorCodes[
'na'];
9918 for ($i=0; $i <
sizeof($lookup_array); ++$i){
9920 if ($match_type==0 && $lookup_array[$i]==$lookup_value){
9925 if ($match_type==-1 && $lookup_array[$i] < $lookup_value){
9935 if ($match_type==1 && $lookup_array[$i] > $lookup_value){
9947 return self::$_errorCodes[
'na'];
9960 public static function INDEX($arrayValues,$rowNum = 0,$columnNum = 0) {
9962 if (($rowNum < 0) || ($columnNum < 0)) {
9963 return self::$_errorCodes[
'value'];
9966 $rowKeys = array_keys($arrayValues);
9967 $columnKeys = array_keys($arrayValues[$rowKeys[0]]);
9968 if ($columnNum > count($columnKeys)) {
9969 return self::$_errorCodes[
'value'];
9970 }
elseif ($columnNum == 0) {
9972 return $arrayValues;
9974 $rowNum = $rowKeys[--$rowNum];
9975 $returnArray = array();
9976 foreach($arrayValues as $arrayColumn) {
9977 $returnArray[] = $arrayColumn[$rowNum];
9979 return $returnArray;
9981 $columnNum = $columnKeys[--$columnNum];
9982 if ($rowNum > count($rowKeys)) {
9983 return self::$_errorCodes[
'value'];
9984 }
elseif ($rowNum == 0) {
9985 return $arrayValues[$columnNum];
9987 $rowNum = $rowKeys[--$rowNum];
9989 return $arrayValues[$rowNum][$columnNum];
10004 public static function SYD($cost, $salvage, $life, $period) {
10011 if ((is_numeric($cost)) && (is_numeric($salvage)) && (is_numeric($life)) && (is_numeric($period))) {
10012 if (($life < 1) || ($salvage < $life) || ($period > $life)) {
10013 return self::$_errorCodes[
'num'];
10015 return (($cost - $salvage) * ($life - $period + 1) * 2) / ($life * ($life + 1));
10017 return self::$_errorCodes[
'value'];
10030 $returnMatrix = array();
10031 if (!is_array($matrixData)) { $matrixData = array(array($matrixData)); }
10034 foreach($matrixData as $matrixRow) {
10036 foreach($matrixRow as $matrixCell) {
10037 $returnMatrix[$column][
$row] = $matrixCell;
10042 return $returnMatrix;
10053 public static function MMULT($matrixData1,$matrixData2) {
10054 $matrixAData = $matrixBData = array();
10055 if (!is_array($matrixData1)) { $matrixData1 = array(array($matrixData1)); }
10056 if (!is_array($matrixData2)) { $matrixData2 = array(array($matrixData2)); }
10059 foreach($matrixData1 as $matrixRow) {
10061 foreach($matrixRow as $matrixCell) {
10062 if ((is_string($matrixCell)) || ($matrixCell === null)) {
10063 return self::$_errorCodes[
'value'];
10065 $matrixAData[$rowA][$columnA] = $matrixCell;
10071 $matrixA =
new Matrix($matrixAData);
10073 foreach($matrixData2 as $matrixRow) {
10075 foreach($matrixRow as $matrixCell) {
10076 if ((is_string($matrixCell)) || ($matrixCell === null)) {
10077 return self::$_errorCodes[
'value'];
10079 $matrixBData[$rowB][$columnB] = $matrixCell;
10084 $matrixB =
new Matrix($matrixBData);
10086 if (($rowA != $columnB) || ($rowB != $columnA)) {
10087 return self::$_errorCodes[
'value'];
10090 return $matrixA->times($matrixB)->getArray();
10092 return self::$_errorCodes[
'value'];
10104 $matrixData = array();
10105 if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); }
10107 $row = $maxColumn = 0;
10108 foreach($matrixValues as $matrixRow) {
10110 foreach($matrixRow as $matrixCell) {
10111 if ((is_string($matrixCell)) || ($matrixCell === null)) {
10112 return self::$_errorCodes[
'value'];
10114 $matrixData[$column][
$row] = $matrixCell;
10117 if ($column > $maxColumn) { $maxColumn = $column; }
10120 if (
$row != $maxColumn) {
return self::$_errorCodes[
'value']; }
10123 $matrix =
new Matrix($matrixData);
10124 return $matrix->inverse()->getArray();
10126 return self::$_errorCodes[
'value'];
10138 $matrixData = array();
10139 if (!is_array($matrixValues)) { $matrixValues = array(array($matrixValues)); }
10141 $row = $maxColumn = 0;
10142 foreach($matrixValues as $matrixRow) {
10144 foreach($matrixRow as $matrixCell) {
10145 if ((is_string($matrixCell)) || ($matrixCell === null)) {
10146 return self::$_errorCodes[
'value'];
10148 $matrixData[$column][
$row] = $matrixCell;
10151 if ($column > $maxColumn) { $maxColumn = $column; }
10154 if (
$row != $maxColumn) {
return self::$_errorCodes[
'value']; }
10157 $matrix =
new Matrix($matrixData);
10158 return $matrix->det();
10160 return self::$_errorCodes[
'value'];
10171 public static function SUMX2MY2($matrixData1,$matrixData2) {
10174 $count1 = count($array1);
10175 $count2 = count($array2);
10176 if ($count1 < $count2) {
10183 for ($i = 0; $i < $count; ++$i) {
10184 if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
10185 ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
10186 $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
10200 public static function SUMX2PY2($matrixData1,$matrixData2) {
10203 $count1 = count($array1);
10204 $count2 = count($array2);
10205 if ($count1 < $count2) {
10212 for ($i = 0; $i < $count; ++$i) {
10213 if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
10214 ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
10215 $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
10229 public static function SUMXMY2($matrixData1,$matrixData2) {
10232 $count1 = count($array1);
10233 $count2 = count($array2);
10234 if ($count1 < $count2) {
10241 for ($i = 0; $i < $count; ++$i) {
10242 if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
10243 ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
10244 $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
10261 public static function VLOOKUP($lookup_value, $lookup_array, $index_number, $not_exact_match=
true) {
10263 if ($index_number < 1) {
10264 return self::$_errorCodes[
'value'];
10268 if ($index_number > count($lookup_array)) {
10269 return self::$_errorCodes[
'reference'];
10273 array_unshift($lookup_array, array());
10274 $lookup_array = array_slice(array_values($lookup_array), 1, count($lookup_array),
true);
10277 $row_number = array_search($lookup_value, $lookup_array[1]);
10280 if ($not_exact_match ==
false) {
10281 if ($row_number ===
false) {
10282 return self::$_errorCodes[
'na'];
10284 return $lookup_array[$index_number][$row_number];
10291 return self::$_errorCodes[
'na'];
10302 public static function LOOKUP($lookup_value, $lookup_vector, $result_vector=null) {
10305 if( is_null($result_vector) )
10311 $kl = key($lookup_vector);
10313 if( in_array($lookup_value, $lookup_vector[$kl]) )
10316 $k_res = array_search($lookup_value, $lookup_vector[$kl]);
10321 $ksv = key($lookup_vector[$kl]);
10322 $smallest_value = $lookup_vector[$kl][$ksv];
10324 if( $lookup_value < $smallest_value )
10326 return self::$_errorCodes[
'na'];
10330 foreach( $lookup_vector[$kl] AS $kk => $value )
10332 if( $lookup_value >= $value )
10342 $kr = key($result_vector);
10343 if( isset($result_vector[$kr][$k_res]) )
10345 return $result_vector[$kr][$k_res];
10360 if(!is_array ( $array ) ){
10361 $array = array ( $array );
10364 $arrayValues = array();
10366 foreach ($array as $value) {
10367 if (is_scalar($value)) {
10369 }
elseif (is_array($value)) {
10370 $arrayValues = array_merge($arrayValues, self::flattenArray($value));
10372 $arrayValues[] = $value;
10376 return $arrayValues;
10387 if (is_array($value)) {
10401 if (!function_exists(
'acosh')) {
10402 function acosh(
$x) {
10403 return 2 * log(sqrt((
$x + 1) / 2) + sqrt((
$x - 1) / 2));
10407 if (!function_exists(
'asinh')) {
10408 function asinh(
$x) {
10409 return log(
$x + sqrt(1 +
$x *
$x));
10413 if (!function_exists(
'atanh')) {
10414 function atanh(
$x) {
10415 return (log(1 +
$x) - log(1 -
$x)) / 2;
10419 if (!function_exists(
'money_format')) {
10420 function money_format($format, $number) {
10421 $regex = array(
'/%((?:[\^!\-]|\+|\(|\=.)*)([0-9]+)?(?:#([0-9]+))?',
10422 '(?:\.([0-9]+))?([in%])/'
10424 $regex = implode(
'', $regex);
10425 if (setlocale(LC_MONETARY, null) ==
'') {
10426 setlocale(LC_MONETARY,
'');
10428 $locale = localeconv();
10429 $number = floatval($number);
10430 if (!preg_match($regex, $format, $fmatch)) {
10431 trigger_error(
"No format specified or invalid format", E_USER_WARNING);
10434 $flags = array(
'fillchar' => preg_match(
'/\=(.)/', $fmatch[1], $match) ? $match[1] :
' ',
10435 'nogroup' => preg_match(
'/\^/', $fmatch[1]) > 0,
10436 'usesignal' => preg_match(
'/\+|\(/', $fmatch[1], $match) ? $match[0] :
'+',
10437 'nosimbol' => preg_match(
'/\!/', $fmatch[1]) > 0,
10438 'isleft' => preg_match(
'/\-/', $fmatch[1]) > 0
10440 $width = trim($fmatch[2]) ? (int)$fmatch[2] : 0;
10441 $left = trim($fmatch[3]) ? (int)$fmatch[3] : 0;
10442 $right = trim($fmatch[4]) ? (int)$fmatch[4] : $locale[
'int_frac_digits'];
10443 $conversion = $fmatch[5];
10449 $letter = $positive ?
'p' :
'n';
10450 $prefix = $suffix = $cprefix = $csuffix = $signal =
'';
10452 $signal = $locale[
'negative_sign'];
10454 case $locale[
'n_sign_posn'] == 0 || $flags[
'usesignal'] ==
'(':
10458 case $locale[
'n_sign_posn'] == 1:
10461 case $locale[
'n_sign_posn'] == 2:
10464 case $locale[
'n_sign_posn'] == 3:
10465 $cprefix = $signal;
10467 case $locale[
'n_sign_posn'] == 4:
10468 $csuffix = $signal;
10472 if (!$flags[
'nosimbol']) {
10473 $currency = $cprefix;
10474 $currency .= ($conversion ==
'i' ? $locale[
'int_curr_symbol'] : $locale[
'currency_symbol']);
10475 $currency .= $csuffix;
10476 $currency = iconv(
'ISO-8859-1',
'UTF-8',$currency);
10480 $space = $locale[
"{$letter}_sep_by_space"] ?
' ' :
'';
10482 $number = number_format($number, $right, $locale[
'mon_decimal_point'], $flags[
'nogroup'] ?
'' : $locale[
'mon_thousands_sep'] );
10483 $number = explode($locale[
'mon_decimal_point'], $number);
10485 $n = strlen($prefix) + strlen($currency);
10486 if ($left > 0 && $left >
$n) {
10487 if ($flags[
'isleft']) {
10488 $number[0] .= str_repeat($flags[
'fillchar'], $left -
$n);
10490 $number[0] = str_repeat($flags[
'fillchar'], $left -
$n) . $number[0];
10493 $number = implode($locale[
'mon_decimal_point'], $number);
10494 if ($locale[
"{$letter}_cs_precedes"]) {
10495 $number = $prefix . $currency .
$space . $number . $suffix;
10497 $number = $prefix . $number .
$space . $currency . $suffix;
10500 $number = str_pad($number, $width, $flags[
'fillchar'], $flags[
'isleft'] ? STR_PAD_RIGHT : STR_PAD_LEFT);
10502 $format = str_replace($fmatch[0], $number, $format);