Engineering.php 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Calculation;
  3. use Complex\Complex;
  4. use Complex\Exception as ComplexException;
  5. class Engineering
  6. {
  7. /**
  8. * EULER.
  9. */
  10. const EULER = 2.71828182845904523536;
  11. /**
  12. * Details of the Units of measure that can be used in CONVERTUOM().
  13. *
  14. * @var mixed[]
  15. */
  16. private static $conversionUnits = [
  17. 'g' => ['Group' => 'Mass', 'Unit Name' => 'Gram', 'AllowPrefix' => true],
  18. 'sg' => ['Group' => 'Mass', 'Unit Name' => 'Slug', 'AllowPrefix' => false],
  19. 'lbm' => ['Group' => 'Mass', 'Unit Name' => 'Pound mass (avoirdupois)', 'AllowPrefix' => false],
  20. 'u' => ['Group' => 'Mass', 'Unit Name' => 'U (atomic mass unit)', 'AllowPrefix' => true],
  21. 'ozm' => ['Group' => 'Mass', 'Unit Name' => 'Ounce mass (avoirdupois)', 'AllowPrefix' => false],
  22. 'm' => ['Group' => 'Distance', 'Unit Name' => 'Meter', 'AllowPrefix' => true],
  23. 'mi' => ['Group' => 'Distance', 'Unit Name' => 'Statute mile', 'AllowPrefix' => false],
  24. 'Nmi' => ['Group' => 'Distance', 'Unit Name' => 'Nautical mile', 'AllowPrefix' => false],
  25. 'in' => ['Group' => 'Distance', 'Unit Name' => 'Inch', 'AllowPrefix' => false],
  26. 'ft' => ['Group' => 'Distance', 'Unit Name' => 'Foot', 'AllowPrefix' => false],
  27. 'yd' => ['Group' => 'Distance', 'Unit Name' => 'Yard', 'AllowPrefix' => false],
  28. 'ang' => ['Group' => 'Distance', 'Unit Name' => 'Angstrom', 'AllowPrefix' => true],
  29. 'Pica' => ['Group' => 'Distance', 'Unit Name' => 'Pica (1/72 in)', 'AllowPrefix' => false],
  30. 'yr' => ['Group' => 'Time', 'Unit Name' => 'Year', 'AllowPrefix' => false],
  31. 'day' => ['Group' => 'Time', 'Unit Name' => 'Day', 'AllowPrefix' => false],
  32. 'hr' => ['Group' => 'Time', 'Unit Name' => 'Hour', 'AllowPrefix' => false],
  33. 'mn' => ['Group' => 'Time', 'Unit Name' => 'Minute', 'AllowPrefix' => false],
  34. 'sec' => ['Group' => 'Time', 'Unit Name' => 'Second', 'AllowPrefix' => true],
  35. 'Pa' => ['Group' => 'Pressure', 'Unit Name' => 'Pascal', 'AllowPrefix' => true],
  36. 'p' => ['Group' => 'Pressure', 'Unit Name' => 'Pascal', 'AllowPrefix' => true],
  37. 'atm' => ['Group' => 'Pressure', 'Unit Name' => 'Atmosphere', 'AllowPrefix' => true],
  38. 'at' => ['Group' => 'Pressure', 'Unit Name' => 'Atmosphere', 'AllowPrefix' => true],
  39. 'mmHg' => ['Group' => 'Pressure', 'Unit Name' => 'mm of Mercury', 'AllowPrefix' => true],
  40. 'N' => ['Group' => 'Force', 'Unit Name' => 'Newton', 'AllowPrefix' => true],
  41. 'dyn' => ['Group' => 'Force', 'Unit Name' => 'Dyne', 'AllowPrefix' => true],
  42. 'dy' => ['Group' => 'Force', 'Unit Name' => 'Dyne', 'AllowPrefix' => true],
  43. 'lbf' => ['Group' => 'Force', 'Unit Name' => 'Pound force', 'AllowPrefix' => false],
  44. 'J' => ['Group' => 'Energy', 'Unit Name' => 'Joule', 'AllowPrefix' => true],
  45. 'e' => ['Group' => 'Energy', 'Unit Name' => 'Erg', 'AllowPrefix' => true],
  46. 'c' => ['Group' => 'Energy', 'Unit Name' => 'Thermodynamic calorie', 'AllowPrefix' => true],
  47. 'cal' => ['Group' => 'Energy', 'Unit Name' => 'IT calorie', 'AllowPrefix' => true],
  48. 'eV' => ['Group' => 'Energy', 'Unit Name' => 'Electron volt', 'AllowPrefix' => true],
  49. 'ev' => ['Group' => 'Energy', 'Unit Name' => 'Electron volt', 'AllowPrefix' => true],
  50. 'HPh' => ['Group' => 'Energy', 'Unit Name' => 'Horsepower-hour', 'AllowPrefix' => false],
  51. 'hh' => ['Group' => 'Energy', 'Unit Name' => 'Horsepower-hour', 'AllowPrefix' => false],
  52. 'Wh' => ['Group' => 'Energy', 'Unit Name' => 'Watt-hour', 'AllowPrefix' => true],
  53. 'wh' => ['Group' => 'Energy', 'Unit Name' => 'Watt-hour', 'AllowPrefix' => true],
  54. 'flb' => ['Group' => 'Energy', 'Unit Name' => 'Foot-pound', 'AllowPrefix' => false],
  55. 'BTU' => ['Group' => 'Energy', 'Unit Name' => 'BTU', 'AllowPrefix' => false],
  56. 'btu' => ['Group' => 'Energy', 'Unit Name' => 'BTU', 'AllowPrefix' => false],
  57. 'HP' => ['Group' => 'Power', 'Unit Name' => 'Horsepower', 'AllowPrefix' => false],
  58. 'h' => ['Group' => 'Power', 'Unit Name' => 'Horsepower', 'AllowPrefix' => false],
  59. 'W' => ['Group' => 'Power', 'Unit Name' => 'Watt', 'AllowPrefix' => true],
  60. 'w' => ['Group' => 'Power', 'Unit Name' => 'Watt', 'AllowPrefix' => true],
  61. 'T' => ['Group' => 'Magnetism', 'Unit Name' => 'Tesla', 'AllowPrefix' => true],
  62. 'ga' => ['Group' => 'Magnetism', 'Unit Name' => 'Gauss', 'AllowPrefix' => true],
  63. 'C' => ['Group' => 'Temperature', 'Unit Name' => 'Celsius', 'AllowPrefix' => false],
  64. 'cel' => ['Group' => 'Temperature', 'Unit Name' => 'Celsius', 'AllowPrefix' => false],
  65. 'F' => ['Group' => 'Temperature', 'Unit Name' => 'Fahrenheit', 'AllowPrefix' => false],
  66. 'fah' => ['Group' => 'Temperature', 'Unit Name' => 'Fahrenheit', 'AllowPrefix' => false],
  67. 'K' => ['Group' => 'Temperature', 'Unit Name' => 'Kelvin', 'AllowPrefix' => false],
  68. 'kel' => ['Group' => 'Temperature', 'Unit Name' => 'Kelvin', 'AllowPrefix' => false],
  69. 'tsp' => ['Group' => 'Liquid', 'Unit Name' => 'Teaspoon', 'AllowPrefix' => false],
  70. 'tbs' => ['Group' => 'Liquid', 'Unit Name' => 'Tablespoon', 'AllowPrefix' => false],
  71. 'oz' => ['Group' => 'Liquid', 'Unit Name' => 'Fluid Ounce', 'AllowPrefix' => false],
  72. 'cup' => ['Group' => 'Liquid', 'Unit Name' => 'Cup', 'AllowPrefix' => false],
  73. 'pt' => ['Group' => 'Liquid', 'Unit Name' => 'U.S. Pint', 'AllowPrefix' => false],
  74. 'us_pt' => ['Group' => 'Liquid', 'Unit Name' => 'U.S. Pint', 'AllowPrefix' => false],
  75. 'uk_pt' => ['Group' => 'Liquid', 'Unit Name' => 'U.K. Pint', 'AllowPrefix' => false],
  76. 'qt' => ['Group' => 'Liquid', 'Unit Name' => 'Quart', 'AllowPrefix' => false],
  77. 'gal' => ['Group' => 'Liquid', 'Unit Name' => 'Gallon', 'AllowPrefix' => false],
  78. 'l' => ['Group' => 'Liquid', 'Unit Name' => 'Litre', 'AllowPrefix' => true],
  79. 'lt' => ['Group' => 'Liquid', 'Unit Name' => 'Litre', 'AllowPrefix' => true],
  80. ];
  81. /**
  82. * Details of the Multiplier prefixes that can be used with Units of Measure in CONVERTUOM().
  83. *
  84. * @var mixed[]
  85. */
  86. private static $conversionMultipliers = [
  87. 'Y' => ['multiplier' => 1E24, 'name' => 'yotta'],
  88. 'Z' => ['multiplier' => 1E21, 'name' => 'zetta'],
  89. 'E' => ['multiplier' => 1E18, 'name' => 'exa'],
  90. 'P' => ['multiplier' => 1E15, 'name' => 'peta'],
  91. 'T' => ['multiplier' => 1E12, 'name' => 'tera'],
  92. 'G' => ['multiplier' => 1E9, 'name' => 'giga'],
  93. 'M' => ['multiplier' => 1E6, 'name' => 'mega'],
  94. 'k' => ['multiplier' => 1E3, 'name' => 'kilo'],
  95. 'h' => ['multiplier' => 1E2, 'name' => 'hecto'],
  96. 'e' => ['multiplier' => 1E1, 'name' => 'deka'],
  97. 'd' => ['multiplier' => 1E-1, 'name' => 'deci'],
  98. 'c' => ['multiplier' => 1E-2, 'name' => 'centi'],
  99. 'm' => ['multiplier' => 1E-3, 'name' => 'milli'],
  100. 'u' => ['multiplier' => 1E-6, 'name' => 'micro'],
  101. 'n' => ['multiplier' => 1E-9, 'name' => 'nano'],
  102. 'p' => ['multiplier' => 1E-12, 'name' => 'pico'],
  103. 'f' => ['multiplier' => 1E-15, 'name' => 'femto'],
  104. 'a' => ['multiplier' => 1E-18, 'name' => 'atto'],
  105. 'z' => ['multiplier' => 1E-21, 'name' => 'zepto'],
  106. 'y' => ['multiplier' => 1E-24, 'name' => 'yocto'],
  107. ];
  108. /**
  109. * Details of the Units of measure conversion factors, organised by group.
  110. *
  111. * @var mixed[]
  112. */
  113. private static $unitConversions = [
  114. 'Mass' => [
  115. 'g' => [
  116. 'g' => 1.0,
  117. 'sg' => 6.85220500053478E-05,
  118. 'lbm' => 2.20462291469134E-03,
  119. 'u' => 6.02217000000000E+23,
  120. 'ozm' => 3.52739718003627E-02,
  121. ],
  122. 'sg' => [
  123. 'g' => 1.45938424189287E+04,
  124. 'sg' => 1.0,
  125. 'lbm' => 3.21739194101647E+01,
  126. 'u' => 8.78866000000000E+27,
  127. 'ozm' => 5.14782785944229E+02,
  128. ],
  129. 'lbm' => [
  130. 'g' => 4.5359230974881148E+02,
  131. 'sg' => 3.10810749306493E-02,
  132. 'lbm' => 1.0,
  133. 'u' => 2.73161000000000E+26,
  134. 'ozm' => 1.60000023429410E+01,
  135. ],
  136. 'u' => [
  137. 'g' => 1.66053100460465E-24,
  138. 'sg' => 1.13782988532950E-28,
  139. 'lbm' => 3.66084470330684E-27,
  140. 'u' => 1.0,
  141. 'ozm' => 5.85735238300524E-26,
  142. ],
  143. 'ozm' => [
  144. 'g' => 2.83495152079732E+01,
  145. 'sg' => 1.94256689870811E-03,
  146. 'lbm' => 6.24999908478882E-02,
  147. 'u' => 1.70725600000000E+25,
  148. 'ozm' => 1.0,
  149. ],
  150. ],
  151. 'Distance' => [
  152. 'm' => [
  153. 'm' => 1.0,
  154. 'mi' => 6.21371192237334E-04,
  155. 'Nmi' => 5.39956803455724E-04,
  156. 'in' => 3.93700787401575E+01,
  157. 'ft' => 3.28083989501312E+00,
  158. 'yd' => 1.09361329797891E+00,
  159. 'ang' => 1.00000000000000E+10,
  160. 'Pica' => 2.83464566929116E+03,
  161. ],
  162. 'mi' => [
  163. 'm' => 1.60934400000000E+03,
  164. 'mi' => 1.0,
  165. 'Nmi' => 8.68976241900648E-01,
  166. 'in' => 6.33600000000000E+04,
  167. 'ft' => 5.28000000000000E+03,
  168. 'yd' => 1.76000000000000E+03,
  169. 'ang' => 1.60934400000000E+13,
  170. 'Pica' => 4.56191999999971E+06,
  171. ],
  172. 'Nmi' => [
  173. 'm' => 1.85200000000000E+03,
  174. 'mi' => 1.15077944802354E+00,
  175. 'Nmi' => 1.0,
  176. 'in' => 7.29133858267717E+04,
  177. 'ft' => 6.07611548556430E+03,
  178. 'yd' => 2.02537182785694E+03,
  179. 'ang' => 1.85200000000000E+13,
  180. 'Pica' => 5.24976377952723E+06,
  181. ],
  182. 'in' => [
  183. 'm' => 2.54000000000000E-02,
  184. 'mi' => 1.57828282828283E-05,
  185. 'Nmi' => 1.37149028077754E-05,
  186. 'in' => 1.0,
  187. 'ft' => 8.33333333333333E-02,
  188. 'yd' => 2.77777777686643E-02,
  189. 'ang' => 2.54000000000000E+08,
  190. 'Pica' => 7.19999999999955E+01,
  191. ],
  192. 'ft' => [
  193. 'm' => 3.04800000000000E-01,
  194. 'mi' => 1.89393939393939E-04,
  195. 'Nmi' => 1.64578833693305E-04,
  196. 'in' => 1.20000000000000E+01,
  197. 'ft' => 1.0,
  198. 'yd' => 3.33333333223972E-01,
  199. 'ang' => 3.04800000000000E+09,
  200. 'Pica' => 8.63999999999946E+02,
  201. ],
  202. 'yd' => [
  203. 'm' => 9.14400000300000E-01,
  204. 'mi' => 5.68181818368230E-04,
  205. 'Nmi' => 4.93736501241901E-04,
  206. 'in' => 3.60000000118110E+01,
  207. 'ft' => 3.00000000000000E+00,
  208. 'yd' => 1.0,
  209. 'ang' => 9.14400000300000E+09,
  210. 'Pica' => 2.59200000085023E+03,
  211. ],
  212. 'ang' => [
  213. 'm' => 1.00000000000000E-10,
  214. 'mi' => 6.21371192237334E-14,
  215. 'Nmi' => 5.39956803455724E-14,
  216. 'in' => 3.93700787401575E-09,
  217. 'ft' => 3.28083989501312E-10,
  218. 'yd' => 1.09361329797891E-10,
  219. 'ang' => 1.0,
  220. 'Pica' => 2.83464566929116E-07,
  221. ],
  222. 'Pica' => [
  223. 'm' => 3.52777777777800E-04,
  224. 'mi' => 2.19205948372629E-07,
  225. 'Nmi' => 1.90484761219114E-07,
  226. 'in' => 1.38888888888898E-02,
  227. 'ft' => 1.15740740740748E-03,
  228. 'yd' => 3.85802469009251E-04,
  229. 'ang' => 3.52777777777800E+06,
  230. 'Pica' => 1.0,
  231. ],
  232. ],
  233. 'Time' => [
  234. 'yr' => [
  235. 'yr' => 1.0,
  236. 'day' => 365.25,
  237. 'hr' => 8766.0,
  238. 'mn' => 525960.0,
  239. 'sec' => 31557600.0,
  240. ],
  241. 'day' => [
  242. 'yr' => 2.73785078713210E-03,
  243. 'day' => 1.0,
  244. 'hr' => 24.0,
  245. 'mn' => 1440.0,
  246. 'sec' => 86400.0,
  247. ],
  248. 'hr' => [
  249. 'yr' => 1.14077116130504E-04,
  250. 'day' => 4.16666666666667E-02,
  251. 'hr' => 1.0,
  252. 'mn' => 60.0,
  253. 'sec' => 3600.0,
  254. ],
  255. 'mn' => [
  256. 'yr' => 1.90128526884174E-06,
  257. 'day' => 6.94444444444444E-04,
  258. 'hr' => 1.66666666666667E-02,
  259. 'mn' => 1.0,
  260. 'sec' => 60.0,
  261. ],
  262. 'sec' => [
  263. 'yr' => 3.16880878140289E-08,
  264. 'day' => 1.15740740740741E-05,
  265. 'hr' => 2.77777777777778E-04,
  266. 'mn' => 1.66666666666667E-02,
  267. 'sec' => 1.0,
  268. ],
  269. ],
  270. 'Pressure' => [
  271. 'Pa' => [
  272. 'Pa' => 1.0,
  273. 'p' => 1.0,
  274. 'atm' => 9.86923299998193E-06,
  275. 'at' => 9.86923299998193E-06,
  276. 'mmHg' => 7.50061707998627E-03,
  277. ],
  278. 'p' => [
  279. 'Pa' => 1.0,
  280. 'p' => 1.0,
  281. 'atm' => 9.86923299998193E-06,
  282. 'at' => 9.86923299998193E-06,
  283. 'mmHg' => 7.50061707998627E-03,
  284. ],
  285. 'atm' => [
  286. 'Pa' => 1.01324996583000E+05,
  287. 'p' => 1.01324996583000E+05,
  288. 'atm' => 1.0,
  289. 'at' => 1.0,
  290. 'mmHg' => 760.0,
  291. ],
  292. 'at' => [
  293. 'Pa' => 1.01324996583000E+05,
  294. 'p' => 1.01324996583000E+05,
  295. 'atm' => 1.0,
  296. 'at' => 1.0,
  297. 'mmHg' => 760.0,
  298. ],
  299. 'mmHg' => [
  300. 'Pa' => 1.33322363925000E+02,
  301. 'p' => 1.33322363925000E+02,
  302. 'atm' => 1.31578947368421E-03,
  303. 'at' => 1.31578947368421E-03,
  304. 'mmHg' => 1.0,
  305. ],
  306. ],
  307. 'Force' => [
  308. 'N' => [
  309. 'N' => 1.0,
  310. 'dyn' => 1.0E+5,
  311. 'dy' => 1.0E+5,
  312. 'lbf' => 2.24808923655339E-01,
  313. ],
  314. 'dyn' => [
  315. 'N' => 1.0E-5,
  316. 'dyn' => 1.0,
  317. 'dy' => 1.0,
  318. 'lbf' => 2.24808923655339E-06,
  319. ],
  320. 'dy' => [
  321. 'N' => 1.0E-5,
  322. 'dyn' => 1.0,
  323. 'dy' => 1.0,
  324. 'lbf' => 2.24808923655339E-06,
  325. ],
  326. 'lbf' => [
  327. 'N' => 4.448222,
  328. 'dyn' => 4.448222E+5,
  329. 'dy' => 4.448222E+5,
  330. 'lbf' => 1.0,
  331. ],
  332. ],
  333. 'Energy' => [
  334. 'J' => [
  335. 'J' => 1.0,
  336. 'e' => 9.99999519343231E+06,
  337. 'c' => 2.39006249473467E-01,
  338. 'cal' => 2.38846190642017E-01,
  339. 'eV' => 6.24145700000000E+18,
  340. 'ev' => 6.24145700000000E+18,
  341. 'HPh' => 3.72506430801000E-07,
  342. 'hh' => 3.72506430801000E-07,
  343. 'Wh' => 2.77777916238711E-04,
  344. 'wh' => 2.77777916238711E-04,
  345. 'flb' => 2.37304222192651E+01,
  346. 'BTU' => 9.47815067349015E-04,
  347. 'btu' => 9.47815067349015E-04,
  348. ],
  349. 'e' => [
  350. 'J' => 1.00000048065700E-07,
  351. 'e' => 1.0,
  352. 'c' => 2.39006364353494E-08,
  353. 'cal' => 2.38846305445111E-08,
  354. 'eV' => 6.24146000000000E+11,
  355. 'ev' => 6.24146000000000E+11,
  356. 'HPh' => 3.72506609848824E-14,
  357. 'hh' => 3.72506609848824E-14,
  358. 'Wh' => 2.77778049754611E-11,
  359. 'wh' => 2.77778049754611E-11,
  360. 'flb' => 2.37304336254586E-06,
  361. 'BTU' => 9.47815522922962E-11,
  362. 'btu' => 9.47815522922962E-11,
  363. ],
  364. 'c' => [
  365. 'J' => 4.18399101363672E+00,
  366. 'e' => 4.18398900257312E+07,
  367. 'c' => 1.0,
  368. 'cal' => 9.99330315287563E-01,
  369. 'eV' => 2.61142000000000E+19,
  370. 'ev' => 2.61142000000000E+19,
  371. 'HPh' => 1.55856355899327E-06,
  372. 'hh' => 1.55856355899327E-06,
  373. 'Wh' => 1.16222030532950E-03,
  374. 'wh' => 1.16222030532950E-03,
  375. 'flb' => 9.92878733152102E+01,
  376. 'BTU' => 3.96564972437776E-03,
  377. 'btu' => 3.96564972437776E-03,
  378. ],
  379. 'cal' => [
  380. 'J' => 4.18679484613929E+00,
  381. 'e' => 4.18679283372801E+07,
  382. 'c' => 1.00067013349059E+00,
  383. 'cal' => 1.0,
  384. 'eV' => 2.61317000000000E+19,
  385. 'ev' => 2.61317000000000E+19,
  386. 'HPh' => 1.55960800463137E-06,
  387. 'hh' => 1.55960800463137E-06,
  388. 'Wh' => 1.16299914807955E-03,
  389. 'wh' => 1.16299914807955E-03,
  390. 'flb' => 9.93544094443283E+01,
  391. 'BTU' => 3.96830723907002E-03,
  392. 'btu' => 3.96830723907002E-03,
  393. ],
  394. 'eV' => [
  395. 'J' => 1.60219000146921E-19,
  396. 'e' => 1.60218923136574E-12,
  397. 'c' => 3.82933423195043E-20,
  398. 'cal' => 3.82676978535648E-20,
  399. 'eV' => 1.0,
  400. 'ev' => 1.0,
  401. 'HPh' => 5.96826078912344E-26,
  402. 'hh' => 5.96826078912344E-26,
  403. 'Wh' => 4.45053000026614E-23,
  404. 'wh' => 4.45053000026614E-23,
  405. 'flb' => 3.80206452103492E-18,
  406. 'BTU' => 1.51857982414846E-22,
  407. 'btu' => 1.51857982414846E-22,
  408. ],
  409. 'ev' => [
  410. 'J' => 1.60219000146921E-19,
  411. 'e' => 1.60218923136574E-12,
  412. 'c' => 3.82933423195043E-20,
  413. 'cal' => 3.82676978535648E-20,
  414. 'eV' => 1.0,
  415. 'ev' => 1.0,
  416. 'HPh' => 5.96826078912344E-26,
  417. 'hh' => 5.96826078912344E-26,
  418. 'Wh' => 4.45053000026614E-23,
  419. 'wh' => 4.45053000026614E-23,
  420. 'flb' => 3.80206452103492E-18,
  421. 'BTU' => 1.51857982414846E-22,
  422. 'btu' => 1.51857982414846E-22,
  423. ],
  424. 'HPh' => [
  425. 'J' => 2.68451741316170E+06,
  426. 'e' => 2.68451612283024E+13,
  427. 'c' => 6.41616438565991E+05,
  428. 'cal' => 6.41186757845835E+05,
  429. 'eV' => 1.67553000000000E+25,
  430. 'ev' => 1.67553000000000E+25,
  431. 'HPh' => 1.0,
  432. 'hh' => 1.0,
  433. 'Wh' => 7.45699653134593E+02,
  434. 'wh' => 7.45699653134593E+02,
  435. 'flb' => 6.37047316692964E+07,
  436. 'BTU' => 2.54442605275546E+03,
  437. 'btu' => 2.54442605275546E+03,
  438. ],
  439. 'hh' => [
  440. 'J' => 2.68451741316170E+06,
  441. 'e' => 2.68451612283024E+13,
  442. 'c' => 6.41616438565991E+05,
  443. 'cal' => 6.41186757845835E+05,
  444. 'eV' => 1.67553000000000E+25,
  445. 'ev' => 1.67553000000000E+25,
  446. 'HPh' => 1.0,
  447. 'hh' => 1.0,
  448. 'Wh' => 7.45699653134593E+02,
  449. 'wh' => 7.45699653134593E+02,
  450. 'flb' => 6.37047316692964E+07,
  451. 'BTU' => 2.54442605275546E+03,
  452. 'btu' => 2.54442605275546E+03,
  453. ],
  454. 'Wh' => [
  455. 'J' => 3.59999820554720E+03,
  456. 'e' => 3.59999647518369E+10,
  457. 'c' => 8.60422069219046E+02,
  458. 'cal' => 8.59845857713046E+02,
  459. 'eV' => 2.24692340000000E+22,
  460. 'ev' => 2.24692340000000E+22,
  461. 'HPh' => 1.34102248243839E-03,
  462. 'hh' => 1.34102248243839E-03,
  463. 'Wh' => 1.0,
  464. 'wh' => 1.0,
  465. 'flb' => 8.54294774062316E+04,
  466. 'BTU' => 3.41213254164705E+00,
  467. 'btu' => 3.41213254164705E+00,
  468. ],
  469. 'wh' => [
  470. 'J' => 3.59999820554720E+03,
  471. 'e' => 3.59999647518369E+10,
  472. 'c' => 8.60422069219046E+02,
  473. 'cal' => 8.59845857713046E+02,
  474. 'eV' => 2.24692340000000E+22,
  475. 'ev' => 2.24692340000000E+22,
  476. 'HPh' => 1.34102248243839E-03,
  477. 'hh' => 1.34102248243839E-03,
  478. 'Wh' => 1.0,
  479. 'wh' => 1.0,
  480. 'flb' => 8.54294774062316E+04,
  481. 'BTU' => 3.41213254164705E+00,
  482. 'btu' => 3.41213254164705E+00,
  483. ],
  484. 'flb' => [
  485. 'J' => 4.21400003236424E-02,
  486. 'e' => 4.21399800687660E+05,
  487. 'c' => 1.00717234301644E-02,
  488. 'cal' => 1.00649785509554E-02,
  489. 'eV' => 2.63015000000000E+17,
  490. 'ev' => 2.63015000000000E+17,
  491. 'HPh' => 1.56974211145130E-08,
  492. 'hh' => 1.56974211145130E-08,
  493. 'Wh' => 1.17055614802000E-05,
  494. 'wh' => 1.17055614802000E-05,
  495. 'flb' => 1.0,
  496. 'BTU' => 3.99409272448406E-05,
  497. 'btu' => 3.99409272448406E-05,
  498. ],
  499. 'BTU' => [
  500. 'J' => 1.05505813786749E+03,
  501. 'e' => 1.05505763074665E+10,
  502. 'c' => 2.52165488508168E+02,
  503. 'cal' => 2.51996617135510E+02,
  504. 'eV' => 6.58510000000000E+21,
  505. 'ev' => 6.58510000000000E+21,
  506. 'HPh' => 3.93015941224568E-04,
  507. 'hh' => 3.93015941224568E-04,
  508. 'Wh' => 2.93071851047526E-01,
  509. 'wh' => 2.93071851047526E-01,
  510. 'flb' => 2.50369750774671E+04,
  511. 'BTU' => 1.0,
  512. 'btu' => 1.0,
  513. ],
  514. 'btu' => [
  515. 'J' => 1.05505813786749E+03,
  516. 'e' => 1.05505763074665E+10,
  517. 'c' => 2.52165488508168E+02,
  518. 'cal' => 2.51996617135510E+02,
  519. 'eV' => 6.58510000000000E+21,
  520. 'ev' => 6.58510000000000E+21,
  521. 'HPh' => 3.93015941224568E-04,
  522. 'hh' => 3.93015941224568E-04,
  523. 'Wh' => 2.93071851047526E-01,
  524. 'wh' => 2.93071851047526E-01,
  525. 'flb' => 2.50369750774671E+04,
  526. 'BTU' => 1.0,
  527. 'btu' => 1.0,
  528. ],
  529. ],
  530. 'Power' => [
  531. 'HP' => [
  532. 'HP' => 1.0,
  533. 'h' => 1.0,
  534. 'W' => 7.45701000000000E+02,
  535. 'w' => 7.45701000000000E+02,
  536. ],
  537. 'h' => [
  538. 'HP' => 1.0,
  539. 'h' => 1.0,
  540. 'W' => 7.45701000000000E+02,
  541. 'w' => 7.45701000000000E+02,
  542. ],
  543. 'W' => [
  544. 'HP' => 1.34102006031908E-03,
  545. 'h' => 1.34102006031908E-03,
  546. 'W' => 1.0,
  547. 'w' => 1.0,
  548. ],
  549. 'w' => [
  550. 'HP' => 1.34102006031908E-03,
  551. 'h' => 1.34102006031908E-03,
  552. 'W' => 1.0,
  553. 'w' => 1.0,
  554. ],
  555. ],
  556. 'Magnetism' => [
  557. 'T' => [
  558. 'T' => 1.0,
  559. 'ga' => 10000.0,
  560. ],
  561. 'ga' => [
  562. 'T' => 0.0001,
  563. 'ga' => 1.0,
  564. ],
  565. ],
  566. 'Liquid' => [
  567. 'tsp' => [
  568. 'tsp' => 1.0,
  569. 'tbs' => 3.33333333333333E-01,
  570. 'oz' => 1.66666666666667E-01,
  571. 'cup' => 2.08333333333333E-02,
  572. 'pt' => 1.04166666666667E-02,
  573. 'us_pt' => 1.04166666666667E-02,
  574. 'uk_pt' => 8.67558516821960E-03,
  575. 'qt' => 5.20833333333333E-03,
  576. 'gal' => 1.30208333333333E-03,
  577. 'l' => 4.92999408400710E-03,
  578. 'lt' => 4.92999408400710E-03,
  579. ],
  580. 'tbs' => [
  581. 'tsp' => 3.00000000000000E+00,
  582. 'tbs' => 1.0,
  583. 'oz' => 5.00000000000000E-01,
  584. 'cup' => 6.25000000000000E-02,
  585. 'pt' => 3.12500000000000E-02,
  586. 'us_pt' => 3.12500000000000E-02,
  587. 'uk_pt' => 2.60267555046588E-02,
  588. 'qt' => 1.56250000000000E-02,
  589. 'gal' => 3.90625000000000E-03,
  590. 'l' => 1.47899822520213E-02,
  591. 'lt' => 1.47899822520213E-02,
  592. ],
  593. 'oz' => [
  594. 'tsp' => 6.00000000000000E+00,
  595. 'tbs' => 2.00000000000000E+00,
  596. 'oz' => 1.0,
  597. 'cup' => 1.25000000000000E-01,
  598. 'pt' => 6.25000000000000E-02,
  599. 'us_pt' => 6.25000000000000E-02,
  600. 'uk_pt' => 5.20535110093176E-02,
  601. 'qt' => 3.12500000000000E-02,
  602. 'gal' => 7.81250000000000E-03,
  603. 'l' => 2.95799645040426E-02,
  604. 'lt' => 2.95799645040426E-02,
  605. ],
  606. 'cup' => [
  607. 'tsp' => 4.80000000000000E+01,
  608. 'tbs' => 1.60000000000000E+01,
  609. 'oz' => 8.00000000000000E+00,
  610. 'cup' => 1.0,
  611. 'pt' => 5.00000000000000E-01,
  612. 'us_pt' => 5.00000000000000E-01,
  613. 'uk_pt' => 4.16428088074541E-01,
  614. 'qt' => 2.50000000000000E-01,
  615. 'gal' => 6.25000000000000E-02,
  616. 'l' => 2.36639716032341E-01,
  617. 'lt' => 2.36639716032341E-01,
  618. ],
  619. 'pt' => [
  620. 'tsp' => 9.60000000000000E+01,
  621. 'tbs' => 3.20000000000000E+01,
  622. 'oz' => 1.60000000000000E+01,
  623. 'cup' => 2.00000000000000E+00,
  624. 'pt' => 1.0,
  625. 'us_pt' => 1.0,
  626. 'uk_pt' => 8.32856176149081E-01,
  627. 'qt' => 5.00000000000000E-01,
  628. 'gal' => 1.25000000000000E-01,
  629. 'l' => 4.73279432064682E-01,
  630. 'lt' => 4.73279432064682E-01,
  631. ],
  632. 'us_pt' => [
  633. 'tsp' => 9.60000000000000E+01,
  634. 'tbs' => 3.20000000000000E+01,
  635. 'oz' => 1.60000000000000E+01,
  636. 'cup' => 2.00000000000000E+00,
  637. 'pt' => 1.0,
  638. 'us_pt' => 1.0,
  639. 'uk_pt' => 8.32856176149081E-01,
  640. 'qt' => 5.00000000000000E-01,
  641. 'gal' => 1.25000000000000E-01,
  642. 'l' => 4.73279432064682E-01,
  643. 'lt' => 4.73279432064682E-01,
  644. ],
  645. 'uk_pt' => [
  646. 'tsp' => 1.15266000000000E+02,
  647. 'tbs' => 3.84220000000000E+01,
  648. 'oz' => 1.92110000000000E+01,
  649. 'cup' => 2.40137500000000E+00,
  650. 'pt' => 1.20068750000000E+00,
  651. 'us_pt' => 1.20068750000000E+00,
  652. 'uk_pt' => 1.0,
  653. 'qt' => 6.00343750000000E-01,
  654. 'gal' => 1.50085937500000E-01,
  655. 'l' => 5.68260698087162E-01,
  656. 'lt' => 5.68260698087162E-01,
  657. ],
  658. 'qt' => [
  659. 'tsp' => 1.92000000000000E+02,
  660. 'tbs' => 6.40000000000000E+01,
  661. 'oz' => 3.20000000000000E+01,
  662. 'cup' => 4.00000000000000E+00,
  663. 'pt' => 2.00000000000000E+00,
  664. 'us_pt' => 2.00000000000000E+00,
  665. 'uk_pt' => 1.66571235229816E+00,
  666. 'qt' => 1.0,
  667. 'gal' => 2.50000000000000E-01,
  668. 'l' => 9.46558864129363E-01,
  669. 'lt' => 9.46558864129363E-01,
  670. ],
  671. 'gal' => [
  672. 'tsp' => 7.68000000000000E+02,
  673. 'tbs' => 2.56000000000000E+02,
  674. 'oz' => 1.28000000000000E+02,
  675. 'cup' => 1.60000000000000E+01,
  676. 'pt' => 8.00000000000000E+00,
  677. 'us_pt' => 8.00000000000000E+00,
  678. 'uk_pt' => 6.66284940919265E+00,
  679. 'qt' => 4.00000000000000E+00,
  680. 'gal' => 1.0,
  681. 'l' => 3.78623545651745E+00,
  682. 'lt' => 3.78623545651745E+00,
  683. ],
  684. 'l' => [
  685. 'tsp' => 2.02840000000000E+02,
  686. 'tbs' => 6.76133333333333E+01,
  687. 'oz' => 3.38066666666667E+01,
  688. 'cup' => 4.22583333333333E+00,
  689. 'pt' => 2.11291666666667E+00,
  690. 'us_pt' => 2.11291666666667E+00,
  691. 'uk_pt' => 1.75975569552166E+00,
  692. 'qt' => 1.05645833333333E+00,
  693. 'gal' => 2.64114583333333E-01,
  694. 'l' => 1.0,
  695. 'lt' => 1.0,
  696. ],
  697. 'lt' => [
  698. 'tsp' => 2.02840000000000E+02,
  699. 'tbs' => 6.76133333333333E+01,
  700. 'oz' => 3.38066666666667E+01,
  701. 'cup' => 4.22583333333333E+00,
  702. 'pt' => 2.11291666666667E+00,
  703. 'us_pt' => 2.11291666666667E+00,
  704. 'uk_pt' => 1.75975569552166E+00,
  705. 'qt' => 1.05645833333333E+00,
  706. 'gal' => 2.64114583333333E-01,
  707. 'l' => 1.0,
  708. 'lt' => 1.0,
  709. ],
  710. ],
  711. ];
  712. /**
  713. * parseComplex.
  714. *
  715. * Parses a complex number into its real and imaginary parts, and an I or J suffix
  716. *
  717. * @deprecated 2.0.0 No longer used by internal code. Please use the Complex\Complex class instead
  718. *
  719. * @param string $complexNumber The complex number
  720. *
  721. * @return mixed[] Indexed on "real", "imaginary" and "suffix"
  722. */
  723. public static function parseComplex($complexNumber)
  724. {
  725. $complex = new Complex($complexNumber);
  726. return [
  727. 'real' => $complex->getReal(),
  728. 'imaginary' => $complex->getImaginary(),
  729. 'suffix' => $complex->getSuffix(),
  730. ];
  731. }
  732. /**
  733. * Formats a number base string value with leading zeroes.
  734. *
  735. * @param string $xVal The "number" to pad
  736. * @param int $places The length that we want to pad this value
  737. *
  738. * @return string The padded "number"
  739. */
  740. private static function nbrConversionFormat($xVal, $places)
  741. {
  742. if ($places !== null) {
  743. if (is_numeric($places)) {
  744. $places = (int) $places;
  745. } else {
  746. return Functions::VALUE();
  747. }
  748. if ($places < 0) {
  749. return Functions::NAN();
  750. }
  751. if (strlen($xVal) <= $places) {
  752. return substr(str_pad($xVal, $places, '0', STR_PAD_LEFT), -10);
  753. }
  754. return Functions::NAN();
  755. }
  756. return substr($xVal, -10);
  757. }
  758. /**
  759. * BESSELI.
  760. *
  761. * Returns the modified Bessel function In(x), which is equivalent to the Bessel function evaluated
  762. * for purely imaginary arguments
  763. *
  764. * Excel Function:
  765. * BESSELI(x,ord)
  766. *
  767. * @category Engineering Functions
  768. *
  769. * @param float $x The value at which to evaluate the function.
  770. * If x is nonnumeric, BESSELI returns the #VALUE! error value.
  771. * @param int $ord The order of the Bessel function.
  772. * If ord is not an integer, it is truncated.
  773. * If $ord is nonnumeric, BESSELI returns the #VALUE! error value.
  774. * If $ord < 0, BESSELI returns the #NUM! error value.
  775. *
  776. * @return float
  777. */
  778. public static function BESSELI($x, $ord)
  779. {
  780. $x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
  781. $ord = ($ord === null) ? 0.0 : Functions::flattenSingleValue($ord);
  782. if ((is_numeric($x)) && (is_numeric($ord))) {
  783. $ord = floor($ord);
  784. if ($ord < 0) {
  785. return Functions::NAN();
  786. }
  787. if (abs($x) <= 30) {
  788. $fResult = $fTerm = pow($x / 2, $ord) / MathTrig::FACT($ord);
  789. $ordK = 1;
  790. $fSqrX = ($x * $x) / 4;
  791. do {
  792. $fTerm *= $fSqrX;
  793. $fTerm /= ($ordK * ($ordK + $ord));
  794. $fResult += $fTerm;
  795. } while ((abs($fTerm) > 1e-12) && (++$ordK < 100));
  796. } else {
  797. $f_2_PI = 2 * M_PI;
  798. $fXAbs = abs($x);
  799. $fResult = exp($fXAbs) / sqrt($f_2_PI * $fXAbs);
  800. if (($ord & 1) && ($x < 0)) {
  801. $fResult = -$fResult;
  802. }
  803. }
  804. return (is_nan($fResult)) ? Functions::NAN() : $fResult;
  805. }
  806. return Functions::VALUE();
  807. }
  808. /**
  809. * BESSELJ.
  810. *
  811. * Returns the Bessel function
  812. *
  813. * Excel Function:
  814. * BESSELJ(x,ord)
  815. *
  816. * @category Engineering Functions
  817. *
  818. * @param float $x The value at which to evaluate the function.
  819. * If x is nonnumeric, BESSELJ returns the #VALUE! error value.
  820. * @param int $ord The order of the Bessel function. If n is not an integer, it is truncated.
  821. * If $ord is nonnumeric, BESSELJ returns the #VALUE! error value.
  822. * If $ord < 0, BESSELJ returns the #NUM! error value.
  823. *
  824. * @return float
  825. */
  826. public static function BESSELJ($x, $ord)
  827. {
  828. $x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
  829. $ord = ($ord === null) ? 0.0 : Functions::flattenSingleValue($ord);
  830. if ((is_numeric($x)) && (is_numeric($ord))) {
  831. $ord = floor($ord);
  832. if ($ord < 0) {
  833. return Functions::NAN();
  834. }
  835. $fResult = 0;
  836. if (abs($x) <= 30) {
  837. $fResult = $fTerm = pow($x / 2, $ord) / MathTrig::FACT($ord);
  838. $ordK = 1;
  839. $fSqrX = ($x * $x) / -4;
  840. do {
  841. $fTerm *= $fSqrX;
  842. $fTerm /= ($ordK * ($ordK + $ord));
  843. $fResult += $fTerm;
  844. } while ((abs($fTerm) > 1e-12) && (++$ordK < 100));
  845. } else {
  846. $f_PI_DIV_2 = M_PI / 2;
  847. $f_PI_DIV_4 = M_PI / 4;
  848. $fXAbs = abs($x);
  849. $fResult = sqrt(Functions::M_2DIVPI / $fXAbs) * cos($fXAbs - $ord * $f_PI_DIV_2 - $f_PI_DIV_4);
  850. if (($ord & 1) && ($x < 0)) {
  851. $fResult = -$fResult;
  852. }
  853. }
  854. return (is_nan($fResult)) ? Functions::NAN() : $fResult;
  855. }
  856. return Functions::VALUE();
  857. }
  858. private static function besselK0($fNum)
  859. {
  860. if ($fNum <= 2) {
  861. $fNum2 = $fNum * 0.5;
  862. $y = ($fNum2 * $fNum2);
  863. $fRet = -log($fNum2) * self::BESSELI($fNum, 0) +
  864. (-0.57721566 + $y * (0.42278420 + $y * (0.23069756 + $y * (0.3488590e-1 + $y * (0.262698e-2 + $y *
  865. (0.10750e-3 + $y * 0.74e-5))))));
  866. } else {
  867. $y = 2 / $fNum;
  868. $fRet = exp(-$fNum) / sqrt($fNum) *
  869. (1.25331414 + $y * (-0.7832358e-1 + $y * (0.2189568e-1 + $y * (-0.1062446e-1 + $y *
  870. (0.587872e-2 + $y * (-0.251540e-2 + $y * 0.53208e-3))))));
  871. }
  872. return $fRet;
  873. }
  874. private static function besselK1($fNum)
  875. {
  876. if ($fNum <= 2) {
  877. $fNum2 = $fNum * 0.5;
  878. $y = ($fNum2 * $fNum2);
  879. $fRet = log($fNum2) * self::BESSELI($fNum, 1) +
  880. (1 + $y * (0.15443144 + $y * (-0.67278579 + $y * (-0.18156897 + $y * (-0.1919402e-1 + $y *
  881. (-0.110404e-2 + $y * (-0.4686e-4))))))) / $fNum;
  882. } else {
  883. $y = 2 / $fNum;
  884. $fRet = exp(-$fNum) / sqrt($fNum) *
  885. (1.25331414 + $y * (0.23498619 + $y * (-0.3655620e-1 + $y * (0.1504268e-1 + $y * (-0.780353e-2 + $y *
  886. (0.325614e-2 + $y * (-0.68245e-3)))))));
  887. }
  888. return $fRet;
  889. }
  890. /**
  891. * BESSELK.
  892. *
  893. * Returns the modified Bessel function Kn(x), which is equivalent to the Bessel functions evaluated
  894. * for purely imaginary arguments.
  895. *
  896. * Excel Function:
  897. * BESSELK(x,ord)
  898. *
  899. * @category Engineering Functions
  900. *
  901. * @param float $x The value at which to evaluate the function.
  902. * If x is nonnumeric, BESSELK returns the #VALUE! error value.
  903. * @param int $ord The order of the Bessel function. If n is not an integer, it is truncated.
  904. * If $ord is nonnumeric, BESSELK returns the #VALUE! error value.
  905. * If $ord < 0, BESSELK returns the #NUM! error value.
  906. *
  907. * @return float
  908. */
  909. public static function BESSELK($x, $ord)
  910. {
  911. $x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
  912. $ord = ($ord === null) ? 0.0 : Functions::flattenSingleValue($ord);
  913. if ((is_numeric($x)) && (is_numeric($ord))) {
  914. if (($ord < 0) || ($x == 0.0)) {
  915. return Functions::NAN();
  916. }
  917. switch (floor($ord)) {
  918. case 0:
  919. $fBk = self::besselK0($x);
  920. break;
  921. case 1:
  922. $fBk = self::besselK1($x);
  923. break;
  924. default:
  925. $fTox = 2 / $x;
  926. $fBkm = self::besselK0($x);
  927. $fBk = self::besselK1($x);
  928. for ($n = 1; $n < $ord; ++$n) {
  929. $fBkp = $fBkm + $n * $fTox * $fBk;
  930. $fBkm = $fBk;
  931. $fBk = $fBkp;
  932. }
  933. }
  934. return (is_nan($fBk)) ? Functions::NAN() : $fBk;
  935. }
  936. return Functions::VALUE();
  937. }
  938. private static function besselY0($fNum)
  939. {
  940. if ($fNum < 8.0) {
  941. $y = ($fNum * $fNum);
  942. $f1 = -2957821389.0 + $y * (7062834065.0 + $y * (-512359803.6 + $y * (10879881.29 + $y * (-86327.92757 + $y * 228.4622733))));
  943. $f2 = 40076544269.0 + $y * (745249964.8 + $y * (7189466.438 + $y * (47447.26470 + $y * (226.1030244 + $y))));
  944. $fRet = $f1 / $f2 + 0.636619772 * self::BESSELJ($fNum, 0) * log($fNum);
  945. } else {
  946. $z = 8.0 / $fNum;
  947. $y = ($z * $z);
  948. $xx = $fNum - 0.785398164;
  949. $f1 = 1 + $y * (-0.1098628627e-2 + $y * (0.2734510407e-4 + $y * (-0.2073370639e-5 + $y * 0.2093887211e-6)));
  950. $f2 = -0.1562499995e-1 + $y * (0.1430488765e-3 + $y * (-0.6911147651e-5 + $y * (0.7621095161e-6 + $y * (-0.934945152e-7))));
  951. $fRet = sqrt(0.636619772 / $fNum) * (sin($xx) * $f1 + $z * cos($xx) * $f2);
  952. }
  953. return $fRet;
  954. }
  955. private static function besselY1($fNum)
  956. {
  957. if ($fNum < 8.0) {
  958. $y = ($fNum * $fNum);
  959. $f1 = $fNum * (-0.4900604943e13 + $y * (0.1275274390e13 + $y * (-0.5153438139e11 + $y * (0.7349264551e9 + $y *
  960. (-0.4237922726e7 + $y * 0.8511937935e4)))));
  961. $f2 = 0.2499580570e14 + $y * (0.4244419664e12 + $y * (0.3733650367e10 + $y * (0.2245904002e8 + $y *
  962. (0.1020426050e6 + $y * (0.3549632885e3 + $y)))));
  963. $fRet = $f1 / $f2 + 0.636619772 * (self::BESSELJ($fNum, 1) * log($fNum) - 1 / $fNum);
  964. } else {
  965. $fRet = sqrt(0.636619772 / $fNum) * sin($fNum - 2.356194491);
  966. }
  967. return $fRet;
  968. }
  969. /**
  970. * BESSELY.
  971. *
  972. * Returns the Bessel function, which is also called the Weber function or the Neumann function.
  973. *
  974. * Excel Function:
  975. * BESSELY(x,ord)
  976. *
  977. * @category Engineering Functions
  978. *
  979. * @param float $x The value at which to evaluate the function.
  980. * If x is nonnumeric, BESSELK returns the #VALUE! error value.
  981. * @param int $ord The order of the Bessel function. If n is not an integer, it is truncated.
  982. * If $ord is nonnumeric, BESSELK returns the #VALUE! error value.
  983. * If $ord < 0, BESSELK returns the #NUM! error value.
  984. *
  985. * @return float
  986. */
  987. public static function BESSELY($x, $ord)
  988. {
  989. $x = ($x === null) ? 0.0 : Functions::flattenSingleValue($x);
  990. $ord = ($ord === null) ? 0.0 : Functions::flattenSingleValue($ord);
  991. if ((is_numeric($x)) && (is_numeric($ord))) {
  992. if (($ord < 0) || ($x == 0.0)) {
  993. return Functions::NAN();
  994. }
  995. switch (floor($ord)) {
  996. case 0:
  997. $fBy = self::besselY0($x);
  998. break;
  999. case 1:
  1000. $fBy = self::besselY1($x);
  1001. break;
  1002. default:
  1003. $fTox = 2 / $x;
  1004. $fBym = self::besselY0($x);
  1005. $fBy = self::besselY1($x);
  1006. for ($n = 1; $n < $ord; ++$n) {
  1007. $fByp = $n * $fTox * $fBy - $fBym;
  1008. $fBym = $fBy;
  1009. $fBy = $fByp;
  1010. }
  1011. }
  1012. return (is_nan($fBy)) ? Functions::NAN() : $fBy;
  1013. }
  1014. return Functions::VALUE();
  1015. }
  1016. /**
  1017. * BINTODEC.
  1018. *
  1019. * Return a binary value as decimal.
  1020. *
  1021. * Excel Function:
  1022. * BIN2DEC(x)
  1023. *
  1024. * @category Engineering Functions
  1025. *
  1026. * @param string $x The binary number (as a string) that you want to convert. The number
  1027. * cannot contain more than 10 characters (10 bits). The most significant
  1028. * bit of number is the sign bit. The remaining 9 bits are magnitude bits.
  1029. * Negative numbers are represented using two's-complement notation.
  1030. * If number is not a valid binary number, or if number contains more than
  1031. * 10 characters (10 bits), BIN2DEC returns the #NUM! error value.
  1032. *
  1033. * @return string
  1034. */
  1035. public static function BINTODEC($x)
  1036. {
  1037. $x = Functions::flattenSingleValue($x);
  1038. if (is_bool($x)) {
  1039. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
  1040. $x = (int) $x;
  1041. } else {
  1042. return Functions::VALUE();
  1043. }
  1044. }
  1045. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
  1046. $x = floor($x);
  1047. }
  1048. $x = (string) $x;
  1049. if (strlen($x) > preg_match_all('/[01]/', $x, $out)) {
  1050. return Functions::NAN();
  1051. }
  1052. if (strlen($x) > 10) {
  1053. return Functions::NAN();
  1054. } elseif (strlen($x) == 10) {
  1055. // Two's Complement
  1056. $x = substr($x, -9);
  1057. return '-' . (512 - bindec($x));
  1058. }
  1059. return bindec($x);
  1060. }
  1061. /**
  1062. * BINTOHEX.
  1063. *
  1064. * Return a binary value as hex.
  1065. *
  1066. * Excel Function:
  1067. * BIN2HEX(x[,places])
  1068. *
  1069. * @category Engineering Functions
  1070. *
  1071. * @param string $x The binary number (as a string) that you want to convert. The number
  1072. * cannot contain more than 10 characters (10 bits). The most significant
  1073. * bit of number is the sign bit. The remaining 9 bits are magnitude bits.
  1074. * Negative numbers are represented using two's-complement notation.
  1075. * If number is not a valid binary number, or if number contains more than
  1076. * 10 characters (10 bits), BIN2HEX returns the #NUM! error value.
  1077. * @param int $places The number of characters to use. If places is omitted, BIN2HEX uses the
  1078. * minimum number of characters necessary. Places is useful for padding the
  1079. * return value with leading 0s (zeros).
  1080. * If places is not an integer, it is truncated.
  1081. * If places is nonnumeric, BIN2HEX returns the #VALUE! error value.
  1082. * If places is negative, BIN2HEX returns the #NUM! error value.
  1083. *
  1084. * @return string
  1085. */
  1086. public static function BINTOHEX($x, $places = null)
  1087. {
  1088. $x = Functions::flattenSingleValue($x);
  1089. $places = Functions::flattenSingleValue($places);
  1090. // Argument X
  1091. if (is_bool($x)) {
  1092. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
  1093. $x = (int) $x;
  1094. } else {
  1095. return Functions::VALUE();
  1096. }
  1097. }
  1098. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
  1099. $x = floor($x);
  1100. }
  1101. $x = (string) $x;
  1102. if (strlen($x) > preg_match_all('/[01]/', $x, $out)) {
  1103. return Functions::NAN();
  1104. }
  1105. if (strlen($x) > 10) {
  1106. return Functions::NAN();
  1107. } elseif (strlen($x) == 10) {
  1108. // Two's Complement
  1109. return str_repeat('F', 8) . substr(strtoupper(dechex(bindec(substr($x, -9)))), -2);
  1110. }
  1111. $hexVal = (string) strtoupper(dechex(bindec($x)));
  1112. return self::nbrConversionFormat($hexVal, $places);
  1113. }
  1114. /**
  1115. * BINTOOCT.
  1116. *
  1117. * Return a binary value as octal.
  1118. *
  1119. * Excel Function:
  1120. * BIN2OCT(x[,places])
  1121. *
  1122. * @category Engineering Functions
  1123. *
  1124. * @param string $x The binary number (as a string) that you want to convert. The number
  1125. * cannot contain more than 10 characters (10 bits). The most significant
  1126. * bit of number is the sign bit. The remaining 9 bits are magnitude bits.
  1127. * Negative numbers are represented using two's-complement notation.
  1128. * If number is not a valid binary number, or if number contains more than
  1129. * 10 characters (10 bits), BIN2OCT returns the #NUM! error value.
  1130. * @param int $places The number of characters to use. If places is omitted, BIN2OCT uses the
  1131. * minimum number of characters necessary. Places is useful for padding the
  1132. * return value with leading 0s (zeros).
  1133. * If places is not an integer, it is truncated.
  1134. * If places is nonnumeric, BIN2OCT returns the #VALUE! error value.
  1135. * If places is negative, BIN2OCT returns the #NUM! error value.
  1136. *
  1137. * @return string
  1138. */
  1139. public static function BINTOOCT($x, $places = null)
  1140. {
  1141. $x = Functions::flattenSingleValue($x);
  1142. $places = Functions::flattenSingleValue($places);
  1143. if (is_bool($x)) {
  1144. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
  1145. $x = (int) $x;
  1146. } else {
  1147. return Functions::VALUE();
  1148. }
  1149. }
  1150. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_GNUMERIC) {
  1151. $x = floor($x);
  1152. }
  1153. $x = (string) $x;
  1154. if (strlen($x) > preg_match_all('/[01]/', $x, $out)) {
  1155. return Functions::NAN();
  1156. }
  1157. if (strlen($x) > 10) {
  1158. return Functions::NAN();
  1159. } elseif (strlen($x) == 10) {
  1160. // Two's Complement
  1161. return str_repeat('7', 7) . substr(strtoupper(decoct(bindec(substr($x, -9)))), -3);
  1162. }
  1163. $octVal = (string) decoct(bindec($x));
  1164. return self::nbrConversionFormat($octVal, $places);
  1165. }
  1166. /**
  1167. * DECTOBIN.
  1168. *
  1169. * Return a decimal value as binary.
  1170. *
  1171. * Excel Function:
  1172. * DEC2BIN(x[,places])
  1173. *
  1174. * @category Engineering Functions
  1175. *
  1176. * @param string $x The decimal integer you want to convert. If number is negative,
  1177. * valid place values are ignored and DEC2BIN returns a 10-character
  1178. * (10-bit) binary number in which the most significant bit is the sign
  1179. * bit. The remaining 9 bits are magnitude bits. Negative numbers are
  1180. * represented using two's-complement notation.
  1181. * If number < -512 or if number > 511, DEC2BIN returns the #NUM! error
  1182. * value.
  1183. * If number is nonnumeric, DEC2BIN returns the #VALUE! error value.
  1184. * If DEC2BIN requires more than places characters, it returns the #NUM!
  1185. * error value.
  1186. * @param int $places The number of characters to use. If places is omitted, DEC2BIN uses
  1187. * the minimum number of characters necessary. Places is useful for
  1188. * padding the return value with leading 0s (zeros).
  1189. * If places is not an integer, it is truncated.
  1190. * If places is nonnumeric, DEC2BIN returns the #VALUE! error value.
  1191. * If places is zero or negative, DEC2BIN returns the #NUM! error value.
  1192. *
  1193. * @return string
  1194. */
  1195. public static function DECTOBIN($x, $places = null)
  1196. {
  1197. $x = Functions::flattenSingleValue($x);
  1198. $places = Functions::flattenSingleValue($places);
  1199. if (is_bool($x)) {
  1200. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
  1201. $x = (int) $x;
  1202. } else {
  1203. return Functions::VALUE();
  1204. }
  1205. }
  1206. $x = (string) $x;
  1207. if (strlen($x) > preg_match_all('/[-0123456789.]/', $x, $out)) {
  1208. return Functions::VALUE();
  1209. }
  1210. $x = (string) floor($x);
  1211. if ($x < -512 || $x > 511) {
  1212. return Functions::NAN();
  1213. }
  1214. $r = decbin($x);
  1215. // Two's Complement
  1216. $r = substr($r, -10);
  1217. if (strlen($r) >= 11) {
  1218. return Functions::NAN();
  1219. }
  1220. return self::nbrConversionFormat($r, $places);
  1221. }
  1222. /**
  1223. * DECTOHEX.
  1224. *
  1225. * Return a decimal value as hex.
  1226. *
  1227. * Excel Function:
  1228. * DEC2HEX(x[,places])
  1229. *
  1230. * @category Engineering Functions
  1231. *
  1232. * @param string $x The decimal integer you want to convert. If number is negative,
  1233. * places is ignored and DEC2HEX returns a 10-character (40-bit)
  1234. * hexadecimal number in which the most significant bit is the sign
  1235. * bit. The remaining 39 bits are magnitude bits. Negative numbers
  1236. * are represented using two's-complement notation.
  1237. * If number < -549,755,813,888 or if number > 549,755,813,887,
  1238. * DEC2HEX returns the #NUM! error value.
  1239. * If number is nonnumeric, DEC2HEX returns the #VALUE! error value.
  1240. * If DEC2HEX requires more than places characters, it returns the
  1241. * #NUM! error value.
  1242. * @param int $places The number of characters to use. If places is omitted, DEC2HEX uses
  1243. * the minimum number of characters necessary. Places is useful for
  1244. * padding the return value with leading 0s (zeros).
  1245. * If places is not an integer, it is truncated.
  1246. * If places is nonnumeric, DEC2HEX returns the #VALUE! error value.
  1247. * If places is zero or negative, DEC2HEX returns the #NUM! error value.
  1248. *
  1249. * @return string
  1250. */
  1251. public static function DECTOHEX($x, $places = null)
  1252. {
  1253. $x = Functions::flattenSingleValue($x);
  1254. $places = Functions::flattenSingleValue($places);
  1255. if (is_bool($x)) {
  1256. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
  1257. $x = (int) $x;
  1258. } else {
  1259. return Functions::VALUE();
  1260. }
  1261. }
  1262. $x = (string) $x;
  1263. if (strlen($x) > preg_match_all('/[-0123456789.]/', $x, $out)) {
  1264. return Functions::VALUE();
  1265. }
  1266. $x = (string) floor($x);
  1267. $r = strtoupper(dechex($x));
  1268. if (strlen($r) == 8) {
  1269. // Two's Complement
  1270. $r = 'FF' . $r;
  1271. }
  1272. return self::nbrConversionFormat($r, $places);
  1273. }
  1274. /**
  1275. * DECTOOCT.
  1276. *
  1277. * Return an decimal value as octal.
  1278. *
  1279. * Excel Function:
  1280. * DEC2OCT(x[,places])
  1281. *
  1282. * @category Engineering Functions
  1283. *
  1284. * @param string $x The decimal integer you want to convert. If number is negative,
  1285. * places is ignored and DEC2OCT returns a 10-character (30-bit)
  1286. * octal number in which the most significant bit is the sign bit.
  1287. * The remaining 29 bits are magnitude bits. Negative numbers are
  1288. * represented using two's-complement notation.
  1289. * If number < -536,870,912 or if number > 536,870,911, DEC2OCT
  1290. * returns the #NUM! error value.
  1291. * If number is nonnumeric, DEC2OCT returns the #VALUE! error value.
  1292. * If DEC2OCT requires more than places characters, it returns the
  1293. * #NUM! error value.
  1294. * @param int $places The number of characters to use. If places is omitted, DEC2OCT uses
  1295. * the minimum number of characters necessary. Places is useful for
  1296. * padding the return value with leading 0s (zeros).
  1297. * If places is not an integer, it is truncated.
  1298. * If places is nonnumeric, DEC2OCT returns the #VALUE! error value.
  1299. * If places is zero or negative, DEC2OCT returns the #NUM! error value.
  1300. *
  1301. * @return string
  1302. */
  1303. public static function DECTOOCT($x, $places = null)
  1304. {
  1305. $xorig = $x;
  1306. $x = Functions::flattenSingleValue($x);
  1307. $places = Functions::flattenSingleValue($places);
  1308. if (is_bool($x)) {
  1309. if (Functions::getCompatibilityMode() == Functions::COMPATIBILITY_OPENOFFICE) {
  1310. $x = (int) $x;
  1311. } else {
  1312. return Functions::VALUE();
  1313. }
  1314. }
  1315. $x = (string) $x;
  1316. if (strlen($x) > preg_match_all('/[-0123456789.]/', $x, $out)) {
  1317. return Functions::VALUE();
  1318. }
  1319. $x = (string) floor($x);
  1320. $r = decoct($x);
  1321. if (strlen($r) == 11) {
  1322. // Two's Complement
  1323. $r = substr($r, -10);
  1324. }
  1325. return self::nbrConversionFormat($r, $places);
  1326. }
  1327. /**
  1328. * HEXTOBIN.
  1329. *
  1330. * Return a hex value as binary.
  1331. *
  1332. * Excel Function:
  1333. * HEX2BIN(x[,places])
  1334. *
  1335. * @category Engineering Functions
  1336. *
  1337. * @param string $x the hexadecimal number you want to convert.
  1338. * Number cannot contain more than 10 characters.
  1339. * The most significant bit of number is the sign bit (40th bit from the right).
  1340. * The remaining 9 bits are magnitude bits.
  1341. * Negative numbers are represented using two's-complement notation.
  1342. * If number is negative, HEX2BIN ignores places and returns a 10-character binary number.
  1343. * If number is negative, it cannot be less than FFFFFFFE00,
  1344. * and if number is positive, it cannot be greater than 1FF.
  1345. * If number is not a valid hexadecimal number, HEX2BIN returns the #NUM! error value.
  1346. * If HEX2BIN requires more than places characters, it returns the #NUM! error value.
  1347. * @param int $places The number of characters to use. If places is omitted,
  1348. * HEX2BIN uses the minimum number of characters necessary. Places
  1349. * is useful for padding the return value with leading 0s (zeros).
  1350. * If places is not an integer, it is truncated.
  1351. * If places is nonnumeric, HEX2BIN returns the #VALUE! error value.
  1352. * If places is negative, HEX2BIN returns the #NUM! error value.
  1353. *
  1354. * @return string
  1355. */
  1356. public static function HEXTOBIN($x, $places = null)
  1357. {
  1358. $x = Functions::flattenSingleValue($x);
  1359. $places = Functions::flattenSingleValue($places);
  1360. if (is_bool($x)) {
  1361. return Functions::VALUE();
  1362. }
  1363. $x = (string) $x;
  1364. if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
  1365. return Functions::NAN();
  1366. }
  1367. return self::DECTOBIN(self::HEXTODEC($x), $places);
  1368. }
  1369. /**
  1370. * HEXTODEC.
  1371. *
  1372. * Return a hex value as decimal.
  1373. *
  1374. * Excel Function:
  1375. * HEX2DEC(x)
  1376. *
  1377. * @category Engineering Functions
  1378. *
  1379. * @param string $x The hexadecimal number you want to convert. This number cannot
  1380. * contain more than 10 characters (40 bits). The most significant
  1381. * bit of number is the sign bit. The remaining 39 bits are magnitude
  1382. * bits. Negative numbers are represented using two's-complement
  1383. * notation.
  1384. * If number is not a valid hexadecimal number, HEX2DEC returns the
  1385. * #NUM! error value.
  1386. *
  1387. * @return string
  1388. */
  1389. public static function HEXTODEC($x)
  1390. {
  1391. $x = Functions::flattenSingleValue($x);
  1392. if (is_bool($x)) {
  1393. return Functions::VALUE();
  1394. }
  1395. $x = (string) $x;
  1396. if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
  1397. return Functions::NAN();
  1398. }
  1399. if (strlen($x) > 10) {
  1400. return Functions::NAN();
  1401. }
  1402. $binX = '';
  1403. foreach (str_split($x) as $char) {
  1404. $binX .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT);
  1405. }
  1406. if (strlen($binX) == 40 && $binX[0] == '1') {
  1407. for ($i = 0; $i < 40; ++$i) {
  1408. $binX[$i] = ($binX[$i] == '1' ? '0' : '1');
  1409. }
  1410. return (bindec($binX) + 1) * -1;
  1411. }
  1412. return bindec($binX);
  1413. }
  1414. /**
  1415. * HEXTOOCT.
  1416. *
  1417. * Return a hex value as octal.
  1418. *
  1419. * Excel Function:
  1420. * HEX2OCT(x[,places])
  1421. *
  1422. * @category Engineering Functions
  1423. *
  1424. * @param string $x The hexadecimal number you want to convert. Number cannot
  1425. * contain more than 10 characters. The most significant bit of
  1426. * number is the sign bit. The remaining 39 bits are magnitude
  1427. * bits. Negative numbers are represented using two's-complement
  1428. * notation.
  1429. * If number is negative, HEX2OCT ignores places and returns a
  1430. * 10-character octal number.
  1431. * If number is negative, it cannot be less than FFE0000000, and
  1432. * if number is positive, it cannot be greater than 1FFFFFFF.
  1433. * If number is not a valid hexadecimal number, HEX2OCT returns
  1434. * the #NUM! error value.
  1435. * If HEX2OCT requires more than places characters, it returns
  1436. * the #NUM! error value.
  1437. * @param int $places The number of characters to use. If places is omitted, HEX2OCT
  1438. * uses the minimum number of characters necessary. Places is
  1439. * useful for padding the return value with leading 0s (zeros).
  1440. * If places is not an integer, it is truncated.
  1441. * If places is nonnumeric, HEX2OCT returns the #VALUE! error
  1442. * value.
  1443. * If places is negative, HEX2OCT returns the #NUM! error value.
  1444. *
  1445. * @return string
  1446. */
  1447. public static function HEXTOOCT($x, $places = null)
  1448. {
  1449. $x = Functions::flattenSingleValue($x);
  1450. $places = Functions::flattenSingleValue($places);
  1451. if (is_bool($x)) {
  1452. return Functions::VALUE();
  1453. }
  1454. $x = (string) $x;
  1455. if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/', strtoupper($x), $out)) {
  1456. return Functions::NAN();
  1457. }
  1458. $decimal = self::HEXTODEC($x);
  1459. if ($decimal < -536870912 || $decimal > 536870911) {
  1460. return Functions::NAN();
  1461. }
  1462. return self::DECTOOCT($decimal, $places);
  1463. }
  1464. /**
  1465. * OCTTOBIN.
  1466. *
  1467. * Return an octal value as binary.
  1468. *
  1469. * Excel Function:
  1470. * OCT2BIN(x[,places])
  1471. *
  1472. * @category Engineering Functions
  1473. *
  1474. * @param string $x The octal number you want to convert. Number may not
  1475. * contain more than 10 characters. The most significant
  1476. * bit of number is the sign bit. The remaining 29 bits
  1477. * are magnitude bits. Negative numbers are represented
  1478. * using two's-complement notation.
  1479. * If number is negative, OCT2BIN ignores places and returns
  1480. * a 10-character binary number.
  1481. * If number is negative, it cannot be less than 7777777000,
  1482. * and if number is positive, it cannot be greater than 777.
  1483. * If number is not a valid octal number, OCT2BIN returns
  1484. * the #NUM! error value.
  1485. * If OCT2BIN requires more than places characters, it
  1486. * returns the #NUM! error value.
  1487. * @param int $places The number of characters to use. If places is omitted,
  1488. * OCT2BIN uses the minimum number of characters necessary.
  1489. * Places is useful for padding the return value with
  1490. * leading 0s (zeros).
  1491. * If places is not an integer, it is truncated.
  1492. * If places is nonnumeric, OCT2BIN returns the #VALUE!
  1493. * error value.
  1494. * If places is negative, OCT2BIN returns the #NUM! error
  1495. * value.
  1496. *
  1497. * @return string
  1498. */
  1499. public static function OCTTOBIN($x, $places = null)
  1500. {
  1501. $x = Functions::flattenSingleValue($x);
  1502. $places = Functions::flattenSingleValue($places);
  1503. if (is_bool($x)) {
  1504. return Functions::VALUE();
  1505. }
  1506. $x = (string) $x;
  1507. if (preg_match_all('/[01234567]/', $x, $out) != strlen($x)) {
  1508. return Functions::NAN();
  1509. }
  1510. return self::DECTOBIN(self::OCTTODEC($x), $places);
  1511. }
  1512. /**
  1513. * OCTTODEC.
  1514. *
  1515. * Return an octal value as decimal.
  1516. *
  1517. * Excel Function:
  1518. * OCT2DEC(x)
  1519. *
  1520. * @category Engineering Functions
  1521. *
  1522. * @param string $x The octal number you want to convert. Number may not contain
  1523. * more than 10 octal characters (30 bits). The most significant
  1524. * bit of number is the sign bit. The remaining 29 bits are
  1525. * magnitude bits. Negative numbers are represented using
  1526. * two's-complement notation.
  1527. * If number is not a valid octal number, OCT2DEC returns the
  1528. * #NUM! error value.
  1529. *
  1530. * @return string
  1531. */
  1532. public static function OCTTODEC($x)
  1533. {
  1534. $x = Functions::flattenSingleValue($x);
  1535. if (is_bool($x)) {
  1536. return Functions::VALUE();
  1537. }
  1538. $x = (string) $x;
  1539. if (preg_match_all('/[01234567]/', $x, $out) != strlen($x)) {
  1540. return Functions::NAN();
  1541. }
  1542. $binX = '';
  1543. foreach (str_split($x) as $char) {
  1544. $binX .= str_pad(decbin((int) $char), 3, '0', STR_PAD_LEFT);
  1545. }
  1546. if (strlen($binX) == 30 && $binX[0] == '1') {
  1547. for ($i = 0; $i < 30; ++$i) {
  1548. $binX[$i] = ($binX[$i] == '1' ? '0' : '1');
  1549. }
  1550. return (bindec($binX) + 1) * -1;
  1551. }
  1552. return bindec($binX);
  1553. }
  1554. /**
  1555. * OCTTOHEX.
  1556. *
  1557. * Return an octal value as hex.
  1558. *
  1559. * Excel Function:
  1560. * OCT2HEX(x[,places])
  1561. *
  1562. * @category Engineering Functions
  1563. *
  1564. * @param string $x The octal number you want to convert. Number may not contain
  1565. * more than 10 octal characters (30 bits). The most significant
  1566. * bit of number is the sign bit. The remaining 29 bits are
  1567. * magnitude bits. Negative numbers are represented using
  1568. * two's-complement notation.
  1569. * If number is negative, OCT2HEX ignores places and returns a
  1570. * 10-character hexadecimal number.
  1571. * If number is not a valid octal number, OCT2HEX returns the
  1572. * #NUM! error value.
  1573. * If OCT2HEX requires more than places characters, it returns
  1574. * the #NUM! error value.
  1575. * @param int $places The number of characters to use. If places is omitted, OCT2HEX
  1576. * uses the minimum number of characters necessary. Places is useful
  1577. * for padding the return value with leading 0s (zeros).
  1578. * If places is not an integer, it is truncated.
  1579. * If places is nonnumeric, OCT2HEX returns the #VALUE! error value.
  1580. * If places is negative, OCT2HEX returns the #NUM! error value.
  1581. *
  1582. * @return string
  1583. */
  1584. public static function OCTTOHEX($x, $places = null)
  1585. {
  1586. $x = Functions::flattenSingleValue($x);
  1587. $places = Functions::flattenSingleValue($places);
  1588. if (is_bool($x)) {
  1589. return Functions::VALUE();
  1590. }
  1591. $x = (string) $x;
  1592. if (preg_match_all('/[01234567]/', $x, $out) != strlen($x)) {
  1593. return Functions::NAN();
  1594. }
  1595. $hexVal = strtoupper(dechex(self::OCTTODEC($x)));
  1596. return self::nbrConversionFormat($hexVal, $places);
  1597. }
  1598. /**
  1599. * COMPLEX.
  1600. *
  1601. * Converts real and imaginary coefficients into a complex number of the form x +/- yi or x +/- yj.
  1602. *
  1603. * Excel Function:
  1604. * COMPLEX(realNumber,imaginary[,suffix])
  1605. *
  1606. * @category Engineering Functions
  1607. *
  1608. * @param float $realNumber the real coefficient of the complex number
  1609. * @param float $imaginary the imaginary coefficient of the complex number
  1610. * @param string $suffix The suffix for the imaginary component of the complex number.
  1611. * If omitted, the suffix is assumed to be "i".
  1612. *
  1613. * @return string
  1614. */
  1615. public static function COMPLEX($realNumber = 0.0, $imaginary = 0.0, $suffix = 'i')
  1616. {
  1617. $realNumber = ($realNumber === null) ? 0.0 : Functions::flattenSingleValue($realNumber);
  1618. $imaginary = ($imaginary === null) ? 0.0 : Functions::flattenSingleValue($imaginary);
  1619. $suffix = ($suffix === null) ? 'i' : Functions::flattenSingleValue($suffix);
  1620. if (((is_numeric($realNumber)) && (is_numeric($imaginary))) &&
  1621. (($suffix == 'i') || ($suffix == 'j') || ($suffix == ''))
  1622. ) {
  1623. $complex = new Complex($realNumber, $imaginary, $suffix);
  1624. return (string) $complex;
  1625. }
  1626. return Functions::VALUE();
  1627. }
  1628. /**
  1629. * IMAGINARY.
  1630. *
  1631. * Returns the imaginary coefficient of a complex number in x + yi or x + yj text format.
  1632. *
  1633. * Excel Function:
  1634. * IMAGINARY(complexNumber)
  1635. *
  1636. * @category Engineering Functions
  1637. *
  1638. * @param string $complexNumber the complex number for which you want the imaginary
  1639. * coefficient
  1640. *
  1641. * @return float
  1642. */
  1643. public static function IMAGINARY($complexNumber)
  1644. {
  1645. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1646. return (new Complex($complexNumber))->getImaginary();
  1647. }
  1648. /**
  1649. * IMREAL.
  1650. *
  1651. * Returns the real coefficient of a complex number in x + yi or x + yj text format.
  1652. *
  1653. * Excel Function:
  1654. * IMREAL(complexNumber)
  1655. *
  1656. * @category Engineering Functions
  1657. *
  1658. * @param string $complexNumber the complex number for which you want the real coefficient
  1659. *
  1660. * @return float
  1661. */
  1662. public static function IMREAL($complexNumber)
  1663. {
  1664. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1665. return (new Complex($complexNumber))->getReal();
  1666. }
  1667. /**
  1668. * IMABS.
  1669. *
  1670. * Returns the absolute value (modulus) of a complex number in x + yi or x + yj text format.
  1671. *
  1672. * Excel Function:
  1673. * IMABS(complexNumber)
  1674. *
  1675. * @param string $complexNumber the complex number for which you want the absolute value
  1676. *
  1677. * @return float
  1678. */
  1679. public static function IMABS($complexNumber)
  1680. {
  1681. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1682. return (new Complex($complexNumber))->abs();
  1683. }
  1684. /**
  1685. * IMARGUMENT.
  1686. *
  1687. * Returns the argument theta of a complex number, i.e. the angle in radians from the real
  1688. * axis to the representation of the number in polar coordinates.
  1689. *
  1690. * Excel Function:
  1691. * IMARGUMENT(complexNumber)
  1692. *
  1693. * @param string $complexNumber the complex number for which you want the argument theta
  1694. *
  1695. * @return float|string
  1696. */
  1697. public static function IMARGUMENT($complexNumber)
  1698. {
  1699. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1700. $complex = new Complex($complexNumber);
  1701. if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
  1702. return Functions::DIV0();
  1703. }
  1704. return $complex->argument();
  1705. }
  1706. /**
  1707. * IMCONJUGATE.
  1708. *
  1709. * Returns the complex conjugate of a complex number in x + yi or x + yj text format.
  1710. *
  1711. * Excel Function:
  1712. * IMCONJUGATE(complexNumber)
  1713. *
  1714. * @param string $complexNumber the complex number for which you want the conjugate
  1715. *
  1716. * @return string
  1717. */
  1718. public static function IMCONJUGATE($complexNumber)
  1719. {
  1720. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1721. return (string) (new Complex($complexNumber))->conjugate();
  1722. }
  1723. /**
  1724. * IMCOS.
  1725. *
  1726. * Returns the cosine of a complex number in x + yi or x + yj text format.
  1727. *
  1728. * Excel Function:
  1729. * IMCOS(complexNumber)
  1730. *
  1731. * @param string $complexNumber the complex number for which you want the cosine
  1732. *
  1733. * @return float|string
  1734. */
  1735. public static function IMCOS($complexNumber)
  1736. {
  1737. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1738. return (string) (new Complex($complexNumber))->cos();
  1739. }
  1740. /**
  1741. * IMCOSH.
  1742. *
  1743. * Returns the hyperbolic cosine of a complex number in x + yi or x + yj text format.
  1744. *
  1745. * Excel Function:
  1746. * IMCOSH(complexNumber)
  1747. *
  1748. * @param string $complexNumber the complex number for which you want the hyperbolic cosine
  1749. *
  1750. * @return float|string
  1751. */
  1752. public static function IMCOSH($complexNumber)
  1753. {
  1754. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1755. return (string) (new Complex($complexNumber))->cosh();
  1756. }
  1757. /**
  1758. * IMCOT.
  1759. *
  1760. * Returns the cotangent of a complex number in x + yi or x + yj text format.
  1761. *
  1762. * Excel Function:
  1763. * IMCOT(complexNumber)
  1764. *
  1765. * @param string $complexNumber the complex number for which you want the cotangent
  1766. *
  1767. * @return float|string
  1768. */
  1769. public static function IMCOT($complexNumber)
  1770. {
  1771. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1772. return (string) (new Complex($complexNumber))->cot();
  1773. }
  1774. /**
  1775. * IMCSC.
  1776. *
  1777. * Returns the cosecant of a complex number in x + yi or x + yj text format.
  1778. *
  1779. * Excel Function:
  1780. * IMCSC(complexNumber)
  1781. *
  1782. * @param string $complexNumber the complex number for which you want the cosecant
  1783. *
  1784. * @return float|string
  1785. */
  1786. public static function IMCSC($complexNumber)
  1787. {
  1788. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1789. return (string) (new Complex($complexNumber))->csc();
  1790. }
  1791. /**
  1792. * IMCSCH.
  1793. *
  1794. * Returns the hyperbolic cosecant of a complex number in x + yi or x + yj text format.
  1795. *
  1796. * Excel Function:
  1797. * IMCSCH(complexNumber)
  1798. *
  1799. * @param string $complexNumber the complex number for which you want the hyperbolic cosecant
  1800. *
  1801. * @return float|string
  1802. */
  1803. public static function IMCSCH($complexNumber)
  1804. {
  1805. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1806. return (string) (new Complex($complexNumber))->csch();
  1807. }
  1808. /**
  1809. * IMSIN.
  1810. *
  1811. * Returns the sine of a complex number in x + yi or x + yj text format.
  1812. *
  1813. * Excel Function:
  1814. * IMSIN(complexNumber)
  1815. *
  1816. * @param string $complexNumber the complex number for which you want the sine
  1817. *
  1818. * @return float|string
  1819. */
  1820. public static function IMSIN($complexNumber)
  1821. {
  1822. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1823. return (string) (new Complex($complexNumber))->sin();
  1824. }
  1825. /**
  1826. * IMSINH.
  1827. *
  1828. * Returns the hyperbolic sine of a complex number in x + yi or x + yj text format.
  1829. *
  1830. * Excel Function:
  1831. * IMSINH(complexNumber)
  1832. *
  1833. * @param string $complexNumber the complex number for which you want the hyperbolic sine
  1834. *
  1835. * @return float|string
  1836. */
  1837. public static function IMSINH($complexNumber)
  1838. {
  1839. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1840. return (string) (new Complex($complexNumber))->sinh();
  1841. }
  1842. /**
  1843. * IMSEC.
  1844. *
  1845. * Returns the secant of a complex number in x + yi or x + yj text format.
  1846. *
  1847. * Excel Function:
  1848. * IMSEC(complexNumber)
  1849. *
  1850. * @param string $complexNumber the complex number for which you want the secant
  1851. *
  1852. * @return float|string
  1853. */
  1854. public static function IMSEC($complexNumber)
  1855. {
  1856. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1857. return (string) (new Complex($complexNumber))->sec();
  1858. }
  1859. /**
  1860. * IMSECH.
  1861. *
  1862. * Returns the hyperbolic secant of a complex number in x + yi or x + yj text format.
  1863. *
  1864. * Excel Function:
  1865. * IMSECH(complexNumber)
  1866. *
  1867. * @param string $complexNumber the complex number for which you want the hyperbolic secant
  1868. *
  1869. * @return float|string
  1870. */
  1871. public static function IMSECH($complexNumber)
  1872. {
  1873. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1874. return (string) (new Complex($complexNumber))->sech();
  1875. }
  1876. /**
  1877. * IMTAN.
  1878. *
  1879. * Returns the tangent of a complex number in x + yi or x + yj text format.
  1880. *
  1881. * Excel Function:
  1882. * IMTAN(complexNumber)
  1883. *
  1884. * @param string $complexNumber the complex number for which you want the tangent
  1885. *
  1886. * @return float|string
  1887. */
  1888. public static function IMTAN($complexNumber)
  1889. {
  1890. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1891. return (string) (new Complex($complexNumber))->tan();
  1892. }
  1893. /**
  1894. * IMSQRT.
  1895. *
  1896. * Returns the square root of a complex number in x + yi or x + yj text format.
  1897. *
  1898. * Excel Function:
  1899. * IMSQRT(complexNumber)
  1900. *
  1901. * @param string $complexNumber the complex number for which you want the square root
  1902. *
  1903. * @return string
  1904. */
  1905. public static function IMSQRT($complexNumber)
  1906. {
  1907. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1908. $theta = self::IMARGUMENT($complexNumber);
  1909. if ($theta === Functions::DIV0()) {
  1910. return '0';
  1911. }
  1912. return (string) (new Complex($complexNumber))->sqrt();
  1913. }
  1914. /**
  1915. * IMLN.
  1916. *
  1917. * Returns the natural logarithm of a complex number in x + yi or x + yj text format.
  1918. *
  1919. * Excel Function:
  1920. * IMLN(complexNumber)
  1921. *
  1922. * @param string $complexNumber the complex number for which you want the natural logarithm
  1923. *
  1924. * @return string
  1925. */
  1926. public static function IMLN($complexNumber)
  1927. {
  1928. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1929. $complex = new Complex($complexNumber);
  1930. if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
  1931. return Functions::NAN();
  1932. }
  1933. return (string) (new Complex($complexNumber))->ln();
  1934. }
  1935. /**
  1936. * IMLOG10.
  1937. *
  1938. * Returns the common logarithm (base 10) of a complex number in x + yi or x + yj text format.
  1939. *
  1940. * Excel Function:
  1941. * IMLOG10(complexNumber)
  1942. *
  1943. * @param string $complexNumber the complex number for which you want the common logarithm
  1944. *
  1945. * @return string
  1946. */
  1947. public static function IMLOG10($complexNumber)
  1948. {
  1949. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1950. $complex = new Complex($complexNumber);
  1951. if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
  1952. return Functions::NAN();
  1953. }
  1954. return (string) (new Complex($complexNumber))->log10();
  1955. }
  1956. /**
  1957. * IMLOG2.
  1958. *
  1959. * Returns the base-2 logarithm of a complex number in x + yi or x + yj text format.
  1960. *
  1961. * Excel Function:
  1962. * IMLOG2(complexNumber)
  1963. *
  1964. * @param string $complexNumber the complex number for which you want the base-2 logarithm
  1965. *
  1966. * @return string
  1967. */
  1968. public static function IMLOG2($complexNumber)
  1969. {
  1970. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1971. $complex = new Complex($complexNumber);
  1972. if ($complex->getReal() == 0.0 && $complex->getImaginary() == 0.0) {
  1973. return Functions::NAN();
  1974. }
  1975. return (string) (new Complex($complexNumber))->log2();
  1976. }
  1977. /**
  1978. * IMEXP.
  1979. *
  1980. * Returns the exponential of a complex number in x + yi or x + yj text format.
  1981. *
  1982. * Excel Function:
  1983. * IMEXP(complexNumber)
  1984. *
  1985. * @param string $complexNumber the complex number for which you want the exponential
  1986. *
  1987. * @return string
  1988. */
  1989. public static function IMEXP($complexNumber)
  1990. {
  1991. $complexNumber = Functions::flattenSingleValue($complexNumber);
  1992. return (string) (new Complex($complexNumber))->exp();
  1993. }
  1994. /**
  1995. * IMPOWER.
  1996. *
  1997. * Returns a complex number in x + yi or x + yj text format raised to a power.
  1998. *
  1999. * Excel Function:
  2000. * IMPOWER(complexNumber,realNumber)
  2001. *
  2002. * @param string $complexNumber the complex number you want to raise to a power
  2003. * @param float $realNumber the power to which you want to raise the complex number
  2004. *
  2005. * @return string
  2006. */
  2007. public static function IMPOWER($complexNumber, $realNumber)
  2008. {
  2009. $complexNumber = Functions::flattenSingleValue($complexNumber);
  2010. $realNumber = Functions::flattenSingleValue($realNumber);
  2011. if (!is_numeric($realNumber)) {
  2012. return Functions::VALUE();
  2013. }
  2014. return (string) (new Complex($complexNumber))->pow($realNumber);
  2015. }
  2016. /**
  2017. * IMDIV.
  2018. *
  2019. * Returns the quotient of two complex numbers in x + yi or x + yj text format.
  2020. *
  2021. * Excel Function:
  2022. * IMDIV(complexDividend,complexDivisor)
  2023. *
  2024. * @param string $complexDividend the complex numerator or dividend
  2025. * @param string $complexDivisor the complex denominator or divisor
  2026. *
  2027. * @return string
  2028. */
  2029. public static function IMDIV($complexDividend, $complexDivisor)
  2030. {
  2031. $complexDividend = Functions::flattenSingleValue($complexDividend);
  2032. $complexDivisor = Functions::flattenSingleValue($complexDivisor);
  2033. try {
  2034. return (string) (new Complex($complexDividend))->divideby(new Complex($complexDivisor));
  2035. } catch (ComplexException $e) {
  2036. return Functions::NAN();
  2037. }
  2038. }
  2039. /**
  2040. * IMSUB.
  2041. *
  2042. * Returns the difference of two complex numbers in x + yi or x + yj text format.
  2043. *
  2044. * Excel Function:
  2045. * IMSUB(complexNumber1,complexNumber2)
  2046. *
  2047. * @param string $complexNumber1 the complex number from which to subtract complexNumber2
  2048. * @param string $complexNumber2 the complex number to subtract from complexNumber1
  2049. *
  2050. * @return string
  2051. */
  2052. public static function IMSUB($complexNumber1, $complexNumber2)
  2053. {
  2054. $complexNumber1 = Functions::flattenSingleValue($complexNumber1);
  2055. $complexNumber2 = Functions::flattenSingleValue($complexNumber2);
  2056. try {
  2057. return (string) (new Complex($complexNumber1))->subtract(new Complex($complexNumber2));
  2058. } catch (ComplexException $e) {
  2059. return Functions::NAN();
  2060. }
  2061. }
  2062. /**
  2063. * IMSUM.
  2064. *
  2065. * Returns the sum of two or more complex numbers in x + yi or x + yj text format.
  2066. *
  2067. * Excel Function:
  2068. * IMSUM(complexNumber[,complexNumber[,...]])
  2069. *
  2070. * @param string ...$complexNumbers Series of complex numbers to add
  2071. *
  2072. * @return string
  2073. */
  2074. public static function IMSUM(...$complexNumbers)
  2075. {
  2076. // Return value
  2077. $returnValue = new Complex(0.0);
  2078. $aArgs = Functions::flattenArray($complexNumbers);
  2079. try {
  2080. // Loop through the arguments
  2081. foreach ($aArgs as $complex) {
  2082. $returnValue = $returnValue->add(new Complex($complex));
  2083. }
  2084. } catch (ComplexException $e) {
  2085. return Functions::NAN();
  2086. }
  2087. return (string) $returnValue;
  2088. }
  2089. /**
  2090. * IMPRODUCT.
  2091. *
  2092. * Returns the product of two or more complex numbers in x + yi or x + yj text format.
  2093. *
  2094. * Excel Function:
  2095. * IMPRODUCT(complexNumber[,complexNumber[,...]])
  2096. *
  2097. * @param string ...$complexNumbers Series of complex numbers to multiply
  2098. *
  2099. * @return string
  2100. */
  2101. public static function IMPRODUCT(...$complexNumbers)
  2102. {
  2103. // Return value
  2104. $returnValue = new Complex(1.0);
  2105. $aArgs = Functions::flattenArray($complexNumbers);
  2106. try {
  2107. // Loop through the arguments
  2108. foreach ($aArgs as $complex) {
  2109. $returnValue = $returnValue->multiply(new Complex($complex));
  2110. }
  2111. } catch (ComplexException $e) {
  2112. return Functions::NAN();
  2113. }
  2114. return (string) $returnValue;
  2115. }
  2116. /**
  2117. * DELTA.
  2118. *
  2119. * Tests whether two values are equal. Returns 1 if number1 = number2; returns 0 otherwise.
  2120. * Use this function to filter a set of values. For example, by summing several DELTA
  2121. * functions you calculate the count of equal pairs. This function is also known as the
  2122. * Kronecker Delta function.
  2123. *
  2124. * Excel Function:
  2125. * DELTA(a[,b])
  2126. *
  2127. * @param float $a the first number
  2128. * @param float $b The second number. If omitted, b is assumed to be zero.
  2129. *
  2130. * @return int
  2131. */
  2132. public static function DELTA($a, $b = 0)
  2133. {
  2134. $a = Functions::flattenSingleValue($a);
  2135. $b = Functions::flattenSingleValue($b);
  2136. return (int) ($a == $b);
  2137. }
  2138. /**
  2139. * GESTEP.
  2140. *
  2141. * Excel Function:
  2142. * GESTEP(number[,step])
  2143. *
  2144. * Returns 1 if number >= step; returns 0 (zero) otherwise
  2145. * Use this function to filter a set of values. For example, by summing several GESTEP
  2146. * functions you calculate the count of values that exceed a threshold.
  2147. *
  2148. * @param float $number the value to test against step
  2149. * @param float $step The threshold value.
  2150. * If you omit a value for step, GESTEP uses zero.
  2151. *
  2152. * @return int
  2153. */
  2154. public static function GESTEP($number, $step = 0)
  2155. {
  2156. $number = Functions::flattenSingleValue($number);
  2157. $step = Functions::flattenSingleValue($step);
  2158. return (int) ($number >= $step);
  2159. }
  2160. //
  2161. // Private method to calculate the erf value
  2162. //
  2163. private static $twoSqrtPi = 1.128379167095512574;
  2164. public static function erfVal($x)
  2165. {
  2166. if (abs($x) > 2.2) {
  2167. return 1 - self::erfcVal($x);
  2168. }
  2169. $sum = $term = $x;
  2170. $xsqr = ($x * $x);
  2171. $j = 1;
  2172. do {
  2173. $term *= $xsqr / $j;
  2174. $sum -= $term / (2 * $j + 1);
  2175. ++$j;
  2176. $term *= $xsqr / $j;
  2177. $sum += $term / (2 * $j + 1);
  2178. ++$j;
  2179. if ($sum == 0.0) {
  2180. break;
  2181. }
  2182. } while (abs($term / $sum) > Functions::PRECISION);
  2183. return self::$twoSqrtPi * $sum;
  2184. }
  2185. /**
  2186. * Validate arguments passed to the bitwise functions.
  2187. *
  2188. * @param mixed $value
  2189. *
  2190. * @throws Exception
  2191. *
  2192. * @return int
  2193. */
  2194. private static function validateBitwiseArgument($value)
  2195. {
  2196. $value = Functions::flattenSingleValue($value);
  2197. if (is_int($value)) {
  2198. return $value;
  2199. } elseif (is_numeric($value)) {
  2200. if ($value == (int) ($value)) {
  2201. $value = (int) ($value);
  2202. if (($value > pow(2, 48) - 1) || ($value < 0)) {
  2203. throw new Exception(Functions::NAN());
  2204. }
  2205. return $value;
  2206. }
  2207. throw new Exception(Functions::NAN());
  2208. }
  2209. throw new Exception(Functions::VALUE());
  2210. }
  2211. /**
  2212. * BITAND.
  2213. *
  2214. * Returns the bitwise AND of two integer values.
  2215. *
  2216. * Excel Function:
  2217. * BITAND(number1, number2)
  2218. *
  2219. * @category Engineering Functions
  2220. *
  2221. * @param int $number1
  2222. * @param int $number2
  2223. *
  2224. * @return int|string
  2225. */
  2226. public static function BITAND($number1, $number2)
  2227. {
  2228. try {
  2229. $number1 = self::validateBitwiseArgument($number1);
  2230. $number2 = self::validateBitwiseArgument($number2);
  2231. } catch (Exception $e) {
  2232. return $e->getMessage();
  2233. }
  2234. return $number1 & $number2;
  2235. }
  2236. /**
  2237. * BITOR.
  2238. *
  2239. * Returns the bitwise OR of two integer values.
  2240. *
  2241. * Excel Function:
  2242. * BITOR(number1, number2)
  2243. *
  2244. * @category Engineering Functions
  2245. *
  2246. * @param int $number1
  2247. * @param int $number2
  2248. *
  2249. * @return int|string
  2250. */
  2251. public static function BITOR($number1, $number2)
  2252. {
  2253. try {
  2254. $number1 = self::validateBitwiseArgument($number1);
  2255. $number2 = self::validateBitwiseArgument($number2);
  2256. } catch (Exception $e) {
  2257. return $e->getMessage();
  2258. }
  2259. return $number1 | $number2;
  2260. }
  2261. /**
  2262. * BITXOR.
  2263. *
  2264. * Returns the bitwise XOR of two integer values.
  2265. *
  2266. * Excel Function:
  2267. * BITXOR(number1, number2)
  2268. *
  2269. * @category Engineering Functions
  2270. *
  2271. * @param int $number1
  2272. * @param int $number2
  2273. *
  2274. * @return int|string
  2275. */
  2276. public static function BITXOR($number1, $number2)
  2277. {
  2278. try {
  2279. $number1 = self::validateBitwiseArgument($number1);
  2280. $number2 = self::validateBitwiseArgument($number2);
  2281. } catch (Exception $e) {
  2282. return $e->getMessage();
  2283. }
  2284. return $number1 ^ $number2;
  2285. }
  2286. /**
  2287. * BITLSHIFT.
  2288. *
  2289. * Returns the number value shifted left by shift_amount bits.
  2290. *
  2291. * Excel Function:
  2292. * BITLSHIFT(number, shift_amount)
  2293. *
  2294. * @category Engineering Functions
  2295. *
  2296. * @param int $number
  2297. * @param int $shiftAmount
  2298. *
  2299. * @return int|string
  2300. */
  2301. public static function BITLSHIFT($number, $shiftAmount)
  2302. {
  2303. try {
  2304. $number = self::validateBitwiseArgument($number);
  2305. } catch (Exception $e) {
  2306. return $e->getMessage();
  2307. }
  2308. $shiftAmount = Functions::flattenSingleValue($shiftAmount);
  2309. $result = $number << $shiftAmount;
  2310. if ($result > pow(2, 48) - 1) {
  2311. return Functions::NAN();
  2312. }
  2313. return $result;
  2314. }
  2315. /**
  2316. * BITRSHIFT.
  2317. *
  2318. * Returns the number value shifted right by shift_amount bits.
  2319. *
  2320. * Excel Function:
  2321. * BITRSHIFT(number, shift_amount)
  2322. *
  2323. * @category Engineering Functions
  2324. *
  2325. * @param int $number
  2326. * @param int $shiftAmount
  2327. *
  2328. * @return int|string
  2329. */
  2330. public static function BITRSHIFT($number, $shiftAmount)
  2331. {
  2332. try {
  2333. $number = self::validateBitwiseArgument($number);
  2334. } catch (Exception $e) {
  2335. return $e->getMessage();
  2336. }
  2337. $shiftAmount = Functions::flattenSingleValue($shiftAmount);
  2338. return $number >> $shiftAmount;
  2339. }
  2340. /**
  2341. * ERF.
  2342. *
  2343. * Returns the error function integrated between the lower and upper bound arguments.
  2344. *
  2345. * Note: In Excel 2007 or earlier, if you input a negative value for the upper or lower bound arguments,
  2346. * the function would return a #NUM! error. However, in Excel 2010, the function algorithm was
  2347. * improved, so that it can now calculate the function for both positive and negative ranges.
  2348. * PhpSpreadsheet follows Excel 2010 behaviour, and accepts negative arguments.
  2349. *
  2350. * Excel Function:
  2351. * ERF(lower[,upper])
  2352. *
  2353. * @param float $lower lower bound for integrating ERF
  2354. * @param float $upper upper bound for integrating ERF.
  2355. * If omitted, ERF integrates between zero and lower_limit
  2356. *
  2357. * @return float|string
  2358. */
  2359. public static function ERF($lower, $upper = null)
  2360. {
  2361. $lower = Functions::flattenSingleValue($lower);
  2362. $upper = Functions::flattenSingleValue($upper);
  2363. if (is_numeric($lower)) {
  2364. if ($upper === null) {
  2365. return self::erfVal($lower);
  2366. }
  2367. if (is_numeric($upper)) {
  2368. return self::erfVal($upper) - self::erfVal($lower);
  2369. }
  2370. }
  2371. return Functions::VALUE();
  2372. }
  2373. /**
  2374. * ERFPRECISE.
  2375. *
  2376. * Returns the error function integrated between the lower and upper bound arguments.
  2377. *
  2378. * Excel Function:
  2379. * ERF.PRECISE(limit)
  2380. *
  2381. * @param float $limit bound for integrating ERF
  2382. *
  2383. * @return float|string
  2384. */
  2385. public static function ERFPRECISE($limit)
  2386. {
  2387. $limit = Functions::flattenSingleValue($limit);
  2388. return self::ERF($limit);
  2389. }
  2390. //
  2391. // Private method to calculate the erfc value
  2392. //
  2393. private static $oneSqrtPi = 0.564189583547756287;
  2394. private static function erfcVal($x)
  2395. {
  2396. if (abs($x) < 2.2) {
  2397. return 1 - self::erfVal($x);
  2398. }
  2399. if ($x < 0) {
  2400. return 2 - self::ERFC(-$x);
  2401. }
  2402. $a = $n = 1;
  2403. $b = $c = $x;
  2404. $d = ($x * $x) + 0.5;
  2405. $q1 = $q2 = $b / $d;
  2406. $t = 0;
  2407. do {
  2408. $t = $a * $n + $b * $x;
  2409. $a = $b;
  2410. $b = $t;
  2411. $t = $c * $n + $d * $x;
  2412. $c = $d;
  2413. $d = $t;
  2414. $n += 0.5;
  2415. $q1 = $q2;
  2416. $q2 = $b / $d;
  2417. } while ((abs($q1 - $q2) / $q2) > Functions::PRECISION);
  2418. return self::$oneSqrtPi * exp(-$x * $x) * $q2;
  2419. }
  2420. /**
  2421. * ERFC.
  2422. *
  2423. * Returns the complementary ERF function integrated between x and infinity
  2424. *
  2425. * Note: In Excel 2007 or earlier, if you input a negative value for the lower bound argument,
  2426. * the function would return a #NUM! error. However, in Excel 2010, the function algorithm was
  2427. * improved, so that it can now calculate the function for both positive and negative x values.
  2428. * PhpSpreadsheet follows Excel 2010 behaviour, and accepts nagative arguments.
  2429. *
  2430. * Excel Function:
  2431. * ERFC(x)
  2432. *
  2433. * @param float $x The lower bound for integrating ERFC
  2434. *
  2435. * @return float|string
  2436. */
  2437. public static function ERFC($x)
  2438. {
  2439. $x = Functions::flattenSingleValue($x);
  2440. if (is_numeric($x)) {
  2441. return self::erfcVal($x);
  2442. }
  2443. return Functions::VALUE();
  2444. }
  2445. /**
  2446. * getConversionGroups
  2447. * Returns a list of the different conversion groups for UOM conversions.
  2448. *
  2449. * @return array
  2450. */
  2451. public static function getConversionGroups()
  2452. {
  2453. $conversionGroups = [];
  2454. foreach (self::$conversionUnits as $conversionUnit) {
  2455. $conversionGroups[] = $conversionUnit['Group'];
  2456. }
  2457. return array_merge(array_unique($conversionGroups));
  2458. }
  2459. /**
  2460. * getConversionGroupUnits
  2461. * Returns an array of units of measure, for a specified conversion group, or for all groups.
  2462. *
  2463. * @param string $group The group whose units of measure you want to retrieve
  2464. *
  2465. * @return array
  2466. */
  2467. public static function getConversionGroupUnits($group = null)
  2468. {
  2469. $conversionGroups = [];
  2470. foreach (self::$conversionUnits as $conversionUnit => $conversionGroup) {
  2471. if (($group === null) || ($conversionGroup['Group'] == $group)) {
  2472. $conversionGroups[$conversionGroup['Group']][] = $conversionUnit;
  2473. }
  2474. }
  2475. return $conversionGroups;
  2476. }
  2477. /**
  2478. * getConversionGroupUnitDetails.
  2479. *
  2480. * @param string $group The group whose units of measure you want to retrieve
  2481. *
  2482. * @return array
  2483. */
  2484. public static function getConversionGroupUnitDetails($group = null)
  2485. {
  2486. $conversionGroups = [];
  2487. foreach (self::$conversionUnits as $conversionUnit => $conversionGroup) {
  2488. if (($group === null) || ($conversionGroup['Group'] == $group)) {
  2489. $conversionGroups[$conversionGroup['Group']][] = [
  2490. 'unit' => $conversionUnit,
  2491. 'description' => $conversionGroup['Unit Name'],
  2492. ];
  2493. }
  2494. }
  2495. return $conversionGroups;
  2496. }
  2497. /**
  2498. * getConversionMultipliers
  2499. * Returns an array of the Multiplier prefixes that can be used with Units of Measure in CONVERTUOM().
  2500. *
  2501. * @return array of mixed
  2502. */
  2503. public static function getConversionMultipliers()
  2504. {
  2505. return self::$conversionMultipliers;
  2506. }
  2507. /**
  2508. * CONVERTUOM.
  2509. *
  2510. * Converts a number from one measurement system to another.
  2511. * For example, CONVERT can translate a table of distances in miles to a table of distances
  2512. * in kilometers.
  2513. *
  2514. * Excel Function:
  2515. * CONVERT(value,fromUOM,toUOM)
  2516. *
  2517. * @param float $value the value in fromUOM to convert
  2518. * @param string $fromUOM the units for value
  2519. * @param string $toUOM the units for the result
  2520. *
  2521. * @return float
  2522. */
  2523. public static function CONVERTUOM($value, $fromUOM, $toUOM)
  2524. {
  2525. $value = Functions::flattenSingleValue($value);
  2526. $fromUOM = Functions::flattenSingleValue($fromUOM);
  2527. $toUOM = Functions::flattenSingleValue($toUOM);
  2528. if (!is_numeric($value)) {
  2529. return Functions::VALUE();
  2530. }
  2531. $fromMultiplier = 1.0;
  2532. if (isset(self::$conversionUnits[$fromUOM])) {
  2533. $unitGroup1 = self::$conversionUnits[$fromUOM]['Group'];
  2534. } else {
  2535. $fromMultiplier = substr($fromUOM, 0, 1);
  2536. $fromUOM = substr($fromUOM, 1);
  2537. if (isset(self::$conversionMultipliers[$fromMultiplier])) {
  2538. $fromMultiplier = self::$conversionMultipliers[$fromMultiplier]['multiplier'];
  2539. } else {
  2540. return Functions::NA();
  2541. }
  2542. if ((isset(self::$conversionUnits[$fromUOM])) && (self::$conversionUnits[$fromUOM]['AllowPrefix'])) {
  2543. $unitGroup1 = self::$conversionUnits[$fromUOM]['Group'];
  2544. } else {
  2545. return Functions::NA();
  2546. }
  2547. }
  2548. $value *= $fromMultiplier;
  2549. $toMultiplier = 1.0;
  2550. if (isset(self::$conversionUnits[$toUOM])) {
  2551. $unitGroup2 = self::$conversionUnits[$toUOM]['Group'];
  2552. } else {
  2553. $toMultiplier = substr($toUOM, 0, 1);
  2554. $toUOM = substr($toUOM, 1);
  2555. if (isset(self::$conversionMultipliers[$toMultiplier])) {
  2556. $toMultiplier = self::$conversionMultipliers[$toMultiplier]['multiplier'];
  2557. } else {
  2558. return Functions::NA();
  2559. }
  2560. if ((isset(self::$conversionUnits[$toUOM])) && (self::$conversionUnits[$toUOM]['AllowPrefix'])) {
  2561. $unitGroup2 = self::$conversionUnits[$toUOM]['Group'];
  2562. } else {
  2563. return Functions::NA();
  2564. }
  2565. }
  2566. if ($unitGroup1 != $unitGroup2) {
  2567. return Functions::NA();
  2568. }
  2569. if (($fromUOM == $toUOM) && ($fromMultiplier == $toMultiplier)) {
  2570. // We've already factored $fromMultiplier into the value, so we need
  2571. // to reverse it again
  2572. return $value / $fromMultiplier;
  2573. } elseif ($unitGroup1 == 'Temperature') {
  2574. if (($fromUOM == 'F') || ($fromUOM == 'fah')) {
  2575. if (($toUOM == 'F') || ($toUOM == 'fah')) {
  2576. return $value;
  2577. }
  2578. $value = (($value - 32) / 1.8);
  2579. if (($toUOM == 'K') || ($toUOM == 'kel')) {
  2580. $value += 273.15;
  2581. }
  2582. return $value;
  2583. } elseif ((($fromUOM == 'K') || ($fromUOM == 'kel')) &&
  2584. (($toUOM == 'K') || ($toUOM == 'kel'))
  2585. ) {
  2586. return $value;
  2587. } elseif ((($fromUOM == 'C') || ($fromUOM == 'cel')) &&
  2588. (($toUOM == 'C') || ($toUOM == 'cel'))
  2589. ) {
  2590. return $value;
  2591. }
  2592. if (($toUOM == 'F') || ($toUOM == 'fah')) {
  2593. if (($fromUOM == 'K') || ($fromUOM == 'kel')) {
  2594. $value -= 273.15;
  2595. }
  2596. return ($value * 1.8) + 32;
  2597. }
  2598. if (($toUOM == 'C') || ($toUOM == 'cel')) {
  2599. return $value - 273.15;
  2600. }
  2601. return $value + 273.15;
  2602. }
  2603. return ($value * self::$unitConversions[$unitGroup1][$fromUOM][$toUOM]) / $toMultiplier;
  2604. }
  2605. }