PageRenderTime 61ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/PHPExcel/PHPExcel/Calculation/Engineering.php

https://bitbucket.org/thomashii/vtigercrm-6-for-postgresql
PHP | 2509 lines | 1505 code | 212 blank | 792 comment | 320 complexity | 858d083f65fc584cb91bfb0087335e9a MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, LGPL-2.1, GPL-2.0, GPL-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 1.7.7, 2012-05-19
  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, which is equivalent to the Bessel function evaluated for
  753. * 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. If n is not an integer, it is truncated.
  763. * If $ord is nonnumeric, BESSELI returns the #VALUE! error value.
  764. * If $ord < 0, BESSELI returns the #NUM! error value.
  765. * @return float
  766. *
  767. * @TODO Better handling of the approximation method to support the differences between Excel/Gnumeric and Open/Libre Office
  768. *
  769. */
  770. public static function BESSELI($x, $ord) {
  771. $x = (is_null($x)) ? 0.0 : PHPExcel_Calculation_Functions::flattenSingleValue($x);
  772. $ord = (is_null($ord)) ? 0.0 : PHPExcel_Calculation_Functions::flattenSingleValue($ord);
  773. if ((is_numeric($x)) && (is_numeric($ord))) {
  774. $ord = floor($ord);
  775. if ($ord < 0) {
  776. return PHPExcel_Calculation_Functions::NaN();
  777. }
  778. if (abs($x) <= 30) {
  779. $fResult = $fTerm = pow($x / 2, $ord) / PHPExcel_Calculation_MathTrig::FACT($ord);
  780. $ordK = 1;
  781. $fSqrX = ($x * $x) / 4;
  782. do {
  783. $fTerm *= $fSqrX;
  784. $fTerm /= ($ordK * ($ordK + $ord));
  785. $fResult += $fTerm;
  786. } while ((abs($fTerm) > 1e-12) && (++$ordK < 100));
  787. } else {
  788. $f_2_PI = 2 * M_PI;
  789. $fXAbs = abs($x);
  790. $fResult = exp($fXAbs) / sqrt($f_2_PI * $fXAbs);
  791. if (($ord & 1) && ($x < 0)) {
  792. $fResult = -$fResult;
  793. }
  794. }
  795. return (is_nan($fResult)) ? PHPExcel_Calculation_Functions::NaN() : $fResult;
  796. }
  797. return PHPExcel_Calculation_Functions::VALUE();
  798. } // function BESSELI()
  799. /**
  800. * BESSELJ
  801. *
  802. * Returns the Bessel function
  803. *
  804. * Excel Function:
  805. * BESSELJ(x,ord)
  806. *
  807. * @access public
  808. * @category Engineering Functions
  809. * @param float $x The value at which to evaluate the function.
  810. * If x is nonnumeric, BESSELJ returns the #VALUE! error value.
  811. * @param integer $ord The order of the Bessel function. If n is not an integer, it is truncated.
  812. * If $ord is nonnumeric, BESSELJ returns the #VALUE! error value.
  813. * If $ord < 0, BESSELJ returns the #NUM! error value.
  814. * @return float
  815. *
  816. * @TODO Better handling of the approximation method to support the differences between Excel/Gnumeric and Open/Libre Office
  817. *
  818. */
  819. public static function BESSELJ($x, $ord) {
  820. $x = (is_null($x)) ? 0.0 : PHPExcel_Calculation_Functions::flattenSingleValue($x);
  821. $ord = (is_null($ord)) ? 0.0 : PHPExcel_Calculation_Functions::flattenSingleValue($ord);
  822. if ((is_numeric($x)) && (is_numeric($ord))) {
  823. $ord = floor($ord);
  824. if ($ord < 0) {
  825. return PHPExcel_Calculation_Functions::NaN();
  826. }
  827. $fResult = 0;
  828. if (abs($x) <= 30) {
  829. $fResult = $fTerm = pow($x / 2, $ord) / PHPExcel_Calculation_MathTrig::FACT($ord);
  830. $ordK = 1;
  831. $fSqrX = ($x * $x) / -4;
  832. do {
  833. $fTerm *= $fSqrX;
  834. $fTerm /= ($ordK * ($ordK + $ord));
  835. $fResult += $fTerm;
  836. } while ((abs($fTerm) > 1e-12) && (++$ordK < 100));
  837. } else {
  838. $f_PI_DIV_2 = M_PI / 2;
  839. $f_PI_DIV_4 = M_PI / 4;
  840. $fXAbs = abs($x);
  841. $fResult = sqrt(M_2DIVPI / $fXAbs) * cos($fXAbs - $ord * $f_PI_DIV_2 - $f_PI_DIV_4);
  842. if (($ord & 1) && ($x < 0)) {
  843. $fResult = -$fResult;
  844. }
  845. }
  846. return (is_nan($fResult)) ? PHPExcel_Calculation_Functions::NaN() : $fResult;
  847. }
  848. return PHPExcel_Calculation_Functions::VALUE();
  849. } // function BESSELJ()
  850. private static function _Besselk0($fNum) {
  851. if ($fNum <= 2) {
  852. $fNum2 = $fNum * 0.5;
  853. $y = ($fNum2 * $fNum2);
  854. $fRet = -log($fNum2) * self::BESSELI($fNum, 0) +
  855. (-0.57721566 + $y * (0.42278420 + $y * (0.23069756 + $y * (0.3488590e-1 + $y * (0.262698e-2 + $y *
  856. (0.10750e-3 + $y * 0.74e-5))))));
  857. } else {
  858. $y = 2 / $fNum;
  859. $fRet = exp(-$fNum) / sqrt($fNum) *
  860. (1.25331414 + $y * (-0.7832358e-1 + $y * (0.2189568e-1 + $y * (-0.1062446e-1 + $y *
  861. (0.587872e-2 + $y * (-0.251540e-2 + $y * 0.53208e-3))))));
  862. }
  863. return $fRet;
  864. } // function _Besselk0()
  865. private static function _Besselk1($fNum) {
  866. if ($fNum <= 2) {
  867. $fNum2 = $fNum * 0.5;
  868. $y = ($fNum2 * $fNum2);
  869. $fRet = log($fNum2) * self::BESSELI($fNum, 1) +
  870. (1 + $y * (0.15443144 + $y * (-0.67278579 + $y * (-0.18156897 + $y * (-0.1919402e-1 + $y *
  871. (-0.110404e-2 + $y * (-0.4686e-4))))))) / $fNum;
  872. } else {
  873. $y = 2 / $fNum;
  874. $fRet = exp(-$fNum) / sqrt($fNum) *
  875. (1.25331414 + $y * (0.23498619 + $y * (-0.3655620e-1 + $y * (0.1504268e-1 + $y * (-0.780353e-2 + $y *
  876. (0.325614e-2 + $y * (-0.68245e-3)))))));
  877. }
  878. return $fRet;
  879. } // function _Besselk1()
  880. /**
  881. * BESSELK
  882. *
  883. * Returns the modified Bessel function, which is equivalent to the Bessel functions evaluated
  884. * for purely imaginary arguments.
  885. *
  886. * Excel Function:
  887. * BESSELK(x,ord)
  888. *
  889. * @access public
  890. * @category Engineering Functions
  891. * @param float $x The value at which to evaluate the function.
  892. * If x is nonnumeric, BESSELK returns the #VALUE! error value.
  893. * @param integer $ord The order of the Bessel function. If n is not an integer, it is truncated.
  894. * If $ord is nonnumeric, BESSELK returns the #VALUE! error value.
  895. * If $ord < 0, BESSELK returns the #NUM! error value.
  896. * @return float
  897. *
  898. * @TODO Better handling of the approximation method to support the differences between Excel/Gnumeric and Open/Libre Office
  899. *
  900. */
  901. public static function BESSELK($x, $ord) {
  902. $x = (is_null($x)) ? 0.0 : PHPExcel_Calculation_Functions::flattenSingleValue($x);
  903. $ord = (is_null($ord)) ? 0.0 : PHPExcel_Calculation_Functions::flattenSingleValue($ord);
  904. if ((is_numeric($x)) && (is_numeric($ord))) {
  905. if (($ord < 0) || ($x == 0.0)) {
  906. return PHPExcel_Calculation_Functions::NaN();
  907. }
  908. switch(floor($ord)) {
  909. case 0 : return self::_Besselk0($x);
  910. break;
  911. case 1 : return self::_Besselk1($x);
  912. break;
  913. default : $fTox = 2 / $x;
  914. $fBkm = self::_Besselk0($x);
  915. $fBk = self::_Besselk1($x);
  916. for ($n = 1; $n < $ord; ++$n) {
  917. $fBkp = $fBkm + $n * $fTox * $fBk;
  918. $fBkm = $fBk;
  919. $fBk = $fBkp;
  920. }
  921. }
  922. return (is_nan($fBk)) ? PHPExcel_Calculation_Functions::NaN() : $fBk;
  923. }
  924. return PHPExcel_Calculation_Functions::VALUE();
  925. } // function BESSELK()
  926. private static function _Bessely0($fNum) {
  927. if ($fNum < 8.0) {
  928. $y = ($fNum * $fNum);
  929. $f1 = -2957821389.0 + $y * (7062834065.0 + $y * (-512359803.6 + $y * (10879881.29 + $y * (-86327.92757 + $y * 228.4622733))));
  930. $f2 = 40076544269.0 + $y * (745249964.8 + $y * (7189466.438 + $y * (47447.26470 + $y * (226.1030244 + $y))));
  931. $fRet = $f1 / $f2 + 0.636619772 * self::BESSELJ($fNum, 0) * log($fNum);
  932. } else {
  933. $z = 8.0 / $fNum;
  934. $y = ($z * $z);
  935. $xx = $fNum - 0.785398164;
  936. $f1 = 1 + $y * (-0.1098628627e-2 + $y * (0.2734510407e-4 + $y * (-0.2073370639e-5 + $y * 0.2093887211e-6)));
  937. $f2 = -0.1562499995e-1 + $y * (0.1430488765e-3 + $y * (-0.6911147651e-5 + $y * (0.7621095161e-6 + $y * (-0.934945152e-7))));
  938. $fRet = sqrt(0.636619772 / $fNum) * (sin($xx) * $f1 + $z * cos($xx) * $f2);
  939. }
  940. return $fRet;
  941. } // function _Bessely0()
  942. private static function _Bessely1($fNum) {
  943. if ($fNum < 8.0) {
  944. $y = ($fNum * $fNum);
  945. $f1 = $fNum * (-0.4900604943e13 + $y * (0.1275274390e13 + $y * (-0.5153438139e11 + $y * (0.7349264551e9 + $y *
  946. (-0.4237922726e7 + $y * 0.8511937935e4)))));
  947. $f2 = 0.2499580570e14 + $y * (0.4244419664e12 + $y * (0.3733650367e10 + $y * (0.2245904002e8 + $y *
  948. (0.1020426050e6 + $y * (0.3549632885e3 + $y)))));
  949. $fRet = $f1 / $f2 + 0.636619772 * ( self::BESSELJ($fNum, 1) * log($fNum) - 1 / $fNum);
  950. } else {
  951. // $z = 8.0 / $fNum;
  952. // $y = ($z * $z);
  953. // $xx = $fNum - 2.356194491;
  954. // $f1 = 1 + $y * (0.183105e-2 + $y * (-0.3516396496e-4 + $y * (0.2457520174e-5 + $y * (-0.240337019e6))));
  955. // $f2 = 0.04687499995 + $y * (-0.2002690873e-3 + $y * (0.8449199096e-5 + $y * (-0.88228987e-6 + $y * 0.105787412e-6)));
  956. // $fRet = sqrt(0.636619772 / $fNum) * (sin($xx) * $f1 + $z * cos($xx) * $f2);
  957. #i12430# ...but this seems to work much better.
  958. $fRet = sqrt(0.636619772 / $fNum) * sin($fNum - 2.356194491);
  959. }
  960. return $fRet;
  961. } // function _Bessely1()
  962. /**
  963. * BESSELY
  964. *
  965. * Returns the Bessel function, which is also called the Weber function or the Neumann function.
  966. *
  967. * Excel Function:
  968. * BESSELY(x,ord)
  969. *
  970. * @access public
  971. * @category Engineering Functions
  972. * @param float $x The value at which to evaluate the function.
  973. * If x is nonnumeric, BESSELK returns the #VALUE! error value.
  974. * @param integer $ord The order of the Bessel function. If n is not an integer, it is truncated.
  975. * If $ord is nonnumeric, BESSELK returns the #VALUE! error value.
  976. * If $ord < 0, BESSELK returns the #NUM! error value.
  977. *
  978. * @TODO Better handling of the approximation method to support the differences between Excel/Gnumeric and Open/Libre Office
  979. *
  980. * @return float
  981. */
  982. public static function BESSELY($x, $ord) {
  983. $x = (is_null($x)) ? 0.0 : PHPExcel_Calculation_Functions::flattenSingleValue($x);
  984. $ord = (is_null($ord)) ? 0.0 : PHPExcel_Calculation_Functions::flattenSingleValue($ord);
  985. if ((is_numeric($x)) && (is_numeric($ord))) {
  986. if (($ord < 0) || ($x == 0.0)) {
  987. return PHPExcel_Calculation_Functions::NaN();
  988. }
  989. switch(floor($ord)) {
  990. case 0 : return self::_Bessely0($x);
  991. break;
  992. case 1 : return self::_Bessely1($x);
  993. break;
  994. default: $fTox = 2 / $x;
  995. $fBym = self::_Bessely0($x);
  996. $fBy = self::_Bessely1($x);
  997. for ($n = 1; $n < $ord; ++$n) {
  998. $fByp = $n * $fTox * $fBy - $fBym;
  999. $fBym = $fBy;
  1000. $fBy = $fByp;
  1001. }
  1002. }
  1003. return (is_nan($fBy)) ? PHPExcel_Calculation_Functions::NaN() : $fBy;
  1004. }
  1005. return PHPExcel_Calculation_Functions::VALUE();
  1006. } // function BESSELY()
  1007. /**
  1008. * BINTODEC
  1009. *
  1010. * Return a binary value as decimal.
  1011. *
  1012. * Excel Function:
  1013. * BIN2DEC(x)
  1014. *
  1015. * @access public
  1016. * @category Engineering Functions
  1017. * @param string $x The binary number (as a string) that you want to convert. The number
  1018. * cannot contain more than 10 characters (10 bits). The most significant
  1019. * bit of number is the sign bit. The remaining 9 bits are magnitude bits.
  1020. * Negative numbers are represented using two's-complement notation.
  1021. * If number is not a valid binary number, or if number contains more than
  1022. * 10 characters (10 bits), BIN2DEC returns the #NUM! error value.
  1023. * @return string
  1024. */
  1025. public static function BINTODEC($x) {
  1026. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1027. if (is_bool($x)) {
  1028. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
  1029. $x = (int) $x;
  1030. } else {
  1031. return PHPExcel_Calculation_Functions::VALUE();
  1032. }
  1033. }
  1034. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  1035. $x = floor($x);
  1036. }
  1037. $x = (string) $x;
  1038. if (strlen($x) > preg_match_all('/[01]/',$x,$out)) {
  1039. return PHPExcel_Calculation_Functions::NaN();
  1040. }
  1041. if (strlen($x) > 10) {
  1042. return PHPExcel_Calculation_Functions::NaN();
  1043. } elseif (strlen($x) == 10) {
  1044. // Two's Complement
  1045. $x = substr($x,-9);
  1046. return '-'.(512-bindec($x));
  1047. }
  1048. return bindec($x);
  1049. } // function BINTODEC()
  1050. /**
  1051. * BINTOHEX
  1052. *
  1053. * Return a binary value as hex.
  1054. *
  1055. * Excel Function:
  1056. * BIN2HEX(x[,places])
  1057. *
  1058. * @access public
  1059. * @category Engineering Functions
  1060. * @param string $x The binary number (as a string) that you want to convert. The number
  1061. * cannot contain more than 10 characters (10 bits). The most significant
  1062. * bit of number is the sign bit. The remaining 9 bits are magnitude bits.
  1063. * Negative numbers are represented using two's-complement notation.
  1064. * If number is not a valid binary number, or if number contains more than
  1065. * 10 characters (10 bits), BIN2HEX returns the #NUM! error value.
  1066. * @param integer $places The number of characters to use. If places is omitted, BIN2HEX uses the
  1067. * minimum number of characters necessary. Places is useful for padding the
  1068. * return value with leading 0s (zeros).
  1069. * If places is not an integer, it is truncated.
  1070. * If places is nonnumeric, BIN2HEX returns the #VALUE! error value.
  1071. * If places is negative, BIN2HEX returns the #NUM! error value.
  1072. * @return string
  1073. */
  1074. public static function BINTOHEX($x, $places=NULL) {
  1075. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1076. $places = PHPExcel_Calculation_Functions::flattenSingleValue($places);
  1077. if (is_bool($x)) {
  1078. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
  1079. $x = (int) $x;
  1080. } else {
  1081. return PHPExcel_Calculation_Functions::VALUE();
  1082. }
  1083. }
  1084. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  1085. $x = floor($x);
  1086. }
  1087. $x = (string) $x;
  1088. if (strlen($x) > preg_match_all('/[01]/',$x,$out)) {
  1089. return PHPExcel_Calculation_Functions::NaN();
  1090. }
  1091. if (strlen($x) > 10) {
  1092. return PHPExcel_Calculation_Functions::NaN();
  1093. } elseif (strlen($x) == 10) {
  1094. // Two's Complement
  1095. return str_repeat('F',8).substr(strtoupper(dechex(bindec(substr($x,-9)))),-2);
  1096. }
  1097. $hexVal = (string) strtoupper(dechex(bindec($x)));
  1098. return self::_nbrConversionFormat($hexVal,$places);
  1099. } // function BINTOHEX()
  1100. /**
  1101. * BINTOOCT
  1102. *
  1103. * Return a binary value as octal.
  1104. *
  1105. * Excel Function:
  1106. * BIN2OCT(x[,places])
  1107. *
  1108. * @access public
  1109. * @category Engineering Functions
  1110. * @param string $x The binary number (as a string) that you want to convert. The number
  1111. * cannot contain more than 10 characters (10 bits). The most significant
  1112. * bit of number is the sign bit. The remaining 9 bits are magnitude bits.
  1113. * Negative numbers are represented using two's-complement notation.
  1114. * If number is not a valid binary number, or if number contains more than
  1115. * 10 characters (10 bits), BIN2OCT returns the #NUM! error value.
  1116. * @param integer $places The number of characters to use. If places is omitted, BIN2OCT uses the
  1117. * minimum number of characters necessary. Places is useful for padding the
  1118. * return value with leading 0s (zeros).
  1119. * If places is not an integer, it is truncated.
  1120. * If places is nonnumeric, BIN2OCT returns the #VALUE! error value.
  1121. * If places is negative, BIN2OCT returns the #NUM! error value.
  1122. * @return string
  1123. */
  1124. public static function BINTOOCT($x, $places=NULL) {
  1125. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1126. $places = PHPExcel_Calculation_Functions::flattenSingleValue($places);
  1127. if (is_bool($x)) {
  1128. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
  1129. $x = (int) $x;
  1130. } else {
  1131. return PHPExcel_Calculation_Functions::VALUE();
  1132. }
  1133. }
  1134. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_GNUMERIC) {
  1135. $x = floor($x);
  1136. }
  1137. $x = (string) $x;
  1138. if (strlen($x) > preg_match_all('/[01]/',$x,$out)) {
  1139. return PHPExcel_Calculation_Functions::NaN();
  1140. }
  1141. if (strlen($x) > 10) {
  1142. return PHPExcel_Calculation_Functions::NaN();
  1143. } elseif (strlen($x) == 10) {
  1144. // Two's Complement
  1145. return str_repeat('7',7).substr(strtoupper(decoct(bindec(substr($x,-9)))),-3);
  1146. }
  1147. $octVal = (string) decoct(bindec($x));
  1148. return self::_nbrConversionFormat($octVal,$places);
  1149. } // function BINTOOCT()
  1150. /**
  1151. * DECTOBIN
  1152. *
  1153. * Return a decimal value as binary.
  1154. *
  1155. * Excel Function:
  1156. * DEC2BIN(x[,places])
  1157. *
  1158. * @access public
  1159. * @category Engineering Functions
  1160. * @param string $x The decimal integer you want to convert. If number is negative,
  1161. * valid place values are ignored and DEC2BIN returns a 10-character
  1162. * (10-bit) binary number in which the most significant bit is the sign
  1163. * bit. The remaining 9 bits are magnitude bits. Negative numbers are
  1164. * represented using two's-complement notation.
  1165. * If number < -512 or if number > 511, DEC2BIN returns the #NUM! error
  1166. * value.
  1167. * If number is nonnumeric, DEC2BIN returns the #VALUE! error value.
  1168. * If DEC2BIN requires more than places characters, it returns the #NUM!
  1169. * error value.
  1170. * @param integer $places The number of characters to use. If places is omitted, DEC2BIN uses
  1171. * the minimum number of characters necessary. Places is useful for
  1172. * padding the return value with leading 0s (zeros).
  1173. * If places is not an integer, it is truncated.
  1174. * If places is nonnumeric, DEC2BIN returns the #VALUE! error value.
  1175. * If places is zero or negative, DEC2BIN returns the #NUM! error value.
  1176. * @return string
  1177. */
  1178. public static function DECTOBIN($x, $places=NULL) {
  1179. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1180. $places = PHPExcel_Calculation_Functions::flattenSingleValue($places);
  1181. if (is_bool($x)) {
  1182. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
  1183. $x = (int) $x;
  1184. } else {
  1185. return PHPExcel_Calculation_Functions::VALUE();
  1186. }
  1187. }
  1188. $x = (string) $x;
  1189. if (strlen($x) > preg_match_all('/[-0123456789.]/',$x,$out)) {
  1190. return PHPExcel_Calculation_Functions::VALUE();
  1191. }
  1192. $x = (string) floor($x);
  1193. $r = decbin($x);
  1194. if (strlen($r) == 32) {
  1195. // Two's Complement
  1196. $r = substr($r,-10);
  1197. } elseif (strlen($r) > 11) {
  1198. return PHPExcel_Calculation_Functions::NaN();
  1199. }
  1200. return self::_nbrConversionFormat($r,$places);
  1201. } // function DECTOBIN()
  1202. /**
  1203. * DECTOHEX
  1204. *
  1205. * Return a decimal value as hex.
  1206. *
  1207. * Excel Function:
  1208. * DEC2HEX(x[,places])
  1209. *
  1210. * @access public
  1211. * @category Engineering Functions
  1212. * @param string $x The decimal integer you want to convert. If number is negative,
  1213. * places is ignored and DEC2HEX returns a 10-character (40-bit)
  1214. * hexadecimal number in which the most significant bit is the sign
  1215. * bit. The remaining 39 bits are magnitude bits. Negative numbers
  1216. * are represented using two's-complement notation.
  1217. * If number < -549,755,813,888 or if number > 549,755,813,887,
  1218. * DEC2HEX returns the #NUM! error value.
  1219. * If number is nonnumeric, DEC2HEX returns the #VALUE! error value.
  1220. * If DEC2HEX requires more than places characters, it returns the
  1221. * #NUM! error value.
  1222. * @param integer $places The number of characters to use. If places is omitted, DEC2HEX uses
  1223. * the minimum number of characters necessary. Places is useful for
  1224. * padding the return value with leading 0s (zeros).
  1225. * If places is not an integer, it is truncated.
  1226. * If places is nonnumeric, DEC2HEX returns the #VALUE! error value.
  1227. * If places is zero or negative, DEC2HEX returns the #NUM! error value.
  1228. * @return string
  1229. */
  1230. public static function DECTOHEX($x, $places=null) {
  1231. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1232. $places = PHPExcel_Calculation_Functions::flattenSingleValue($places);
  1233. if (is_bool($x)) {
  1234. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
  1235. $x = (int) $x;
  1236. } else {
  1237. return PHPExcel_Calculation_Functions::VALUE();
  1238. }
  1239. }
  1240. $x = (string) $x;
  1241. if (strlen($x) > preg_match_all('/[-0123456789.]/',$x,$out)) {
  1242. return PHPExcel_Calculation_Functions::VALUE();
  1243. }
  1244. $x = (string) floor($x);
  1245. $r = strtoupper(dechex($x));
  1246. if (strlen($r) == 8) {
  1247. // Two's Complement
  1248. $r = 'FF'.$r;
  1249. }
  1250. return self::_nbrConversionFormat($r,$places);
  1251. } // function DECTOHEX()
  1252. /**
  1253. * DECTOOCT
  1254. *
  1255. * Return an decimal value as octal.
  1256. *
  1257. * Excel Function:
  1258. * DEC2OCT(x[,places])
  1259. *
  1260. * @access public
  1261. * @category Engineering Functions
  1262. * @param string $x The decimal integer you want to convert. If number is negative,
  1263. * places is ignored and DEC2OCT returns a 10-character (30-bit)
  1264. * octal number in which the most significant bit is the sign bit.
  1265. * The remaining 29 bits are magnitude bits. Negative numbers are
  1266. * represented using two's-complement notation.
  1267. * If number < -536,870,912 or if number > 536,870,911, DEC2OCT
  1268. * returns the #NUM! error value.
  1269. * If number is nonnumeric, DEC2OCT returns the #VALUE! error value.
  1270. * If DEC2OCT requires more than places characters, it returns the
  1271. * #NUM! error value.
  1272. * @param integer $places The number of characters to use. If places is omitted, DEC2OCT uses
  1273. * the minimum number of characters necessary. Places is useful for
  1274. * padding the return value with leading 0s (zeros).
  1275. * If places is not an integer, it is truncated.
  1276. * If places is nonnumeric, DEC2OCT returns the #VALUE! error value.
  1277. * If places is zero or negative, DEC2OCT returns the #NUM! error value.
  1278. * @return string
  1279. */
  1280. public static function DECTOOCT($x, $places=null) {
  1281. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1282. $places = PHPExcel_Calculation_Functions::flattenSingleValue($places);
  1283. if (is_bool($x)) {
  1284. if (PHPExcel_Calculation_Functions::getCompatibilityMode() == PHPExcel_Calculation_Functions::COMPATIBILITY_OPENOFFICE) {
  1285. $x = (int) $x;
  1286. } else {
  1287. return PHPExcel_Calculation_Functions::VALUE();
  1288. }
  1289. }
  1290. $x = (string) $x;
  1291. if (strlen($x) > preg_match_all('/[-0123456789.]/',$x,$out)) {
  1292. return PHPExcel_Calculation_Functions::VALUE();
  1293. }
  1294. $x = (string) floor($x);
  1295. $r = decoct($x);
  1296. if (strlen($r) == 11) {
  1297. // Two's Complement
  1298. $r = substr($r,-10);
  1299. }
  1300. return self::_nbrConversionFormat($r,$places);
  1301. } // function DECTOOCT()
  1302. /**
  1303. * HEXTOBIN
  1304. *
  1305. * Return a hex value as binary.
  1306. *
  1307. * Excel Function:
  1308. * HEX2BIN(x[,places])
  1309. *
  1310. * @access public
  1311. * @category Engineering Functions
  1312. * @param string $x the hexadecimal number you want to convert. Number cannot
  1313. * contain more than 10 characters. The most significant bit of
  1314. * number is the sign bit (40th bit from the right). The remaining
  1315. * 9 bits are magnitude bits. Negative numbers are represented
  1316. * using two's-complement notation.
  1317. * If number is negative, HEX2BIN ignores places and returns a
  1318. * 10-character binary number.
  1319. * If number is negative, it cannot be less than FFFFFFFE00, and
  1320. * if number is positive, it cannot be greater than 1FF.
  1321. * If number is not a valid hexadecimal number, HEX2BIN returns
  1322. * the #NUM! error value.
  1323. * If HEX2BIN requires more than places characters, it returns
  1324. * the #NUM! error value.
  1325. * @param integer $places The number of characters to use. If places is omitted,
  1326. * HEX2BIN uses the minimum number of characters necessary. Places
  1327. * is useful for padding the return value with leading 0s (zeros).
  1328. * If places is not an integer, it is truncated.
  1329. * If places is nonnumeric, HEX2BIN returns the #VALUE! error value.
  1330. * If places is negative, HEX2BIN returns the #NUM! error value.
  1331. * @return string
  1332. */
  1333. public static function HEXTOBIN($x, $places=null) {
  1334. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1335. $places = PHPExcel_Calculation_Functions::flattenSingleValue($places);
  1336. if (is_bool($x)) {
  1337. return PHPExcel_Calculation_Functions::VALUE();
  1338. }
  1339. $x = (string) $x;
  1340. if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/',strtoupper($x),$out)) {
  1341. return PHPExcel_Calculation_Functions::NaN();
  1342. }
  1343. $binVal = decbin(hexdec($x));
  1344. return substr(self::_nbrConversionFormat($binVal,$places),-10);
  1345. } // function HEXTOBIN()
  1346. /**
  1347. * HEXTODEC
  1348. *
  1349. * Return a hex value as decimal.
  1350. *
  1351. * Excel Function:
  1352. * HEX2DEC(x)
  1353. *
  1354. * @access public
  1355. * @category Engineering Functions
  1356. * @param string $x The hexadecimal number you want to convert. This number cannot
  1357. * contain more than 10 characters (40 bits). The most significant
  1358. * bit of number is the sign bit. The remaining 39 bits are magnitude
  1359. * bits. Negative numbers are represented using two's-complement
  1360. * notation.
  1361. * If number is not a valid hexadecimal number, HEX2DEC returns the
  1362. * #NUM! error value.
  1363. * @return string
  1364. */
  1365. public static function HEXTODEC($x) {
  1366. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1367. if (is_bool($x)) {
  1368. return PHPExcel_Calculation_Functions::VALUE();
  1369. }
  1370. $x = (string) $x;
  1371. if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/',strtoupper($x),$out)) {
  1372. return PHPExcel_Calculation_Functions::NaN();
  1373. }
  1374. return hexdec($x);
  1375. } // function HEXTODEC()
  1376. /**
  1377. * HEXTOOCT
  1378. *
  1379. * Return a hex value as octal.
  1380. *
  1381. * Excel Function:
  1382. * HEX2OCT(x[,places])
  1383. *
  1384. * @access public
  1385. * @category Engineering Functions
  1386. * @param string $x The hexadecimal number you want to convert. Number cannot
  1387. * contain more than 10 characters. The most significant bit of
  1388. * number is the sign bit. The remaining 39 bits are magnitude
  1389. * bits. Negative numbers are represented using two's-complement
  1390. * notation.
  1391. * If number is negative, HEX2OCT ignores places and returns a
  1392. * 10-character octal number.
  1393. * If number is negative, it cannot be less than FFE0000000, and
  1394. * if number is positive, it cannot be greater than 1FFFFFFF.
  1395. * If number is not a valid hexadecimal number, HEX2OCT returns
  1396. * the #NUM! error value.
  1397. * If HEX2OCT requires more than places characters, it returns
  1398. * the #NUM! error value.
  1399. * @param integer $places The number of characters to use. If places is omitted, HEX2OCT
  1400. * uses the minimum number of characters necessary. Places is
  1401. * useful for padding the return value with leading 0s (zeros).
  1402. * If places is not an integer, it is truncated.
  1403. * If places is nonnumeric, HEX2OCT returns the #VALUE! error
  1404. * value.
  1405. * If places is negative, HEX2OCT returns the #NUM! error value.
  1406. * @return string
  1407. */
  1408. public static function HEXTOOCT($x, $places=null) {
  1409. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1410. $places = PHPExcel_Calculation_Functions::flattenSingleValue($places);
  1411. if (is_bool($x)) {
  1412. return PHPExcel_Calculation_Functions::VALUE();
  1413. }
  1414. $x = (string) $x;
  1415. if (strlen($x) > preg_match_all('/[0123456789ABCDEF]/',strtoupper($x),$out)) {
  1416. return PHPExcel_Calculation_Functions::NaN();
  1417. }
  1418. $octVal = decoct(hexdec($x));
  1419. return self::_nbrConversionFormat($octVal,$places);
  1420. } // function HEXTOOCT()
  1421. /**
  1422. * OCTTOBIN
  1423. *
  1424. * Return an octal value as binary.
  1425. *
  1426. * Excel Function:
  1427. * OCT2BIN(x[,places])
  1428. *
  1429. * @access public
  1430. * @category Engineering Functions
  1431. * @param string $x The octal number you want to convert. Number may not
  1432. * contain more than 10 characters. The most significant
  1433. * bit of number is the sign bit. The remaining 29 bits
  1434. * are magnitude bits. Negative numbers are represented
  1435. * using two's-complement notation.
  1436. * If number is negative, OCT2BIN ignores places and returns
  1437. * a 10-character binary number.
  1438. * If number is negative, it cannot be less than 7777777000,
  1439. * and if number is positive, it cannot be greater than 777.
  1440. * If number is not a valid octal number, OCT2BIN returns
  1441. * the #NUM! error value.
  1442. * If OCT2BIN requires more than places characters, it
  1443. * returns the #NUM! error value.
  1444. * @param integer $places The number of characters to use. If places is omitted,
  1445. * OCT2BIN uses the minimum number of characters necessary.
  1446. * Places is useful for padding the return value with
  1447. * leading 0s (zeros).
  1448. * If places is not an integer, it is truncated.
  1449. * If places is nonnumeric, OCT2BIN returns the #VALUE!
  1450. * error value.
  1451. * If places is negative, OCT2BIN returns the #NUM! error
  1452. * value.
  1453. * @return string
  1454. */
  1455. public static function OCTTOBIN($x, $places=null) {
  1456. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1457. $places = PHPExcel_Calculation_Functions::flattenSingleValue($places);
  1458. if (is_bool($x)) {
  1459. return PHPExcel_Calculation_Functions::VALUE();
  1460. }
  1461. $x = (string) $x;
  1462. if (preg_match_all('/[01234567]/',$x,$out) != strlen($x)) {
  1463. return PHPExcel_Calculation_Functions::NaN();
  1464. }
  1465. $r = decbin(octdec($x));
  1466. return self::_nbrConversionFormat($r,$places);
  1467. } // function OCTTOBIN()
  1468. /**
  1469. * OCTTODEC
  1470. *
  1471. * Return an octal value as decimal.
  1472. *
  1473. * Excel Function:
  1474. * OCT2DEC(x)
  1475. *
  1476. * @access public
  1477. * @category Engineering Functions
  1478. * @param string $x The octal number you want to convert. Number may not contain
  1479. * more than 10 octal characters (30 bits). The most significant
  1480. * bit of number is the sign bit. The remaining 29 bits are
  1481. * magnitude bits. Negative numbers are represented using
  1482. * two's-complement notation.
  1483. * If number is not a valid octal number, OCT2DEC returns the
  1484. * #NUM! error value.
  1485. * @return string
  1486. */
  1487. public static function OCTTODEC($x) {
  1488. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1489. if (is_bool($x)) {
  1490. return PHPExcel_Calculation_Functions::VALUE();
  1491. }
  1492. $x = (string) $x;
  1493. if (preg_match_all('/[01234567]/',$x,$out) != strlen($x)) {
  1494. return PHPExcel_Calculation_Functions::NaN();
  1495. }
  1496. return octdec($x);
  1497. } // function OCTTODEC()
  1498. /**
  1499. * OCTTOHEX
  1500. *
  1501. * Return an octal value as hex.
  1502. *
  1503. * Excel Function:
  1504. * OCT2HEX(x[,places])
  1505. *
  1506. * @access public
  1507. * @category Engineering Functions
  1508. * @param string $x The octal number you want to convert. Number may not contain
  1509. * more than 10 octal characters (30 bits). The most significant
  1510. * bit of number is the sign bit. The remaining 29 bits are
  1511. * magnitude bits. Negative numbers are represented using
  1512. * two's-complement notation.
  1513. * If number is negative, OCT2HEX ignores places and returns a
  1514. * 10-character hexadecimal number.
  1515. * If number is not a valid octal number, OCT2HEX returns the
  1516. * #NUM! error value.
  1517. * If OCT2HEX requires more than places characters, it returns
  1518. * the #NUM! error value.
  1519. * @param integer $places The number of characters to use. If places is omitted, OCT2HEX
  1520. * uses the minimum number of characters necessary. Places is useful
  1521. * for padding the return value with leading 0s (zeros).
  1522. * If places is not an integer, it is truncated.
  1523. * If places is nonnumeric, OCT2HEX returns the #VALUE! error value.
  1524. * If places is negative, OCT2HEX returns the #NUM! error value.
  1525. * @return string
  1526. */
  1527. public static function OCTTOHEX($x, $places=null) {
  1528. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  1529. $places = PHPExcel_Calculation_Functions::flattenSingleValue($places);
  1530. if (is_bool($x)) {
  1531. return PHPExcel_Calculation_Functions::VALUE();
  1532. }
  1533. $x = (string) $x;
  1534. if (preg_match_all('/[01234567]/',$x,$out) != strlen($x)) {
  1535. return PHPExcel_Calculation_Functions::NaN();
  1536. }
  1537. $hexVal = strtoupper(dechex(octdec($x)));
  1538. return self::_nbrConversionFormat($hexVal,$places);
  1539. } // function OCTTOHEX()
  1540. /**
  1541. * COMPLEX
  1542. *
  1543. * Converts real and imaginary coefficients into a complex number of the form x + yi or x + yj.
  1544. *
  1545. * Excel Function:
  1546. * COMPLEX(realNumber,imaginary[,places])
  1547. *
  1548. * @access public
  1549. * @category Engineering Functions
  1550. * @param float $realNumber The real coefficient of the complex number.
  1551. * @param float $imaginary The imaginary coefficient of the complex number.
  1552. * @param string $suffix The suffix for the imaginary component of the complex number.
  1553. * If omitted, the suffix is assumed to be "i".
  1554. * @return string
  1555. */
  1556. public static function COMPLEX($realNumber=0.0, $imaginary=0.0, $suffix='i') {
  1557. $realNumber = (is_null($realNumber)) ? 0.0 : PHPExcel_Calculation_Functions::flattenSingleValue($realNumber);
  1558. $imaginary = (is_null($imaginary)) ? 0.0 : PHPExcel_Calculation_Functions::flattenSingleValue($imaginary);
  1559. $suffix = (is_null($suffix)) ? 'i' : PHPExcel_Calculation_Functions::flattenSingleValue($suffix);
  1560. if (((is_numeric($realNumber)) && (is_numeric($imaginary))) &&
  1561. (($suffix == 'i') || ($suffix == 'j') || ($suffix == ''))) {
  1562. $realNumber = (float) $realNumber;
  1563. $imaginary = (float) $imaginary;
  1564. if ($suffix == '') $suffix = 'i';
  1565. if ($realNumber == 0.0) {
  1566. if ($imaginary == 0.0) {
  1567. return (string) '0';
  1568. } elseif ($imaginary == 1.0) {
  1569. return (string) $suffix;
  1570. } elseif ($imaginary == -1.0) {
  1571. return (string) '-'.$suffix;
  1572. }
  1573. return (string) $imaginary.$suffix;
  1574. } elseif ($imaginary == 0.0) {
  1575. return (string) $realNumber;
  1576. } elseif ($imaginary == 1.0) {
  1577. return (string) $realNumber.'+'.$suffix;
  1578. } elseif ($imaginary == -1.0) {
  1579. return (string) $realNumber.'-'.$suffix;
  1580. }
  1581. if ($imaginary > 0) { $imaginary = (string) '+'.$imaginary; }
  1582. return (string) $realNumber.$imaginary.$suffix;
  1583. }
  1584. return PHPExcel_Calculation_Functions::VALUE();
  1585. } // function COMPLEX()
  1586. /**
  1587. * IMAGINARY
  1588. *
  1589. * Returns the imaginary coefficient of a complex number in x + yi or x + yj text format.
  1590. *
  1591. * Excel Function:
  1592. * IMAGINARY(complexNumber)
  1593. *
  1594. * @access public
  1595. * @category Engineering Functions
  1596. * @param string $complexNumber The complex number for which you want the imaginary
  1597. * coefficient.
  1598. * @return float
  1599. */
  1600. public static function IMAGINARY($complexNumber) {
  1601. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1602. $parsedComplex = self::_parseComplex($complexNumber);
  1603. return $parsedComplex['imaginary'];
  1604. } // function IMAGINARY()
  1605. /**
  1606. * IMREAL
  1607. *
  1608. * Returns the real coefficient of a complex number in x + yi or x + yj text format.
  1609. *
  1610. * Excel Function:
  1611. * IMREAL(complexNumber)
  1612. *
  1613. * @access public
  1614. * @category Engineering Functions
  1615. * @param string $complexNumber The complex number for which you want the real coefficient.
  1616. * @return float
  1617. */
  1618. public static function IMREAL($complexNumber) {
  1619. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1620. $parsedComplex = self::_parseComplex($complexNumber);
  1621. return $parsedComplex['real'];
  1622. } // function IMREAL()
  1623. /**
  1624. * IMABS
  1625. *
  1626. * Returns the absolute value (modulus) of a complex number in x + yi or x + yj text format.
  1627. *
  1628. * Excel Function:
  1629. * IMABS(complexNumber)
  1630. *
  1631. * @param string $complexNumber The complex number for which you want the absolute value.
  1632. * @return float
  1633. */
  1634. public static function IMABS($complexNumber) {
  1635. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1636. $parsedComplex = self::_parseComplex($complexNumber);
  1637. return sqrt(($parsedComplex['real'] * $parsedComplex['real']) + ($parsedComplex['imaginary'] * $parsedComplex['imaginary']));
  1638. } // function IMABS()
  1639. /**
  1640. * IMARGUMENT
  1641. *
  1642. * Returns the argument theta of a complex number, i.e. the angle in radians from the real
  1643. * axis to the representation of the number in polar coordinates.
  1644. *
  1645. * Excel Function:
  1646. * IMARGUMENT(complexNumber)
  1647. *
  1648. * @param string $complexNumber The complex number for which you want the argument theta.
  1649. * @return float
  1650. */
  1651. public static function IMARGUMENT($complexNumber) {
  1652. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1653. $parsedComplex = self::_parseComplex($complexNumber);
  1654. if ($parsedComplex['real'] == 0.0) {
  1655. if ($parsedComplex['imaginary'] == 0.0) {
  1656. return 0.0;
  1657. } elseif($parsedComplex['imaginary'] < 0.0) {
  1658. return M_PI / -2;
  1659. } else {
  1660. return M_PI / 2;
  1661. }
  1662. } elseif ($parsedComplex['real'] > 0.0) {
  1663. return atan($parsedComplex['imaginary'] / $parsedComplex['real']);
  1664. } elseif ($parsedComplex['imaginary'] < 0.0) {
  1665. return 0 - (M_PI - atan(abs($parsedComplex['imaginary']) / abs($parsedComplex['real'])));
  1666. } else {
  1667. return M_PI - atan($parsedComplex['imaginary'] / abs($parsedComplex['real']));
  1668. }
  1669. } // function IMARGUMENT()
  1670. /**
  1671. * IMCONJUGATE
  1672. *
  1673. * Returns the complex conjugate of a complex number in x + yi or x + yj text format.
  1674. *
  1675. * Excel Function:
  1676. * IMCONJUGATE(complexNumber)
  1677. *
  1678. * @param string $complexNumber The complex number for which you want the conjugate.
  1679. * @return string
  1680. */
  1681. public static function IMCONJUGATE($complexNumber) {
  1682. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1683. $parsedComplex = self::_parseComplex($complexNumber);
  1684. if ($parsedComplex['imaginary'] == 0.0) {
  1685. return $parsedComplex['real'];
  1686. } else {
  1687. return self::_cleanComplex( self::COMPLEX( $parsedComplex['real'],
  1688. 0 - $parsedComplex['imaginary'],
  1689. $parsedComplex['suffix']
  1690. )
  1691. );
  1692. }
  1693. } // function IMCONJUGATE()
  1694. /**
  1695. * IMCOS
  1696. *
  1697. * Returns the cosine of a complex number in x + yi or x + yj text format.
  1698. *
  1699. * Excel Function:
  1700. * IMCOS(complexNumber)
  1701. *
  1702. * @param string $complexNumber The complex number for which you want the cosine.
  1703. * @return string|float
  1704. */
  1705. public static function IMCOS($complexNumber) {
  1706. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1707. $parsedComplex = self::_parseComplex($complexNumber);
  1708. if ($parsedComplex['imaginary'] == 0.0) {
  1709. return cos($parsedComplex['real']);
  1710. } else {
  1711. return self::IMCONJUGATE(self::COMPLEX(cos($parsedComplex['real']) * cosh($parsedComplex['imaginary']),sin($parsedComplex['real']) * sinh($parsedComplex['imaginary']),$parsedComplex['suffix']));
  1712. }
  1713. } // function IMCOS()
  1714. /**
  1715. * IMSIN
  1716. *
  1717. * Returns the sine of a complex number in x + yi or x + yj text format.
  1718. *
  1719. * Excel Function:
  1720. * IMSIN(complexNumber)
  1721. *
  1722. * @param string $complexNumber The complex number for which you want the sine.
  1723. * @return string|float
  1724. */
  1725. public static function IMSIN($complexNumber) {
  1726. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1727. $parsedComplex = self::_parseComplex($complexNumber);
  1728. if ($parsedComplex['imaginary'] == 0.0) {
  1729. return sin($parsedComplex['real']);
  1730. } else {
  1731. return self::COMPLEX(sin($parsedComplex['real']) * cosh($parsedComplex['imaginary']),cos($parsedComplex['real']) * sinh($parsedComplex['imaginary']),$parsedComplex['suffix']);
  1732. }
  1733. } // function IMSIN()
  1734. /**
  1735. * IMSQRT
  1736. *
  1737. * Returns the square root of a complex number in x + yi or x + yj text format.
  1738. *
  1739. * Excel Function:
  1740. * IMSQRT(complexNumber)
  1741. *
  1742. * @param string $complexNumber The complex number for which you want the square root.
  1743. * @return string
  1744. */
  1745. public static function IMSQRT($complexNumber) {
  1746. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1747. $parsedComplex = self::_parseComplex($complexNumber);
  1748. $theta = self::IMARGUMENT($complexNumber);
  1749. $d1 = cos($theta / 2);
  1750. $d2 = sin($theta / 2);
  1751. $r = sqrt(sqrt(($parsedComplex['real'] * $parsedComplex['real']) + ($parsedComplex['imaginary'] * $parsedComplex['imaginary'])));
  1752. if ($parsedComplex['suffix'] == '') {
  1753. return self::COMPLEX($d1 * $r,$d2 * $r);
  1754. } else {
  1755. return self::COMPLEX($d1 * $r,$d2 * $r,$parsedComplex['suffix']);
  1756. }
  1757. } // function IMSQRT()
  1758. /**
  1759. * IMLN
  1760. *
  1761. * Returns the natural logarithm of a complex number in x + yi or x + yj text format.
  1762. *
  1763. * Excel Function:
  1764. * IMLN(complexNumber)
  1765. *
  1766. * @param string $complexNumber The complex number for which you want the natural logarithm.
  1767. * @return string
  1768. */
  1769. public static function IMLN($complexNumber) {
  1770. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1771. $parsedComplex = self::_parseComplex($complexNumber);
  1772. if (($parsedComplex['real'] == 0.0) && ($parsedComplex['imaginary'] == 0.0)) {
  1773. return PHPExcel_Calculation_Functions::NaN();
  1774. }
  1775. $logR = log(sqrt(($parsedComplex['real'] * $parsedComplex['real']) + ($parsedComplex['imaginary'] * $parsedComplex['imaginary'])));
  1776. $t = self::IMARGUMENT($complexNumber);
  1777. if ($parsedComplex['suffix'] == '') {
  1778. return self::COMPLEX($logR,$t);
  1779. } else {
  1780. return self::COMPLEX($logR,$t,$parsedComplex['suffix']);
  1781. }
  1782. } // function IMLN()
  1783. /**
  1784. * IMLOG10
  1785. *
  1786. * Returns the common logarithm (base 10) of a complex number in x + yi or x + yj text format.
  1787. *
  1788. * Excel Function:
  1789. * IMLOG10(complexNumber)
  1790. *
  1791. * @param string $complexNumber The complex number for which you want the common logarithm.
  1792. * @return string
  1793. */
  1794. public static function IMLOG10($complexNumber) {
  1795. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1796. $parsedComplex = self::_parseComplex($complexNumber);
  1797. if (($parsedComplex['real'] == 0.0) && ($parsedComplex['imaginary'] == 0.0)) {
  1798. return PHPExcel_Calculation_Functions::NaN();
  1799. } elseif (($parsedComplex['real'] > 0.0) && ($parsedComplex['imaginary'] == 0.0)) {
  1800. return log10($parsedComplex['real']);
  1801. }
  1802. return self::IMPRODUCT(log10(EULER),self::IMLN($complexNumber));
  1803. } // function IMLOG10()
  1804. /**
  1805. * IMLOG2
  1806. *
  1807. * Returns the common logarithm (base 10) of a complex number in x + yi or x + yj text format.
  1808. *
  1809. * Excel Function:
  1810. * IMLOG2(complexNumber)
  1811. *
  1812. * @param string $complexNumber The complex number for which you want the base-2 logarithm.
  1813. * @return string
  1814. */
  1815. public static function IMLOG2($complexNumber) {
  1816. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1817. $parsedComplex = self::_parseComplex($complexNumber);
  1818. if (($parsedComplex['real'] == 0.0) && ($parsedComplex['imaginary'] == 0.0)) {
  1819. return PHPExcel_Calculation_Functions::NaN();
  1820. } elseif (($parsedComplex['real'] > 0.0) && ($parsedComplex['imaginary'] == 0.0)) {
  1821. return log($parsedComplex['real'],2);
  1822. }
  1823. return self::IMPRODUCT(log(EULER,2),self::IMLN($complexNumber));
  1824. } // function IMLOG2()
  1825. /**
  1826. * IMEXP
  1827. *
  1828. * Returns the exponential of a complex number in x + yi or x + yj text format.
  1829. *
  1830. * Excel Function:
  1831. * IMEXP(complexNumber)
  1832. *
  1833. * @param string $complexNumber The complex number for which you want the exponential.
  1834. * @return string
  1835. */
  1836. public static function IMEXP($complexNumber) {
  1837. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1838. $parsedComplex = self::_parseComplex($complexNumber);
  1839. if (($parsedComplex['real'] == 0.0) && ($parsedComplex['imaginary'] == 0.0)) {
  1840. return '1';
  1841. }
  1842. $e = exp($parsedComplex['real']);
  1843. $eX = $e * cos($parsedComplex['imaginary']);
  1844. $eY = $e * sin($parsedComplex['imaginary']);
  1845. if ($parsedComplex['suffix'] == '') {
  1846. return self::COMPLEX($eX,$eY);
  1847. } else {
  1848. return self::COMPLEX($eX,$eY,$parsedComplex['suffix']);
  1849. }
  1850. } // function IMEXP()
  1851. /**
  1852. * IMPOWER
  1853. *
  1854. * Returns a complex number in x + yi or x + yj text format raised to a power.
  1855. *
  1856. * Excel Function:
  1857. * IMPOWER(complexNumber,realNumber)
  1858. *
  1859. * @param string $complexNumber The complex number you want to raise to a power.
  1860. * @param float $realNumber The power to which you want to raise the complex number.
  1861. * @return string
  1862. */
  1863. public static function IMPOWER($complexNumber,$realNumber) {
  1864. $complexNumber = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber);
  1865. $realNumber = PHPExcel_Calculation_Functions::flattenSingleValue($realNumber);
  1866. if (!is_numeric($realNumber)) {
  1867. return PHPExcel_Calculation_Functions::VALUE();
  1868. }
  1869. $parsedComplex = self::_parseComplex($complexNumber);
  1870. $r = sqrt(($parsedComplex['real'] * $parsedComplex['real']) + ($parsedComplex['imaginary'] * $parsedComplex['imaginary']));
  1871. $rPower = pow($r,$realNumber);
  1872. $theta = self::IMARGUMENT($complexNumber) * $realNumber;
  1873. if ($theta == 0) {
  1874. return 1;
  1875. } elseif ($parsedComplex['imaginary'] == 0.0) {
  1876. return self::COMPLEX($rPower * cos($theta),$rPower * sin($theta),$parsedComplex['suffix']);
  1877. } else {
  1878. return self::COMPLEX($rPower * cos($theta),$rPower * sin($theta),$parsedComplex['suffix']);
  1879. }
  1880. } // function IMPOWER()
  1881. /**
  1882. * IMDIV
  1883. *
  1884. * Returns the quotient of two complex numbers in x + yi or x + yj text format.
  1885. *
  1886. * Excel Function:
  1887. * IMDIV(complexDividend,complexDivisor)
  1888. *
  1889. * @param string $complexDividend The complex numerator or dividend.
  1890. * @param string $complexDivisor The complex denominator or divisor.
  1891. * @return string
  1892. */
  1893. public static function IMDIV($complexDividend,$complexDivisor) {
  1894. $complexDividend = PHPExcel_Calculation_Functions::flattenSingleValue($complexDividend);
  1895. $complexDivisor = PHPExcel_Calculation_Functions::flattenSingleValue($complexDivisor);
  1896. $parsedComplexDividend = self::_parseComplex($complexDividend);
  1897. $parsedComplexDivisor = self::_parseComplex($complexDivisor);
  1898. if (($parsedComplexDividend['suffix'] != '') && ($parsedComplexDivisor['suffix'] != '') &&
  1899. ($parsedComplexDividend['suffix'] != $parsedComplexDivisor['suffix'])) {
  1900. return PHPExcel_Calculation_Functions::NaN();
  1901. }
  1902. if (($parsedComplexDividend['suffix'] != '') && ($parsedComplexDivisor['suffix'] == '')) {
  1903. $parsedComplexDivisor['suffix'] = $parsedComplexDividend['suffix'];
  1904. }
  1905. $d1 = ($parsedComplexDividend['real'] * $parsedComplexDivisor['real']) + ($parsedComplexDividend['imaginary'] * $parsedComplexDivisor['imaginary']);
  1906. $d2 = ($parsedComplexDividend['imaginary'] * $parsedComplexDivisor['real']) - ($parsedComplexDividend['real'] * $parsedComplexDivisor['imaginary']);
  1907. $d3 = ($parsedComplexDivisor['real'] * $parsedComplexDivisor['real']) + ($parsedComplexDivisor['imaginary'] * $parsedComplexDivisor['imaginary']);
  1908. $r = $d1/$d3;
  1909. $i = $d2/$d3;
  1910. if ($i > 0.0) {
  1911. return self::_cleanComplex($r.'+'.$i.$parsedComplexDivisor['suffix']);
  1912. } elseif ($i < 0.0) {
  1913. return self::_cleanComplex($r.$i.$parsedComplexDivisor['suffix']);
  1914. } else {
  1915. return $r;
  1916. }
  1917. } // function IMDIV()
  1918. /**
  1919. * IMSUB
  1920. *
  1921. * Returns the difference of two complex numbers in x + yi or x + yj text format.
  1922. *
  1923. * Excel Function:
  1924. * IMSUB(complexNumber1,complexNumber2)
  1925. *
  1926. * @param string $complexNumber1 The complex number from which to subtract complexNumber2.
  1927. * @param string $complexNumber2 The complex number to subtract from complexNumber1.
  1928. * @return string
  1929. */
  1930. public static function IMSUB($complexNumber1,$complexNumber2) {
  1931. $complexNumber1 = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber1);
  1932. $complexNumber2 = PHPExcel_Calculation_Functions::flattenSingleValue($complexNumber2);
  1933. $parsedComplex1 = self::_parseComplex($complexNumber1);
  1934. $parsedComplex2 = self::_parseComplex($complexNumber2);
  1935. if ((($parsedComplex1['suffix'] != '') && ($parsedComplex2['suffix'] != '')) &&
  1936. ($parsedComplex1['suffix'] != $parsedComplex2['suffix'])) {
  1937. return PHPExcel_Calculation_Functions::NaN();
  1938. } elseif (($parsedComplex1['suffix'] == '') && ($parsedComplex2['suffix'] != '')) {
  1939. $parsedComplex1['suffix'] = $parsedComplex2['suffix'];
  1940. }
  1941. $d1 = $parsedComplex1['real'] - $parsedComplex2['real'];
  1942. $d2 = $parsedComplex1['imaginary'] - $parsedComplex2['imaginary'];
  1943. return self::COMPLEX($d1,$d2,$parsedComplex1['suffix']);
  1944. } // function IMSUB()
  1945. /**
  1946. * IMSUM
  1947. *
  1948. * Returns the sum of two or more complex numbers in x + yi or x + yj text format.
  1949. *
  1950. * Excel Function:
  1951. * IMSUM(complexNumber[,complexNumber[,...]])
  1952. *
  1953. * @param string $complexNumber,... Series of complex numbers to add
  1954. * @return string
  1955. */
  1956. public static function IMSUM() {
  1957. // Return value
  1958. $returnValue = self::_parseComplex('0');
  1959. $activeSuffix = '';
  1960. // Loop through the arguments
  1961. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1962. foreach ($aArgs as $arg) {
  1963. $parsedComplex = self::_parseComplex($arg);
  1964. if ($activeSuffix == '') {
  1965. $activeSuffix = $parsedComplex['suffix'];
  1966. } elseif (($parsedComplex['suffix'] != '') && ($activeSuffix != $parsedComplex['suffix'])) {
  1967. return PHPExcel_Calculation_Functions::VALUE();
  1968. }
  1969. $returnValue['real'] += $parsedComplex['real'];
  1970. $returnValue['imaginary'] += $parsedComplex['imaginary'];
  1971. }
  1972. if ($returnValue['imaginary'] == 0.0) { $activeSuffix = ''; }
  1973. return self::COMPLEX($returnValue['real'],$returnValue['imaginary'],$activeSuffix);
  1974. } // function IMSUM()
  1975. /**
  1976. * IMPRODUCT
  1977. *
  1978. * Returns the product of two or more complex numbers in x + yi or x + yj text format.
  1979. *
  1980. * Excel Function:
  1981. * IMPRODUCT(complexNumber[,complexNumber[,...]])
  1982. *
  1983. * @param string $complexNumber,... Series of complex numbers to multiply
  1984. * @return string
  1985. */
  1986. public static function IMPRODUCT() {
  1987. // Return value
  1988. $returnValue = self::_parseComplex('1');
  1989. $activeSuffix = '';
  1990. // Loop through the arguments
  1991. $aArgs = PHPExcel_Calculation_Functions::flattenArray(func_get_args());
  1992. foreach ($aArgs as $arg) {
  1993. $parsedComplex = self::_parseComplex($arg);
  1994. $workValue = $returnValue;
  1995. if (($parsedComplex['suffix'] != '') && ($activeSuffix == '')) {
  1996. $activeSuffix = $parsedComplex['suffix'];
  1997. } elseif (($parsedComplex['suffix'] != '') && ($activeSuffix != $parsedComplex['suffix'])) {
  1998. return PHPExcel_Calculation_Functions::NaN();
  1999. }
  2000. $returnValue['real'] = ($workValue['real'] * $parsedComplex['real']) - ($workValue['imaginary'] * $parsedComplex['imaginary']);
  2001. $returnValue['imaginary'] = ($workValue['real'] * $parsedComplex['imaginary']) + ($workValue['imaginary'] * $parsedComplex['real']);
  2002. }
  2003. if ($returnValue['imaginary'] == 0.0) { $activeSuffix = ''; }
  2004. return self::COMPLEX($returnValue['real'],$returnValue['imaginary'],$activeSuffix);
  2005. } // function IMPRODUCT()
  2006. /**
  2007. * DELTA
  2008. *
  2009. * Tests whether two values are equal. Returns 1 if number1 = number2; returns 0 otherwise.
  2010. * Use this function to filter a set of values. For example, by summing several DELTA
  2011. * functions you calculate the count of equal pairs. This function is also known as the
  2012. * Kronecker Delta function.
  2013. *
  2014. * Excel Function:
  2015. * DELTA(a[,b])
  2016. *
  2017. * @param float $a The first number.
  2018. * @param float $b The second number. If omitted, b is assumed to be zero.
  2019. * @return int
  2020. */
  2021. public static function DELTA($a, $b=0) {
  2022. $a = PHPExcel_Calculation_Functions::flattenSingleValue($a);
  2023. $b = PHPExcel_Calculation_Functions::flattenSingleValue($b);
  2024. return (int) ($a == $b);
  2025. } // function DELTA()
  2026. /**
  2027. * GESTEP
  2028. *
  2029. * Excel Function:
  2030. * GESTEP(number[,step])
  2031. *
  2032. * Returns 1 if number >= step; returns 0 (zero) otherwise
  2033. * Use this function to filter a set of values. For example, by summing several GESTEP
  2034. * functions you calculate the count of values that exceed a threshold.
  2035. *
  2036. * @param float $number The value to test against step.
  2037. * @param float $step The threshold value.
  2038. * If you omit a value for step, GESTEP uses zero.
  2039. * @return int
  2040. */
  2041. public static function GESTEP($number, $step=0) {
  2042. $number = PHPExcel_Calculation_Functions::flattenSingleValue($number);
  2043. $step = PHPExcel_Calculation_Functions::flattenSingleValue($step);
  2044. return (int) ($number >= $step);
  2045. } // function GESTEP()
  2046. //
  2047. // Private method to calculate the erf value
  2048. //
  2049. private static $_two_sqrtpi = 1.128379167095512574;
  2050. public static function _erfVal($x) {
  2051. if (abs($x) > 2.2) {
  2052. return 1 - self::_erfcVal($x);
  2053. }
  2054. $sum = $term = $x;
  2055. $xsqr = ($x * $x);
  2056. $j = 1;
  2057. do {
  2058. $term *= $xsqr / $j;
  2059. $sum -= $term / (2 * $j + 1);
  2060. ++$j;
  2061. $term *= $xsqr / $j;
  2062. $sum += $term / (2 * $j + 1);
  2063. ++$j;
  2064. if ($sum == 0.0) {
  2065. break;
  2066. }
  2067. } while (abs($term / $sum) > PRECISION);
  2068. return self::$_two_sqrtpi * $sum;
  2069. } // function _erfVal()
  2070. /**
  2071. * ERF
  2072. *
  2073. * Returns the error function integrated between lower_limit and upper_limit
  2074. *
  2075. * Excel Function:
  2076. * ERF(lower[,upper])
  2077. *
  2078. * @param float $lower lower bound for integrating ERF
  2079. * @param float $upper upper bound for integrating ERF.
  2080. * If omitted, ERF integrates between zero and lower_limit
  2081. * @return int
  2082. */
  2083. public static function ERF($lower, $upper = null) {
  2084. $lower = PHPExcel_Calculation_Functions::flattenSingleValue($lower);
  2085. $upper = PHPExcel_Calculation_Functions::flattenSingleValue($upper);
  2086. if (is_numeric($lower)) {
  2087. if ($lower < 0) {
  2088. return PHPExcel_Calculation_Functions::NaN();
  2089. }
  2090. if (is_null($upper)) {
  2091. return self::_erfVal($lower);
  2092. }
  2093. if (is_numeric($upper)) {
  2094. if ($upper < 0) {
  2095. return PHPExcel_Calculation_Functions::NaN();
  2096. }
  2097. return self::_erfVal($upper) - self::_erfVal($lower);
  2098. }
  2099. }
  2100. return PHPExcel_Calculation_Functions::VALUE();
  2101. } // function ERF()
  2102. //
  2103. // Private method to calculate the erfc value
  2104. //
  2105. private static $_one_sqrtpi = 0.564189583547756287;
  2106. private static function _erfcVal($x) {
  2107. if (abs($x) < 2.2) {
  2108. return 1 - self::_erfVal($x);
  2109. }
  2110. if ($x < 0) {
  2111. return 2 - self::ERFC(-$x);
  2112. }
  2113. $a = $n = 1;
  2114. $b = $c = $x;
  2115. $d = ($x * $x) + 0.5;
  2116. $q1 = $q2 = $b / $d;
  2117. $t = 0;
  2118. do {
  2119. $t = $a * $n + $b * $x;
  2120. $a = $b;
  2121. $b = $t;
  2122. $t = $c * $n + $d * $x;
  2123. $c = $d;
  2124. $d = $t;
  2125. $n += 0.5;
  2126. $q1 = $q2;
  2127. $q2 = $b / $d;
  2128. } while ((abs($q1 - $q2) / $q2) > PRECISION);
  2129. return self::$_one_sqrtpi * exp(-$x * $x) * $q2;
  2130. } // function _erfcVal()
  2131. /**
  2132. * ERFC
  2133. *
  2134. * Returns the complementary ERF function integrated between x and infinity
  2135. *
  2136. * Excel Function:
  2137. * ERF(x)
  2138. *
  2139. * @param float $x The lower bound for integrating ERF
  2140. * @return int
  2141. */
  2142. public static function ERFC($x) {
  2143. $x = PHPExcel_Calculation_Functions::flattenSingleValue($x);
  2144. if (is_numeric($x)) {
  2145. if ($x < 0) {
  2146. return PHPExcel_Calculation_Functions::NaN();
  2147. }
  2148. return self::_erfcVal($x);
  2149. }
  2150. return PHPExcel_Calculation_Functions::VALUE();
  2151. } // function ERFC()
  2152. /**
  2153. * getConversionGroups
  2154. *
  2155. * @return array
  2156. */
  2157. public static function getConversionGroups() {
  2158. $conversionGroups = array();
  2159. foreach(self::$_conversionUnits as $conversionUnit) {
  2160. $conversionGroups[] = $conversionUnit['Group'];
  2161. }
  2162. return array_merge(array_unique($conversionGroups));
  2163. } // function getConversionGroups()
  2164. /**
  2165. * getConversionGroupUnits
  2166. *
  2167. * @return array
  2168. */
  2169. public static function getConversionGroupUnits($group = NULL) {
  2170. $conversionGroups = array();
  2171. foreach(self::$_conversionUnits as $conversionUnit => $conversionGroup) {
  2172. if ((is_null($group)) || ($conversionGroup['Group'] == $group)) {
  2173. $conversionGroups[$conversionGroup['Group']][] = $conversionUnit;
  2174. }
  2175. }
  2176. return $conversionGroups;
  2177. } // function getConversionGroupUnits()
  2178. /**
  2179. * getConversionGroupUnitDetails
  2180. *
  2181. * @return array
  2182. */
  2183. public static function getConversionGroupUnitDetails($group = NULL) {
  2184. $conversionGroups = array();
  2185. foreach(self::$_conversionUnits as $conversionUnit => $conversionGroup) {
  2186. if ((is_null($group)) || ($conversionGroup['Group'] == $group)) {
  2187. $conversionGroups[$conversionGroup['Group']][] = array( 'unit' => $conversionUnit,
  2188. 'description' => $conversionGroup['Unit Name']
  2189. );
  2190. }
  2191. }
  2192. return $conversionGroups;
  2193. } // function getConversionGroupUnitDetails()
  2194. /**
  2195. * getConversionGroups
  2196. *
  2197. * @return array
  2198. */
  2199. public static function getConversionMultipliers() {
  2200. return self::$_conversionMultipliers;
  2201. } // function getConversionGroups()
  2202. /**
  2203. * CONVERTUOM
  2204. *
  2205. * Converts a number from one measurement system to another.
  2206. * For example, CONVERT can translate a table of distances in miles to a table of distances
  2207. * in kilometers.
  2208. *
  2209. * Excel Function:
  2210. * CONVERT(value,fromUOM,toUOM)
  2211. *
  2212. * @param float $value The value in fromUOM to convert.
  2213. * @param string $fromUOM The units for value.
  2214. * @param string $toUOM The units for the result.
  2215. * @return float
  2216. */
  2217. public static function CONVERTUOM($value, $fromUOM, $toUOM) {
  2218. $value = PHPExcel_Calculation_Functions::flattenSingleValue($value);
  2219. $fromUOM = PHPExcel_Calculation_Functions::flattenSingleValue($fromUOM);
  2220. $toUOM = PHPExcel_Calculation_Functions::flattenSingleValue($toUOM);
  2221. if (!is_numeric($value)) {
  2222. return PHPExcel_Calculation_Functions::VALUE();
  2223. }
  2224. $fromMultiplier = 1.0;
  2225. if (isset(self::$_conversionUnits[$fromUOM])) {
  2226. $unitGroup1 = self::$_conversionUnits[$fromUOM]['Group'];
  2227. } else {
  2228. $fromMultiplier = substr($fromUOM,0,1);
  2229. $fromUOM = substr($fromUOM,1);
  2230. if (isset(self::$_conversionMultipliers[$fromMultiplier])) {
  2231. $fromMultiplier = self::$_conversionMultipliers[$fromMultiplier]['multiplier'];
  2232. } else {
  2233. return PHPExcel_Calculation_Functions::NA();
  2234. }
  2235. if ((isset(self::$_conversionUnits[$fromUOM])) && (self::$_conversionUnits[$fromUOM]['AllowPrefix'])) {
  2236. $unitGroup1 = self::$_conversionUnits[$fromUOM]['Group'];
  2237. } else {
  2238. return PHPExcel_Calculation_Functions::NA();
  2239. }
  2240. }
  2241. $value *= $fromMultiplier;
  2242. $toMultiplier = 1.0;
  2243. if (isset(self::$_conversionUnits[$toUOM])) {
  2244. $unitGroup2 = self::$_conversionUnits[$toUOM]['Group'];
  2245. } else {
  2246. $toMultiplier = substr($toUOM,0,1);
  2247. $toUOM = substr($toUOM,1);
  2248. if (isset(self::$_conversionMultipliers[$toMultiplier])) {
  2249. $toMultiplier = self::$_conversionMultipliers[$toMultiplier]['multiplier'];
  2250. } else {
  2251. return PHPExcel_Calculation_Functions::NA();
  2252. }
  2253. if ((isset(self::$_conversionUnits[$toUOM])) && (self::$_conversionUnits[$toUOM]['AllowPrefix'])) {
  2254. $unitGroup2 = self::$_conversionUnits[$toUOM]['Group'];
  2255. } else {
  2256. return PHPExcel_Calculation_Functions::NA();
  2257. }
  2258. }
  2259. if ($unitGroup1 != $unitGroup2) {
  2260. return PHPExcel_Calculation_Functions::NA();
  2261. }
  2262. if (($fromUOM == $toUOM) && ($fromMultiplier == $toMultiplier)) {
  2263. // We've already factored $fromMultiplier into the value, so we need
  2264. // to reverse it again
  2265. return $value / $fromMultiplier;
  2266. } elseif ($unitGroup1 == 'Temperature') {
  2267. if (($fromUOM == 'F') || ($fromUOM == 'fah')) {
  2268. if (($toUOM == 'F') || ($toUOM == 'fah')) {
  2269. return $value;
  2270. } else {
  2271. $value = (($value - 32) / 1.8);
  2272. if (($toUOM == 'K') || ($toUOM == 'kel')) {
  2273. $value += 273.15;
  2274. }
  2275. return $value;
  2276. }
  2277. } elseif ((($fromUOM == 'K') || ($fromUOM == 'kel')) &&
  2278. (($toUOM == 'K') || ($toUOM == 'kel'))) {
  2279. return $value;
  2280. } elseif ((($fromUOM == 'C') || ($fromUOM == 'cel')) &&
  2281. (($toUOM == 'C') || ($toUOM == 'cel'))) {
  2282. return $value;
  2283. }
  2284. if (($toUOM == 'F') || ($toUOM == 'fah')) {
  2285. if (($fromUOM == 'K') || ($fromUOM == 'kel')) {
  2286. $value -= 273.15;
  2287. }
  2288. return ($value * 1.8) + 32;
  2289. }
  2290. if (($toUOM == 'C') || ($toUOM == 'cel')) {
  2291. return $value - 273.15;
  2292. }
  2293. return $value + 273.15;
  2294. }
  2295. return ($value * self::$_unitConversions[$unitGroup1][$fromUOM][$toUOM]) / $toMultiplier;
  2296. } // function CONVERTUOM()
  2297. } // class PHPExcel_Calculation_Engineering