PageRenderTime 63ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/phpexcel/PHPExcel/Calculation/Engineering.php

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