MathTrig.php 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation;
  3. use Matrix\Exception as MatrixException;
  4. use Matrix\Matrix;
  5. class MathTrig
  6. {
  7. //
  8. // Private method to return an array of the factors of the input value
  9. //
  10. private static function factors($value)
  11. {
  12. $startVal = floor(sqrt($value));
  13. $factorArray = [];
  14. for ($i = $startVal; $i > 1; --$i) {
  15. if (($value % $i) == 0) {
  16. $factorArray = array_merge($factorArray, self::factors($value / $i));
  17. $factorArray = array_merge($factorArray, self::factors($i));
  18. if ($i <= sqrt($value)) {
  19. break;
  20. }
  21. }
  22. }
  23. if (!empty($factorArray)) {
  24. rsort($factorArray);
  25. return $factorArray;
  26. }
  27. return [(int) $value];
  28. }
  29. private static function romanCut($num, $n)
  30. {
  31. return ($num - ($num % $n)) / $n;
  32. }
  33. /**
  34. * ATAN2.
  35. *
  36. * This function calculates the arc tangent of the two variables x and y. It is similar to
  37. * calculating the arc tangent of y ÷ x, except that the signs of both arguments are used
  38. * to determine the quadrant of the result.
  39. * The arctangent is the angle from the x-axis to a line containing the origin (0, 0) and a
  40. * point with coordinates (xCoordinate, yCoordinate). The angle is given in radians between
  41. * -pi and pi, excluding -pi.
  42. *
  43. * Note that the Excel ATAN2() function accepts its arguments in the reverse order to the standard
  44. * PHP atan2() function, so we need to reverse them here before calling the PHP atan() function.
  45. *
  46. * Excel Function:
  47. * ATAN2(xCoordinate,yCoordinate)
  48. *
  49. * @category Mathematical and Trigonometric Functions
  50. *
  51. * @param float $xCoordinate the x-coordinate of the point
  52. * @param float $yCoordinate the y-coordinate of the point
  53. *
  54. * @return float the inverse tangent of the specified x- and y-coordinates
  55. */
  56. public static function ATAN2($xCoordinate = null, $yCoordinate = null)
  57. {
  58. $xCoordinate = Functions::flattenSingleValue($xCoordinate);
  59. $yCoordinate = Functions::flattenSingleValue($yCoordinate);
  60. $xCoordinate = ($xCoordinate !== null) ? $xCoordinate : 0.0;
  61. $yCoordinate = ($yCoordinate !== null) ? $yCoordinate : 0.0;
  62. if (((is_numeric($xCoordinate)) || (is_bool($xCoordinate))) &&
  63. ((is_numeric($yCoordinate))) || (is_bool($yCoordinate))) {
  64. $xCoordinate = (float) $xCoordinate;
  65. $yCoordinate = (float) $yCoordinate;
  66. if (($xCoordinate == 0) && ($yCoordinate == 0)) {
  67. return Functions::DIV0();
  68. }
  69. return atan2($yCoordinate, $xCoordinate);
  70. }
  71. return Functions::VALUE();
  72. }
  73. /**
  74. * CEILING.
  75. *
  76. * Returns number rounded up, away from zero, to the nearest multiple of significance.
  77. * For example, if you want to avoid using pennies in your prices and your product is
  78. * priced at $4.42, use the formula =CEILING(4.42,0.05) to round prices up to the
  79. * nearest nickel.
  80. *
  81. * Excel Function:
  82. * CEILING(number[,significance])
  83. *
  84. * @category Mathematical and Trigonometric Functions
  85. *
  86. * @param float $number the number you want to round
  87. * @param float $significance the multiple to which you want to round
  88. *
  89. * @return float Rounded Number
  90. */
  91. public static function CEILING($number, $significance = null)
  92. {
  93. $number = Functions::flattenSingleValue($number);
  94. $significance = Functions::flattenSingleValue($significance);
  95. if (($significance === null) &&
  96. (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC)) {
  97. $significance = $number / abs($number);
  98. }
  99. if ((is_numeric($number)) && (is_numeric($significance))) {
  100. if (($number == 0.0) || ($significance == 0.0)) {
  101. return 0.0;
  102. } elseif (self::SIGN($number) == self::SIGN($significance)) {
  103. return ceil($number / $significance) * $significance;
  104. }
  105. return Functions::NAN();
  106. }
  107. return Functions::VALUE();
  108. }
  109. /**
  110. * COMBIN.
  111. *
  112. * Returns the number of combinations for a given number of items. Use COMBIN to
  113. * determine the total possible number of groups for a given number of items.
  114. *
  115. * Excel Function:
  116. * COMBIN(numObjs,numInSet)
  117. *
  118. * @category Mathematical and Trigonometric Functions
  119. *
  120. * @param int $numObjs Number of different objects
  121. * @param int $numInSet Number of objects in each combination
  122. *
  123. * @return int Number of combinations
  124. */
  125. public static function COMBIN($numObjs, $numInSet)
  126. {
  127. $numObjs = Functions::flattenSingleValue($numObjs);
  128. $numInSet = Functions::flattenSingleValue($numInSet);
  129. if ((is_numeric($numObjs)) && (is_numeric($numInSet))) {
  130. if ($numObjs < $numInSet) {
  131. return Functions::NAN();
  132. } elseif ($numInSet < 0) {
  133. return Functions::NAN();
  134. }
  135. return round(self::FACT($numObjs) / self::FACT($numObjs - $numInSet)) / self::FACT($numInSet);
  136. }
  137. return Functions::VALUE();
  138. }
  139. /**
  140. * EVEN.
  141. *
  142. * Returns number rounded up to the nearest even integer.
  143. * You can use this function for processing items that come in twos. For example,
  144. * a packing crate accepts rows of one or two items. The crate is full when
  145. * the number of items, rounded up to the nearest two, matches the crate's
  146. * capacity.
  147. *
  148. * Excel Function:
  149. * EVEN(number)
  150. *
  151. * @category Mathematical and Trigonometric Functions
  152. *
  153. * @param float $number Number to round
  154. *
  155. * @return int Rounded Number
  156. */
  157. public static function EVEN($number)
  158. {
  159. $number = Functions::flattenSingleValue($number);
  160. if ($number === null) {
  161. return 0;
  162. } elseif (is_bool($number)) {
  163. $number = (int) $number;
  164. }
  165. if (is_numeric($number)) {
  166. $significance = 2 * self::SIGN($number);
  167. return (int) self::CEILING($number, $significance);
  168. }
  169. return Functions::VALUE();
  170. }
  171. /**
  172. * FACT.
  173. *
  174. * Returns the factorial of a number.
  175. * The factorial of a number is equal to 1*2*3*...* number.
  176. *
  177. * Excel Function:
  178. * FACT(factVal)
  179. *
  180. * @category Mathematical and Trigonometric Functions
  181. *
  182. * @param float $factVal Factorial Value
  183. *
  184. * @return int Factorial
  185. */
  186. public static function FACT($factVal)
  187. {
  188. $factVal = Functions::flattenSingleValue($factVal);
  189. if (is_numeric($factVal)) {
  190. if ($factVal < 0) {
  191. return Functions::NAN();
  192. }
  193. $factLoop = floor($factVal);
  194. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
  195. if ($factVal > $factLoop) {
  196. return Functions::NAN();
  197. }
  198. }
  199. $factorial = 1;
  200. while ($factLoop > 1) {
  201. $factorial *= $factLoop--;
  202. }
  203. return $factorial;
  204. }
  205. return Functions::VALUE();
  206. }
  207. /**
  208. * FACTDOUBLE.
  209. *
  210. * Returns the double factorial of a number.
  211. *
  212. * Excel Function:
  213. * FACTDOUBLE(factVal)
  214. *
  215. * @category Mathematical and Trigonometric Functions
  216. *
  217. * @param float $factVal Factorial Value
  218. *
  219. * @return int Double Factorial
  220. */
  221. public static function FACTDOUBLE($factVal)
  222. {
  223. $factLoop = Functions::flattenSingleValue($factVal);
  224. if (is_numeric($factLoop)) {
  225. $factLoop = floor($factLoop);
  226. if ($factVal < 0) {
  227. return Functions::NAN();
  228. }
  229. $factorial = 1;
  230. while ($factLoop > 1) {
  231. $factorial *= $factLoop--;
  232. --$factLoop;
  233. }
  234. return $factorial;
  235. }
  236. return Functions::VALUE();
  237. }
  238. /**
  239. * FLOOR.
  240. *
  241. * Rounds number down, toward zero, to the nearest multiple of significance.
  242. *
  243. * Excel Function:
  244. * FLOOR(number[,significance])
  245. *
  246. * @category Mathematical and Trigonometric Functions
  247. *
  248. * @param float $number Number to round
  249. * @param float $significance Significance
  250. *
  251. * @return float Rounded Number
  252. */
  253. public static function FLOOR($number, $significance = null)
  254. {
  255. $number = Functions::flattenSingleValue($number);
  256. $significance = Functions::flattenSingleValue($significance);
  257. if (($significance === null) &&
  258. (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC)) {
  259. $significance = $number / abs($number);
  260. }
  261. if ((is_numeric($number)) && (is_numeric($significance))) {
  262. if ($significance == 0.0) {
  263. return Functions::DIV0();
  264. } elseif ($number == 0.0) {
  265. return 0.0;
  266. } elseif (self::SIGN($number) == self::SIGN($significance)) {
  267. return floor($number / $significance) * $significance;
  268. }
  269. return Functions::NAN();
  270. }
  271. return Functions::VALUE();
  272. }
  273. private static function evaluateGCD($a, $b)
  274. {
  275. return $b ? self::evaluateGCD($b, $a % $b) : $a;
  276. }
  277. /**
  278. * GCD.
  279. *
  280. * Returns the greatest common divisor of a series of numbers.
  281. * The greatest common divisor is the largest integer that divides both
  282. * number1 and number2 without a remainder.
  283. *
  284. * Excel Function:
  285. * GCD(number1[,number2[, ...]])
  286. *
  287. * @category Mathematical and Trigonometric Functions
  288. *
  289. * @param mixed ...$args Data values
  290. *
  291. * @return int Greatest Common Divisor
  292. */
  293. public static function GCD(...$args)
  294. {
  295. $args = Functions::flattenArray($args);
  296. // Loop through arguments
  297. foreach (Functions::flattenArray($args) as $value) {
  298. if (!is_numeric($value)) {
  299. return Functions::VALUE();
  300. } elseif ($value < 0) {
  301. return Functions::NAN();
  302. }
  303. }
  304. $gcd = (int) array_pop($args);
  305. do {
  306. $gcd = self::evaluateGCD($gcd, (int) array_pop($args));
  307. } while (!empty($args));
  308. return $gcd;
  309. }
  310. /**
  311. * INT.
  312. *
  313. * Casts a floating point value to an integer
  314. *
  315. * Excel Function:
  316. * INT(number)
  317. *
  318. * @category Mathematical and Trigonometric Functions
  319. *
  320. * @param float $number Number to cast to an integer
  321. *
  322. * @return int Integer value
  323. */
  324. public static function INT($number)
  325. {
  326. $number = Functions::flattenSingleValue($number);
  327. if ($number === null) {
  328. return 0;
  329. } elseif (is_bool($number)) {
  330. return (int) $number;
  331. }
  332. if (is_numeric($number)) {
  333. return (int) floor($number);
  334. }
  335. return Functions::VALUE();
  336. }
  337. /**
  338. * LCM.
  339. *
  340. * Returns the lowest common multiplier of a series of numbers
  341. * The least common multiple is the smallest positive integer that is a multiple
  342. * of all integer arguments number1, number2, and so on. Use LCM to add fractions
  343. * with different denominators.
  344. *
  345. * Excel Function:
  346. * LCM(number1[,number2[, ...]])
  347. *
  348. * @category Mathematical and Trigonometric Functions
  349. *
  350. * @param mixed ...$args Data values
  351. *
  352. * @return int Lowest Common Multiplier
  353. */
  354. public static function LCM(...$args)
  355. {
  356. $returnValue = 1;
  357. $allPoweredFactors = [];
  358. // Loop through arguments
  359. foreach (Functions::flattenArray($args) as $value) {
  360. if (!is_numeric($value)) {
  361. return Functions::VALUE();
  362. }
  363. if ($value == 0) {
  364. return 0;
  365. } elseif ($value < 0) {
  366. return Functions::NAN();
  367. }
  368. $myFactors = self::factors(floor($value));
  369. $myCountedFactors = array_count_values($myFactors);
  370. $myPoweredFactors = [];
  371. foreach ($myCountedFactors as $myCountedFactor => $myCountedPower) {
  372. $myPoweredFactors[$myCountedFactor] = pow($myCountedFactor, $myCountedPower);
  373. }
  374. foreach ($myPoweredFactors as $myPoweredValue => $myPoweredFactor) {
  375. if (isset($allPoweredFactors[$myPoweredValue])) {
  376. if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) {
  377. $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
  378. }
  379. } else {
  380. $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
  381. }
  382. }
  383. }
  384. foreach ($allPoweredFactors as $allPoweredFactor) {
  385. $returnValue *= (int) $allPoweredFactor;
  386. }
  387. return $returnValue;
  388. }
  389. /**
  390. * LOG_BASE.
  391. *
  392. * Returns the logarithm of a number to a specified base. The default base is 10.
  393. *
  394. * Excel Function:
  395. * LOG(number[,base])
  396. *
  397. * @category Mathematical and Trigonometric Functions
  398. *
  399. * @param float $number The positive real number for which you want the logarithm
  400. * @param float $base The base of the logarithm. If base is omitted, it is assumed to be 10.
  401. *
  402. * @return float
  403. */
  404. public static function logBase($number = null, $base = 10)
  405. {
  406. $number = Functions::flattenSingleValue($number);
  407. $base = ($base === null) ? 10 : (float) Functions::flattenSingleValue($base);
  408. if ((!is_numeric($base)) || (!is_numeric($number))) {
  409. return Functions::VALUE();
  410. }
  411. if (($base <= 0) || ($number <= 0)) {
  412. return Functions::NAN();
  413. }
  414. return log($number, $base);
  415. }
  416. /**
  417. * MDETERM.
  418. *
  419. * Returns the matrix determinant of an array.
  420. *
  421. * Excel Function:
  422. * MDETERM(array)
  423. *
  424. * @category Mathematical and Trigonometric Functions
  425. *
  426. * @param array $matrixValues A matrix of values
  427. *
  428. * @return float
  429. */
  430. public static function MDETERM($matrixValues)
  431. {
  432. $matrixData = [];
  433. if (!is_array($matrixValues)) {
  434. $matrixValues = [[$matrixValues]];
  435. }
  436. $row = $maxColumn = 0;
  437. foreach ($matrixValues as $matrixRow) {
  438. if (!is_array($matrixRow)) {
  439. $matrixRow = [$matrixRow];
  440. }
  441. $column = 0;
  442. foreach ($matrixRow as $matrixCell) {
  443. if ((is_string($matrixCell)) || ($matrixCell === null)) {
  444. return Functions::VALUE();
  445. }
  446. $matrixData[$row][$column] = $matrixCell;
  447. ++$column;
  448. }
  449. if ($column > $maxColumn) {
  450. $maxColumn = $column;
  451. }
  452. ++$row;
  453. }
  454. $matrix = new Matrix($matrixData);
  455. if (!$matrix->isSquare()) {
  456. return Functions::VALUE();
  457. }
  458. try {
  459. return $matrix->determinant();
  460. } catch (MatrixException $ex) {
  461. return Functions::VALUE();
  462. }
  463. }
  464. /**
  465. * MINVERSE.
  466. *
  467. * Returns the inverse matrix for the matrix stored in an array.
  468. *
  469. * Excel Function:
  470. * MINVERSE(array)
  471. *
  472. * @category Mathematical and Trigonometric Functions
  473. *
  474. * @param array $matrixValues A matrix of values
  475. *
  476. * @return array
  477. */
  478. public static function MINVERSE($matrixValues)
  479. {
  480. $matrixData = [];
  481. if (!is_array($matrixValues)) {
  482. $matrixValues = [[$matrixValues]];
  483. }
  484. $row = $maxColumn = 0;
  485. foreach ($matrixValues as $matrixRow) {
  486. if (!is_array($matrixRow)) {
  487. $matrixRow = [$matrixRow];
  488. }
  489. $column = 0;
  490. foreach ($matrixRow as $matrixCell) {
  491. if ((is_string($matrixCell)) || ($matrixCell === null)) {
  492. return Functions::VALUE();
  493. }
  494. $matrixData[$row][$column] = $matrixCell;
  495. ++$column;
  496. }
  497. if ($column > $maxColumn) {
  498. $maxColumn = $column;
  499. }
  500. ++$row;
  501. }
  502. $matrix = new Matrix($matrixData);
  503. if (!$matrix->isSquare()) {
  504. return Functions::VALUE();
  505. }
  506. if ($matrix->determinant() == 0.0) {
  507. return Functions::NAN();
  508. }
  509. try {
  510. return $matrix->inverse()->toArray();
  511. } catch (MatrixException $ex) {
  512. return Functions::VALUE();
  513. }
  514. }
  515. /**
  516. * MMULT.
  517. *
  518. * @param array $matrixData1 A matrix of values
  519. * @param array $matrixData2 A matrix of values
  520. *
  521. * @return array
  522. */
  523. public static function MMULT($matrixData1, $matrixData2)
  524. {
  525. $matrixAData = $matrixBData = [];
  526. if (!is_array($matrixData1)) {
  527. $matrixData1 = [[$matrixData1]];
  528. }
  529. if (!is_array($matrixData2)) {
  530. $matrixData2 = [[$matrixData2]];
  531. }
  532. try {
  533. $rowA = 0;
  534. foreach ($matrixData1 as $matrixRow) {
  535. if (!is_array($matrixRow)) {
  536. $matrixRow = [$matrixRow];
  537. }
  538. $columnA = 0;
  539. foreach ($matrixRow as $matrixCell) {
  540. if ((!is_numeric($matrixCell)) || ($matrixCell === null)) {
  541. return Functions::VALUE();
  542. }
  543. $matrixAData[$rowA][$columnA] = $matrixCell;
  544. ++$columnA;
  545. }
  546. ++$rowA;
  547. }
  548. $matrixA = new Matrix($matrixAData);
  549. $rowB = 0;
  550. foreach ($matrixData2 as $matrixRow) {
  551. if (!is_array($matrixRow)) {
  552. $matrixRow = [$matrixRow];
  553. }
  554. $columnB = 0;
  555. foreach ($matrixRow as $matrixCell) {
  556. if ((!is_numeric($matrixCell)) || ($matrixCell === null)) {
  557. return Functions::VALUE();
  558. }
  559. $matrixBData[$rowB][$columnB] = $matrixCell;
  560. ++$columnB;
  561. }
  562. ++$rowB;
  563. }
  564. $matrixB = new Matrix($matrixBData);
  565. if ($columnA != $rowB) {
  566. return Functions::VALUE();
  567. }
  568. return $matrixA->multiply($matrixB)->toArray();
  569. } catch (MatrixException $ex) {
  570. return Functions::VALUE();
  571. }
  572. }
  573. /**
  574. * MOD.
  575. *
  576. * @param int $a Dividend
  577. * @param int $b Divisor
  578. *
  579. * @return int Remainder
  580. */
  581. public static function MOD($a = 1, $b = 1)
  582. {
  583. $a = (float) Functions::flattenSingleValue($a);
  584. $b = (float) Functions::flattenSingleValue($b);
  585. if ($b == 0.0) {
  586. return Functions::DIV0();
  587. } elseif (($a < 0.0) && ($b > 0.0)) {
  588. return $b - fmod(abs($a), $b);
  589. } elseif (($a > 0.0) && ($b < 0.0)) {
  590. return $b + fmod($a, abs($b));
  591. }
  592. return fmod($a, $b);
  593. }
  594. /**
  595. * MROUND.
  596. *
  597. * Rounds a number to the nearest multiple of a specified value
  598. *
  599. * @param float $number Number to round
  600. * @param int $multiple Multiple to which you want to round $number
  601. *
  602. * @return float Rounded Number
  603. */
  604. public static function MROUND($number, $multiple)
  605. {
  606. $number = Functions::flattenSingleValue($number);
  607. $multiple = Functions::flattenSingleValue($multiple);
  608. if ((is_numeric($number)) && (is_numeric($multiple))) {
  609. if ($multiple == 0) {
  610. return 0;
  611. }
  612. if ((self::SIGN($number)) == (self::SIGN($multiple))) {
  613. $multiplier = 1 / $multiple;
  614. return round($number * $multiplier) / $multiplier;
  615. }
  616. return Functions::NAN();
  617. }
  618. return Functions::VALUE();
  619. }
  620. /**
  621. * MULTINOMIAL.
  622. *
  623. * Returns the ratio of the factorial of a sum of values to the product of factorials.
  624. *
  625. * @param array of mixed Data Series
  626. *
  627. * @return float
  628. */
  629. public static function MULTINOMIAL(...$args)
  630. {
  631. $summer = 0;
  632. $divisor = 1;
  633. // Loop through arguments
  634. foreach (Functions::flattenArray($args) as $arg) {
  635. // Is it a numeric value?
  636. if (is_numeric($arg)) {
  637. if ($arg < 1) {
  638. return Functions::NAN();
  639. }
  640. $summer += floor($arg);
  641. $divisor *= self::FACT($arg);
  642. } else {
  643. return Functions::VALUE();
  644. }
  645. }
  646. // Return
  647. if ($summer > 0) {
  648. $summer = self::FACT($summer);
  649. return $summer / $divisor;
  650. }
  651. return 0;
  652. }
  653. /**
  654. * ODD.
  655. *
  656. * Returns number rounded up to the nearest odd integer.
  657. *
  658. * @param float $number Number to round
  659. *
  660. * @return int Rounded Number
  661. */
  662. public static function ODD($number)
  663. {
  664. $number = Functions::flattenSingleValue($number);
  665. if ($number === null) {
  666. return 1;
  667. } elseif (is_bool($number)) {
  668. return 1;
  669. } elseif (is_numeric($number)) {
  670. $significance = self::SIGN($number);
  671. if ($significance == 0) {
  672. return 1;
  673. }
  674. $result = self::CEILING($number, $significance);
  675. if ($result == self::EVEN($result)) {
  676. $result += $significance;
  677. }
  678. return (int) $result;
  679. }
  680. return Functions::VALUE();
  681. }
  682. /**
  683. * POWER.
  684. *
  685. * Computes x raised to the power y.
  686. *
  687. * @param float $x
  688. * @param float $y
  689. *
  690. * @return float
  691. */
  692. public static function POWER($x = 0, $y = 2)
  693. {
  694. $x = Functions::flattenSingleValue($x);
  695. $y = Functions::flattenSingleValue($y);
  696. // Validate parameters
  697. if ($x == 0.0 && $y == 0.0) {
  698. return Functions::NAN();
  699. } elseif ($x == 0.0 && $y < 0.0) {
  700. return Functions::DIV0();
  701. }
  702. // Return
  703. $result = pow($x, $y);
  704. return (!is_nan($result) && !is_infinite($result)) ? $result : Functions::NAN();
  705. }
  706. /**
  707. * PRODUCT.
  708. *
  709. * PRODUCT returns the product of all the values and cells referenced in the argument list.
  710. *
  711. * Excel Function:
  712. * PRODUCT(value1[,value2[, ...]])
  713. *
  714. * @category Mathematical and Trigonometric Functions
  715. *
  716. * @param mixed ...$args Data values
  717. *
  718. * @return float
  719. */
  720. public static function PRODUCT(...$args)
  721. {
  722. // Return value
  723. $returnValue = null;
  724. // Loop through arguments
  725. foreach (Functions::flattenArray($args) as $arg) {
  726. // Is it a numeric value?
  727. if ((is_numeric($arg)) && (!is_string($arg))) {
  728. if ($returnValue === null) {
  729. $returnValue = $arg;
  730. } else {
  731. $returnValue *= $arg;
  732. }
  733. }
  734. }
  735. // Return
  736. if ($returnValue === null) {
  737. return 0;
  738. }
  739. return $returnValue;
  740. }
  741. /**
  742. * QUOTIENT.
  743. *
  744. * QUOTIENT function returns the integer portion of a division. Numerator is the divided number
  745. * and denominator is the divisor.
  746. *
  747. * Excel Function:
  748. * QUOTIENT(value1[,value2[, ...]])
  749. *
  750. * @category Mathematical and Trigonometric Functions
  751. *
  752. * @param mixed ...$args Data values
  753. *
  754. * @return float
  755. */
  756. public static function QUOTIENT(...$args)
  757. {
  758. // Return value
  759. $returnValue = null;
  760. // Loop through arguments
  761. foreach (Functions::flattenArray($args) as $arg) {
  762. // Is it a numeric value?
  763. if ((is_numeric($arg)) && (!is_string($arg))) {
  764. if ($returnValue === null) {
  765. $returnValue = ($arg == 0) ? 0 : $arg;
  766. } else {
  767. if (($returnValue == 0) || ($arg == 0)) {
  768. $returnValue = 0;
  769. } else {
  770. $returnValue /= $arg;
  771. }
  772. }
  773. }
  774. }
  775. // Return
  776. return (int) $returnValue;
  777. }
  778. /**
  779. * RAND.
  780. *
  781. * @param int $min Minimal value
  782. * @param int $max Maximal value
  783. *
  784. * @return int Random number
  785. */
  786. public static function RAND($min = 0, $max = 0)
  787. {
  788. $min = Functions::flattenSingleValue($min);
  789. $max = Functions::flattenSingleValue($max);
  790. if ($min == 0 && $max == 0) {
  791. return (mt_rand(0, 10000000)) / 10000000;
  792. }
  793. return mt_rand($min, $max);
  794. }
  795. public static function ROMAN($aValue, $style = 0)
  796. {
  797. $aValue = Functions::flattenSingleValue($aValue);
  798. $style = ($style === null) ? 0 : (int) Functions::flattenSingleValue($style);
  799. if ((!is_numeric($aValue)) || ($aValue < 0) || ($aValue >= 4000)) {
  800. return Functions::VALUE();
  801. }
  802. $aValue = (int) $aValue;
  803. if ($aValue == 0) {
  804. return '';
  805. }
  806. $mill = ['', 'M', 'MM', 'MMM', 'MMMM', 'MMMMM'];
  807. $cent = ['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'];
  808. $tens = ['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'];
  809. $ones = ['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'];
  810. $roman = '';
  811. while ($aValue > 5999) {
  812. $roman .= 'M';
  813. $aValue -= 1000;
  814. }
  815. $m = self::romanCut($aValue, 1000);
  816. $aValue %= 1000;
  817. $c = self::romanCut($aValue, 100);
  818. $aValue %= 100;
  819. $t = self::romanCut($aValue, 10);
  820. $aValue %= 10;
  821. return $roman . $mill[$m] . $cent[$c] . $tens[$t] . $ones[$aValue];
  822. }
  823. /**
  824. * ROUNDUP.
  825. *
  826. * Rounds a number up to a specified number of decimal places
  827. *
  828. * @param float $number Number to round
  829. * @param int $digits Number of digits to which you want to round $number
  830. *
  831. * @return float Rounded Number
  832. */
  833. public static function ROUNDUP($number, $digits)
  834. {
  835. $number = Functions::flattenSingleValue($number);
  836. $digits = Functions::flattenSingleValue($digits);
  837. if ((is_numeric($number)) && (is_numeric($digits))) {
  838. $significance = pow(10, (int) $digits);
  839. if ($number < 0.0) {
  840. return floor($number * $significance) / $significance;
  841. }
  842. return ceil($number * $significance) / $significance;
  843. }
  844. return Functions::VALUE();
  845. }
  846. /**
  847. * ROUNDDOWN.
  848. *
  849. * Rounds a number down to a specified number of decimal places
  850. *
  851. * @param float $number Number to round
  852. * @param int $digits Number of digits to which you want to round $number
  853. *
  854. * @return float Rounded Number
  855. */
  856. public static function ROUNDDOWN($number, $digits)
  857. {
  858. $number = Functions::flattenSingleValue($number);
  859. $digits = Functions::flattenSingleValue($digits);
  860. if ((is_numeric($number)) && (is_numeric($digits))) {
  861. $significance = pow(10, (int) $digits);
  862. if ($number < 0.0) {
  863. return ceil($number * $significance) / $significance;
  864. }
  865. return floor($number * $significance) / $significance;
  866. }
  867. return Functions::VALUE();
  868. }
  869. /**
  870. * SERIESSUM.
  871. *
  872. * Returns the sum of a power series
  873. *
  874. * @param float $x Input value to the power series
  875. * @param float $n Initial power to which you want to raise $x
  876. * @param float $m Step by which to increase $n for each term in the series
  877. * @param array of mixed Data Series
  878. *
  879. * @return float
  880. */
  881. public static function SERIESSUM(...$args)
  882. {
  883. $returnValue = 0;
  884. // Loop through arguments
  885. $aArgs = Functions::flattenArray($args);
  886. $x = array_shift($aArgs);
  887. $n = array_shift($aArgs);
  888. $m = array_shift($aArgs);
  889. if ((is_numeric($x)) && (is_numeric($n)) && (is_numeric($m))) {
  890. // Calculate
  891. $i = 0;
  892. foreach ($aArgs as $arg) {
  893. // Is it a numeric value?
  894. if ((is_numeric($arg)) && (!is_string($arg))) {
  895. $returnValue += $arg * pow($x, $n + ($m * $i++));
  896. } else {
  897. return Functions::VALUE();
  898. }
  899. }
  900. return $returnValue;
  901. }
  902. return Functions::VALUE();
  903. }
  904. /**
  905. * SIGN.
  906. *
  907. * Determines the sign of a number. Returns 1 if the number is positive, zero (0)
  908. * if the number is 0, and -1 if the number is negative.
  909. *
  910. * @param float $number Number to round
  911. *
  912. * @return int sign value
  913. */
  914. public static function SIGN($number)
  915. {
  916. $number = Functions::flattenSingleValue($number);
  917. if (is_bool($number)) {
  918. return (int) $number;
  919. }
  920. if (is_numeric($number)) {
  921. if ($number == 0.0) {
  922. return 0;
  923. }
  924. return $number / abs($number);
  925. }
  926. return Functions::VALUE();
  927. }
  928. /**
  929. * SQRTPI.
  930. *
  931. * Returns the square root of (number * pi).
  932. *
  933. * @param float $number Number
  934. *
  935. * @return float Square Root of Number * Pi
  936. */
  937. public static function SQRTPI($number)
  938. {
  939. $number = Functions::flattenSingleValue($number);
  940. if (is_numeric($number)) {
  941. if ($number < 0) {
  942. return Functions::NAN();
  943. }
  944. return sqrt($number * M_PI);
  945. }
  946. return Functions::VALUE();
  947. }
  948. protected static function filterHiddenArgs($cellReference, $args)
  949. {
  950. return array_filter(
  951. $args,
  952. function ($index) use ($cellReference) {
  953. list(, $row, $column) = explode('.', $index);
  954. return $cellReference->getWorksheet()->getRowDimension($row)->getVisible() &&
  955. $cellReference->getWorksheet()->getColumnDimension($column)->getVisible();
  956. },
  957. ARRAY_FILTER_USE_KEY
  958. );
  959. }
  960. protected static function filterFormulaArgs($cellReference, $args)
  961. {
  962. return array_filter(
  963. $args,
  964. function ($index) use ($cellReference) {
  965. list(, $row, $column) = explode('.', $index);
  966. if ($cellReference->getWorksheet()->cellExists($column . $row)) {
  967. //take this cell out if it contains the SUBTOTAL or AGGREGATE functions in a formula
  968. $isFormula = $cellReference->getWorksheet()->getCell($column . $row)->isFormula();
  969. $cellFormula = !preg_match('/^=.*\b(SUBTOTAL|AGGREGATE)\s*\(/i', $cellReference->getWorksheet()->getCell($column . $row)->getValue());
  970. return !$isFormula || $cellFormula;
  971. }
  972. return true;
  973. },
  974. ARRAY_FILTER_USE_KEY
  975. );
  976. }
  977. /**
  978. * SUBTOTAL.
  979. *
  980. * Returns a subtotal in a list or database.
  981. *
  982. * @param int the number 1 to 11 that specifies which function to
  983. * use in calculating subtotals within a range
  984. * list
  985. * Numbers 101 to 111 shadow the functions of 1 to 11
  986. * but ignore any values in the range that are
  987. * in hidden rows or columns
  988. * @param array of mixed Data Series
  989. *
  990. * @return float
  991. */
  992. public static function SUBTOTAL(...$args)
  993. {
  994. $cellReference = array_pop($args);
  995. $aArgs = Functions::flattenArrayIndexed($args);
  996. $subtotal = array_shift($aArgs);
  997. // Calculate
  998. if ((is_numeric($subtotal)) && (!is_string($subtotal))) {
  999. if ($subtotal > 100) {
  1000. $aArgs = self::filterHiddenArgs($cellReference, $aArgs);
  1001. $subtotal -= 100;
  1002. }
  1003. $aArgs = self::filterFormulaArgs($cellReference, $aArgs);
  1004. switch ($subtotal) {
  1005. case 1:
  1006. return Statistical::AVERAGE($aArgs);
  1007. case 2:
  1008. return Statistical::COUNT($aArgs);
  1009. case 3:
  1010. return Statistical::COUNTA($aArgs);
  1011. case 4:
  1012. return Statistical::MAX($aArgs);
  1013. case 5:
  1014. return Statistical::MIN($aArgs);
  1015. case 6:
  1016. return self::PRODUCT($aArgs);
  1017. case 7:
  1018. return Statistical::STDEV($aArgs);
  1019. case 8:
  1020. return Statistical::STDEVP($aArgs);
  1021. case 9:
  1022. return self::SUM($aArgs);
  1023. case 10:
  1024. return Statistical::VARFunc($aArgs);
  1025. case 11:
  1026. return Statistical::VARP($aArgs);
  1027. }
  1028. }
  1029. return Functions::VALUE();
  1030. }
  1031. /**
  1032. * SUM.
  1033. *
  1034. * SUM computes the sum of all the values and cells referenced in the argument list.
  1035. *
  1036. * Excel Function:
  1037. * SUM(value1[,value2[, ...]])
  1038. *
  1039. * @category Mathematical and Trigonometric Functions
  1040. *
  1041. * @param mixed ...$args Data values
  1042. *
  1043. * @return float
  1044. */
  1045. public static function SUM(...$args)
  1046. {
  1047. $returnValue = 0;
  1048. // Loop through the arguments
  1049. foreach (Functions::flattenArray($args) as $arg) {
  1050. // Is it a numeric value?
  1051. if ((is_numeric($arg)) && (!is_string($arg))) {
  1052. $returnValue += $arg;
  1053. }
  1054. }
  1055. return $returnValue;
  1056. }
  1057. /**
  1058. * SUMIF.
  1059. *
  1060. * Counts the number of cells that contain numbers within the list of arguments
  1061. *
  1062. * Excel Function:
  1063. * SUMIF(value1[,value2[, ...]],condition)
  1064. *
  1065. * @category Mathematical and Trigonometric Functions
  1066. *
  1067. * @param mixed $aArgs Data values
  1068. * @param string $condition the criteria that defines which cells will be summed
  1069. * @param mixed $sumArgs
  1070. *
  1071. * @return float
  1072. */
  1073. public static function SUMIF($aArgs, $condition, $sumArgs = [])
  1074. {
  1075. $returnValue = 0;
  1076. $aArgs = Functions::flattenArray($aArgs);
  1077. $sumArgs = Functions::flattenArray($sumArgs);
  1078. if (empty($sumArgs)) {
  1079. $sumArgs = $aArgs;
  1080. }
  1081. $condition = Functions::ifCondition($condition);
  1082. // Loop through arguments
  1083. foreach ($aArgs as $key => $arg) {
  1084. if (!is_numeric($arg)) {
  1085. $arg = str_replace('"', '""', $arg);
  1086. $arg = Calculation::wrapResult(strtoupper($arg));
  1087. }
  1088. $testCondition = '=' . $arg . $condition;
  1089. if (is_numeric($sumArgs[$key]) &&
  1090. Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1091. // Is it a value within our criteria and only numeric can be added to the result
  1092. $returnValue += $sumArgs[$key];
  1093. }
  1094. }
  1095. return $returnValue;
  1096. }
  1097. /**
  1098. * SUMIFS.
  1099. *
  1100. * Counts the number of cells that contain numbers within the list of arguments
  1101. *
  1102. * Excel Function:
  1103. * SUMIFS(value1[,value2[, ...]],condition)
  1104. *
  1105. * @category Mathematical and Trigonometric Functions
  1106. *
  1107. * @param mixed $args Data values
  1108. * @param string $condition the criteria that defines which cells will be summed
  1109. *
  1110. * @return float
  1111. */
  1112. public static function SUMIFS(...$args)
  1113. {
  1114. $arrayList = $args;
  1115. // Return value
  1116. $returnValue = 0;
  1117. $sumArgs = Functions::flattenArray(array_shift($arrayList));
  1118. $aArgsArray = [];
  1119. $conditions = [];
  1120. while (count($arrayList) > 0) {
  1121. $aArgsArray[] = Functions::flattenArray(array_shift($arrayList));
  1122. $conditions[] = Functions::ifCondition(array_shift($arrayList));
  1123. }
  1124. // Loop through each sum and see if arguments and conditions are true
  1125. foreach ($sumArgs as $index => $value) {
  1126. $valid = true;
  1127. foreach ($conditions as $cidx => $condition) {
  1128. $arg = $aArgsArray[$cidx][$index];
  1129. // Loop through arguments
  1130. if (!is_numeric($arg)) {
  1131. $arg = Calculation::wrapResult(strtoupper($arg));
  1132. }
  1133. $testCondition = '=' . $arg . $condition;
  1134. if (!Calculation::getInstance()->_calculateFormulaValue($testCondition)) {
  1135. // Is not a value within our criteria
  1136. $valid = false;
  1137. break; // if false found, don't need to check other conditions
  1138. }
  1139. }
  1140. if ($valid) {
  1141. $returnValue += $value;
  1142. }
  1143. }
  1144. // Return
  1145. return $returnValue;
  1146. }
  1147. /**
  1148. * SUMPRODUCT.
  1149. *
  1150. * Excel Function:
  1151. * SUMPRODUCT(value1[,value2[, ...]])
  1152. *
  1153. * @category Mathematical and Trigonometric Functions
  1154. *
  1155. * @param mixed ...$args Data values
  1156. *
  1157. * @return float
  1158. */
  1159. public static function SUMPRODUCT(...$args)
  1160. {
  1161. $arrayList = $args;
  1162. $wrkArray = Functions::flattenArray(array_shift($arrayList));
  1163. $wrkCellCount = count($wrkArray);
  1164. for ($i = 0; $i < $wrkCellCount; ++$i) {
  1165. if ((!is_numeric($wrkArray[$i])) || (is_string($wrkArray[$i]))) {
  1166. $wrkArray[$i] = 0;
  1167. }
  1168. }
  1169. foreach ($arrayList as $matrixData) {
  1170. $array2 = Functions::flattenArray($matrixData);
  1171. $count = count($array2);
  1172. if ($wrkCellCount != $count) {
  1173. return Functions::VALUE();
  1174. }
  1175. foreach ($array2 as $i => $val) {
  1176. if ((!is_numeric($val)) || (is_string($val))) {
  1177. $val = 0;
  1178. }
  1179. $wrkArray[$i] *= $val;
  1180. }
  1181. }
  1182. return array_sum($wrkArray);
  1183. }
  1184. /**
  1185. * SUMSQ.
  1186. *
  1187. * SUMSQ returns the sum of the squares of the arguments
  1188. *
  1189. * Excel Function:
  1190. * SUMSQ(value1[,value2[, ...]])
  1191. *
  1192. * @category Mathematical and Trigonometric Functions
  1193. *
  1194. * @param mixed ...$args Data values
  1195. *
  1196. * @return float
  1197. */
  1198. public static function SUMSQ(...$args)
  1199. {
  1200. $returnValue = 0;
  1201. // Loop through arguments
  1202. foreach (Functions::flattenArray($args) as $arg) {
  1203. // Is it a numeric value?
  1204. if ((is_numeric($arg)) && (!is_string($arg))) {
  1205. $returnValue += ($arg * $arg);
  1206. }
  1207. }
  1208. return $returnValue;
  1209. }
  1210. /**
  1211. * SUMX2MY2.
  1212. *
  1213. * @param mixed[] $matrixData1 Matrix #1
  1214. * @param mixed[] $matrixData2 Matrix #2
  1215. *
  1216. * @return float
  1217. */
  1218. public static function SUMX2MY2($matrixData1, $matrixData2)
  1219. {
  1220. $array1 = Functions::flattenArray($matrixData1);
  1221. $array2 = Functions::flattenArray($matrixData2);
  1222. $count = min(count($array1), count($array2));
  1223. $result = 0;
  1224. for ($i = 0; $i < $count; ++$i) {
  1225. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1226. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1227. $result += ($array1[$i] * $array1[$i]) - ($array2[$i] * $array2[$i]);
  1228. }
  1229. }
  1230. return $result;
  1231. }
  1232. /**
  1233. * SUMX2PY2.
  1234. *
  1235. * @param mixed[] $matrixData1 Matrix #1
  1236. * @param mixed[] $matrixData2 Matrix #2
  1237. *
  1238. * @return float
  1239. */
  1240. public static function SUMX2PY2($matrixData1, $matrixData2)
  1241. {
  1242. $array1 = Functions::flattenArray($matrixData1);
  1243. $array2 = Functions::flattenArray($matrixData2);
  1244. $count = min(count($array1), count($array2));
  1245. $result = 0;
  1246. for ($i = 0; $i < $count; ++$i) {
  1247. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1248. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1249. $result += ($array1[$i] * $array1[$i]) + ($array2[$i] * $array2[$i]);
  1250. }
  1251. }
  1252. return $result;
  1253. }
  1254. /**
  1255. * SUMXMY2.
  1256. *
  1257. * @param mixed[] $matrixData1 Matrix #1
  1258. * @param mixed[] $matrixData2 Matrix #2
  1259. *
  1260. * @return float
  1261. */
  1262. public static function SUMXMY2($matrixData1, $matrixData2)
  1263. {
  1264. $array1 = Functions::flattenArray($matrixData1);
  1265. $array2 = Functions::flattenArray($matrixData2);
  1266. $count = min(count($array1), count($array2));
  1267. $result = 0;
  1268. for ($i = 0; $i < $count; ++$i) {
  1269. if (((is_numeric($array1[$i])) && (!is_string($array1[$i]))) &&
  1270. ((is_numeric($array2[$i])) && (!is_string($array2[$i])))) {
  1271. $result += ($array1[$i] - $array2[$i]) * ($array1[$i] - $array2[$i]);
  1272. }
  1273. }
  1274. return $result;
  1275. }
  1276. /**
  1277. * TRUNC.
  1278. *
  1279. * Truncates value to the number of fractional digits by number_digits.
  1280. *
  1281. * @param float $value
  1282. * @param int $digits
  1283. *
  1284. * @return float Truncated value
  1285. */
  1286. public static function TRUNC($value = 0, $digits = 0)
  1287. {
  1288. $value = Functions::flattenSingleValue($value);
  1289. $digits = Functions::flattenSingleValue($digits);
  1290. // Validate parameters
  1291. if ((!is_numeric($value)) || (!is_numeric($digits))) {
  1292. return Functions::VALUE();
  1293. }
  1294. $digits = floor($digits);
  1295. // Truncate
  1296. $adjust = pow(10, $digits);
  1297. if (($digits > 0) && (rtrim((int) ((abs($value) - abs((int) $value)) * $adjust), '0') < $adjust / 10)) {
  1298. return $value;
  1299. }
  1300. return ((int) ($value * $adjust)) / $adjust;
  1301. }
  1302. /**
  1303. * SEC.
  1304. *
  1305. * Returns the secant of an angle.
  1306. *
  1307. * @param float $angle Number
  1308. *
  1309. * @return float|string The secant of the angle
  1310. */
  1311. public static function SEC($angle)
  1312. {
  1313. $angle = Functions::flattenSingleValue($angle);
  1314. if (!is_numeric($angle)) {
  1315. return Functions::VALUE();
  1316. }
  1317. $result = cos($angle);
  1318. return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
  1319. }
  1320. /**
  1321. * SECH.
  1322. *
  1323. * Returns the hyperbolic secant of an angle.
  1324. *
  1325. * @param float $angle Number
  1326. *
  1327. * @return float|string The hyperbolic secant of the angle
  1328. */
  1329. public static function SECH($angle)
  1330. {
  1331. $angle = Functions::flattenSingleValue($angle);
  1332. if (!is_numeric($angle)) {
  1333. return Functions::VALUE();
  1334. }
  1335. $result = cosh($angle);
  1336. return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
  1337. }
  1338. /**
  1339. * CSC.
  1340. *
  1341. * Returns the cosecant of an angle.
  1342. *
  1343. * @param float $angle Number
  1344. *
  1345. * @return float|string The cosecant of the angle
  1346. */
  1347. public static function CSC($angle)
  1348. {
  1349. $angle = Functions::flattenSingleValue($angle);
  1350. if (!is_numeric($angle)) {
  1351. return Functions::VALUE();
  1352. }
  1353. $result = sin($angle);
  1354. return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
  1355. }
  1356. /**
  1357. * CSCH.
  1358. *
  1359. * Returns the hyperbolic cosecant of an angle.
  1360. *
  1361. * @param float $angle Number
  1362. *
  1363. * @return float|string The hyperbolic cosecant of the angle
  1364. */
  1365. public static function CSCH($angle)
  1366. {
  1367. $angle = Functions::flattenSingleValue($angle);
  1368. if (!is_numeric($angle)) {
  1369. return Functions::VALUE();
  1370. }
  1371. $result = sinh($angle);
  1372. return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
  1373. }
  1374. /**
  1375. * COT.
  1376. *
  1377. * Returns the cotangent of an angle.
  1378. *
  1379. * @param float $angle Number
  1380. *
  1381. * @return float|string The cotangent of the angle
  1382. */
  1383. public static function COT($angle)
  1384. {
  1385. $angle = Functions::flattenSingleValue($angle);
  1386. if (!is_numeric($angle)) {
  1387. return Functions::VALUE();
  1388. }
  1389. $result = tan($angle);
  1390. return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
  1391. }
  1392. /**
  1393. * COTH.
  1394. *
  1395. * Returns the hyperbolic cotangent of an angle.
  1396. *
  1397. * @param float $angle Number
  1398. *
  1399. * @return float|string The hyperbolic cotangent of the angle
  1400. */
  1401. public static function COTH($angle)
  1402. {
  1403. $angle = Functions::flattenSingleValue($angle);
  1404. if (!is_numeric($angle)) {
  1405. return Functions::VALUE();
  1406. }
  1407. $result = tanh($angle);
  1408. return ($result == 0.0) ? Functions::DIV0() : 1 / $result;
  1409. }
  1410. /**
  1411. * ACOT.
  1412. *
  1413. * Returns the arccotangent of a number.
  1414. *
  1415. * @param float $number Number
  1416. *
  1417. * @return float|string The arccotangent of the number
  1418. */
  1419. public static function ACOT($number)
  1420. {
  1421. $number = Functions::flattenSingleValue($number);
  1422. if (!is_numeric($number)) {
  1423. return Functions::VALUE();
  1424. }
  1425. return (M_PI / 2) - atan($number);
  1426. }
  1427. /**
  1428. * ACOTH.
  1429. *
  1430. * Returns the hyperbolic arccotangent of a number.
  1431. *
  1432. * @param float $number Number
  1433. *
  1434. * @return float|string The hyperbolic arccotangent of the number
  1435. */
  1436. public static function ACOTH($number)
  1437. {
  1438. $number = Functions::flattenSingleValue($number);
  1439. if (!is_numeric($number)) {
  1440. return Functions::VALUE();
  1441. }
  1442. $result = log(($number + 1) / ($number - 1)) / 2;
  1443. return is_nan($result) ? Functions::NAN() : $result;
  1444. }
  1445. }