Engineering.php 101 KB

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