PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/standard/math.c

http://github.com/php/php-src
C | 1249 lines | 820 code | 198 blank | 231 comment | 212 complexity | 00ea5dbb8370dc6fe2b94863cb3ede4f MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Jim Winstead <jimw@php.net> |
  14. | Stig Sæther Bakken <ssb@php.net> |
  15. | Zeev Suraski <zeev@php.net> |
  16. | PHP 4.0 patches by Thies C. Arntzen <thies@thieso.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "php.h"
  20. #include "php_math.h"
  21. #include "zend_multiply.h"
  22. #include "zend_exceptions.h"
  23. #include "zend_portability.h"
  24. #include <math.h>
  25. #include <float.h>
  26. #include <stdlib.h>
  27. #include "basic_functions.h"
  28. /* {{{ php_intlog10abs
  29. Returns floor(log10(fabs(val))), uses fast binary search */
  30. static inline int php_intlog10abs(double value) {
  31. int result;
  32. value = fabs(value);
  33. if (value < 1e-8 || value > 1e22) {
  34. result = (int)floor(log10(value));
  35. } else {
  36. static const double values[] = {
  37. 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1,
  38. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,
  39. 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15,
  40. 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
  41. /* Do a binary search with 5 steps */
  42. result = 15;
  43. if (value < values[result]) {
  44. result -= 8;
  45. } else {
  46. result += 8;
  47. }
  48. if (value < values[result]) {
  49. result -= 4;
  50. } else {
  51. result += 4;
  52. }
  53. if (value < values[result]) {
  54. result -= 2;
  55. } else {
  56. result += 2;
  57. }
  58. if (value < values[result]) {
  59. result -= 1;
  60. } else {
  61. result += 1;
  62. }
  63. if (value < values[result]) {
  64. result -= 1;
  65. }
  66. result -= 8;
  67. }
  68. return result;
  69. }
  70. /* }}} */
  71. /* {{{ php_intpow10
  72. Returns pow(10.0, (double)power), uses fast lookup table for exact powers */
  73. static inline double php_intpow10(int power) {
  74. static const double powers[] = {
  75. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,
  76. 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15,
  77. 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
  78. /* Not in lookup table */
  79. if (power < 0 || power > 22) {
  80. return pow(10.0, (double)power);
  81. }
  82. return powers[power];
  83. }
  84. /* }}} */
  85. /* {{{ php_round_helper
  86. Actually performs the rounding of a value to integer in a certain mode */
  87. static inline double php_round_helper(double value, int mode) {
  88. double tmp_value;
  89. if (value >= 0.0) {
  90. tmp_value = floor(value + 0.5);
  91. if ((mode == PHP_ROUND_HALF_DOWN && value == (-0.5 + tmp_value)) ||
  92. (mode == PHP_ROUND_HALF_EVEN && value == (0.5 + 2 * floor(tmp_value/2.0))) ||
  93. (mode == PHP_ROUND_HALF_ODD && value == (0.5 + 2 * floor(tmp_value/2.0) - 1.0)))
  94. {
  95. tmp_value = tmp_value - 1.0;
  96. }
  97. } else {
  98. tmp_value = ceil(value - 0.5);
  99. if ((mode == PHP_ROUND_HALF_DOWN && value == (0.5 + tmp_value)) ||
  100. (mode == PHP_ROUND_HALF_EVEN && value == (-0.5 + 2 * ceil(tmp_value/2.0))) ||
  101. (mode == PHP_ROUND_HALF_ODD && value == (-0.5 + 2 * ceil(tmp_value/2.0) + 1.0)))
  102. {
  103. tmp_value = tmp_value + 1.0;
  104. }
  105. }
  106. return tmp_value;
  107. }
  108. /* }}} */
  109. /* {{{ _php_math_round */
  110. /*
  111. * Rounds a number to a certain number of decimal places in a certain rounding
  112. * mode. For the specifics of the algorithm, see http://wiki.php.net/rfc/rounding
  113. */
  114. PHPAPI double _php_math_round(double value, int places, int mode) {
  115. double f1, f2;
  116. double tmp_value;
  117. int precision_places;
  118. if (!zend_finite(value) || value == 0.0) {
  119. return value;
  120. }
  121. places = places < INT_MIN+1 ? INT_MIN+1 : places;
  122. precision_places = 14 - php_intlog10abs(value);
  123. f1 = php_intpow10(abs(places));
  124. /* If the decimal precision guaranteed by FP arithmetic is higher than
  125. the requested places BUT is small enough to make sure a non-zero value
  126. is returned, pre-round the result to the precision */
  127. if (precision_places > places && precision_places - 15 < places) {
  128. int64_t use_precision = precision_places < INT_MIN+1 ? INT_MIN+1 : precision_places;
  129. f2 = php_intpow10(abs((int)use_precision));
  130. if (use_precision >= 0) {
  131. tmp_value = value * f2;
  132. } else {
  133. tmp_value = value / f2;
  134. }
  135. /* preround the result (tmp_value will always be something * 1e14,
  136. thus never larger than 1e15 here) */
  137. tmp_value = php_round_helper(tmp_value, mode);
  138. use_precision = places - precision_places;
  139. use_precision = use_precision < INT_MIN+1 ? INT_MIN+1 : use_precision;
  140. /* now correctly move the decimal point */
  141. f2 = php_intpow10(abs((int)use_precision));
  142. /* because places < precision_places */
  143. tmp_value = tmp_value / f2;
  144. } else {
  145. /* adjust the value */
  146. if (places >= 0) {
  147. tmp_value = value * f1;
  148. } else {
  149. tmp_value = value / f1;
  150. }
  151. /* This value is beyond our precision, so rounding it is pointless */
  152. if (fabs(tmp_value) >= 1e15) {
  153. return value;
  154. }
  155. }
  156. /* round the temp value */
  157. tmp_value = php_round_helper(tmp_value, mode);
  158. /* see if it makes sense to use simple division to round the value */
  159. if (abs(places) < 23) {
  160. if (places > 0) {
  161. tmp_value = tmp_value / f1;
  162. } else {
  163. tmp_value = tmp_value * f1;
  164. }
  165. } else {
  166. /* Simple division can't be used since that will cause wrong results.
  167. Instead, the number is converted to a string and back again using
  168. strtod(). strtod() will return the nearest possible FP value for
  169. that string. */
  170. /* 40 Bytes should be more than enough for this format string. The
  171. float won't be larger than 1e15 anyway. But just in case, use
  172. snprintf() and make sure the buffer is zero-terminated */
  173. char buf[40];
  174. snprintf(buf, 39, "%15fe%d", tmp_value, -places);
  175. buf[39] = '\0';
  176. tmp_value = zend_strtod(buf, NULL);
  177. /* couldn't convert to string and back */
  178. if (!zend_finite(tmp_value) || zend_isnan(tmp_value)) {
  179. tmp_value = value;
  180. }
  181. }
  182. return tmp_value;
  183. }
  184. /* }}} */
  185. /* {{{ proto int|float abs(int|float number)
  186. Return the absolute value of the number */
  187. PHP_FUNCTION(abs)
  188. {
  189. zval *value;
  190. ZEND_PARSE_PARAMETERS_START(1, 1)
  191. Z_PARAM_NUMBER(value)
  192. ZEND_PARSE_PARAMETERS_END();
  193. if (Z_TYPE_P(value) == IS_DOUBLE) {
  194. RETURN_DOUBLE(fabs(Z_DVAL_P(value)));
  195. } else if (Z_TYPE_P(value) == IS_LONG) {
  196. if (Z_LVAL_P(value) == ZEND_LONG_MIN) {
  197. RETURN_DOUBLE(-(double)ZEND_LONG_MIN);
  198. } else {
  199. RETURN_LONG(Z_LVAL_P(value) < 0 ? -Z_LVAL_P(value) : Z_LVAL_P(value));
  200. }
  201. } else {
  202. ZEND_ASSERT(0 && "Unexpected type");
  203. }
  204. }
  205. /* }}} */
  206. /* {{{ proto float ceil(float number)
  207. Returns the next highest integer value of the number */
  208. PHP_FUNCTION(ceil)
  209. {
  210. zval *value;
  211. ZEND_PARSE_PARAMETERS_START(1, 1)
  212. Z_PARAM_NUMBER(value)
  213. ZEND_PARSE_PARAMETERS_END();
  214. if (Z_TYPE_P(value) == IS_DOUBLE) {
  215. RETURN_DOUBLE(ceil(Z_DVAL_P(value)));
  216. } else if (Z_TYPE_P(value) == IS_LONG) {
  217. RETURN_DOUBLE(zval_get_double(value));
  218. } else {
  219. ZEND_ASSERT(0 && "Unexpected type");
  220. }
  221. }
  222. /* }}} */
  223. /* {{{ proto float floor(float number)
  224. Returns the next lowest integer value from the number */
  225. PHP_FUNCTION(floor)
  226. {
  227. zval *value;
  228. ZEND_PARSE_PARAMETERS_START(1, 1)
  229. Z_PARAM_NUMBER(value)
  230. ZEND_PARSE_PARAMETERS_END();
  231. if (Z_TYPE_P(value) == IS_DOUBLE) {
  232. RETURN_DOUBLE(floor(Z_DVAL_P(value)));
  233. } else if (Z_TYPE_P(value) == IS_LONG) {
  234. RETURN_DOUBLE(zval_get_double(value));
  235. } else {
  236. ZEND_ASSERT(0 && "Unexpected type");
  237. }
  238. }
  239. /* }}} */
  240. /* {{{ proto float round(float number [, int precision [, int mode]])
  241. Returns the number rounded to specified precision */
  242. PHP_FUNCTION(round)
  243. {
  244. zval *value;
  245. int places = 0;
  246. zend_long precision = 0;
  247. zend_long mode = PHP_ROUND_HALF_UP;
  248. double return_val;
  249. ZEND_PARSE_PARAMETERS_START(1, 3)
  250. Z_PARAM_NUMBER(value)
  251. Z_PARAM_OPTIONAL
  252. Z_PARAM_LONG(precision)
  253. Z_PARAM_LONG(mode)
  254. ZEND_PARSE_PARAMETERS_END();
  255. if (ZEND_NUM_ARGS() >= 2) {
  256. #if SIZEOF_ZEND_LONG > SIZEOF_INT
  257. if (precision >= 0) {
  258. places = precision > INT_MAX ? INT_MAX : (int)precision;
  259. } else {
  260. places = precision <= INT_MIN ? INT_MIN+1 : (int)precision;
  261. }
  262. #else
  263. places = precision;
  264. #endif
  265. }
  266. switch (Z_TYPE_P(value)) {
  267. case IS_LONG:
  268. /* Simple case - long that doesn't need to be rounded. */
  269. if (places >= 0) {
  270. RETURN_DOUBLE((double) Z_LVAL_P(value));
  271. }
  272. /* break omitted intentionally */
  273. case IS_DOUBLE:
  274. return_val = (Z_TYPE_P(value) == IS_LONG) ? (double)Z_LVAL_P(value) : Z_DVAL_P(value);
  275. return_val = _php_math_round(return_val, (int)places, (int)mode);
  276. RETURN_DOUBLE(return_val);
  277. break;
  278. EMPTY_SWITCH_DEFAULT_CASE()
  279. }
  280. }
  281. /* }}} */
  282. /* {{{ proto float sin(float number)
  283. Returns the sine of the number in radians */
  284. PHP_FUNCTION(sin)
  285. {
  286. double num;
  287. ZEND_PARSE_PARAMETERS_START(1, 1)
  288. Z_PARAM_DOUBLE(num)
  289. ZEND_PARSE_PARAMETERS_END();
  290. RETURN_DOUBLE(sin(num));
  291. }
  292. /* }}} */
  293. /* {{{ proto float cos(float number)
  294. Returns the cosine of the number in radians */
  295. PHP_FUNCTION(cos)
  296. {
  297. double num;
  298. ZEND_PARSE_PARAMETERS_START(1, 1)
  299. Z_PARAM_DOUBLE(num)
  300. ZEND_PARSE_PARAMETERS_END();
  301. RETURN_DOUBLE(cos(num));
  302. }
  303. /* }}} */
  304. /* {{{ proto float tan(float number)
  305. Returns the tangent of the number in radians */
  306. PHP_FUNCTION(tan)
  307. {
  308. double num;
  309. ZEND_PARSE_PARAMETERS_START(1, 1)
  310. Z_PARAM_DOUBLE(num)
  311. ZEND_PARSE_PARAMETERS_END();
  312. RETURN_DOUBLE(tan(num));
  313. }
  314. /* }}} */
  315. /* {{{ proto float asin(float number)
  316. Returns the arc sine of the number in radians */
  317. PHP_FUNCTION(asin)
  318. {
  319. double num;
  320. ZEND_PARSE_PARAMETERS_START(1, 1)
  321. Z_PARAM_DOUBLE(num)
  322. ZEND_PARSE_PARAMETERS_END();
  323. RETURN_DOUBLE(asin(num));
  324. }
  325. /* }}} */
  326. /* {{{ proto float acos(float number)
  327. Return the arc cosine of the number in radians */
  328. PHP_FUNCTION(acos)
  329. {
  330. double num;
  331. ZEND_PARSE_PARAMETERS_START(1, 1)
  332. Z_PARAM_DOUBLE(num)
  333. ZEND_PARSE_PARAMETERS_END();
  334. RETURN_DOUBLE(acos(num));
  335. }
  336. /* }}} */
  337. /* {{{ proto float atan(float number)
  338. Returns the arc tangent of the number in radians */
  339. PHP_FUNCTION(atan)
  340. {
  341. double num;
  342. ZEND_PARSE_PARAMETERS_START(1, 1)
  343. Z_PARAM_DOUBLE(num)
  344. ZEND_PARSE_PARAMETERS_END();
  345. RETURN_DOUBLE(atan(num));
  346. }
  347. /* }}} */
  348. /* {{{ proto float atan2(float y, float x)
  349. Returns the arc tangent of y/x, with the resulting quadrant determined by the signs of y and x */
  350. PHP_FUNCTION(atan2)
  351. {
  352. double num1, num2;
  353. ZEND_PARSE_PARAMETERS_START(2, 2)
  354. Z_PARAM_DOUBLE(num1)
  355. Z_PARAM_DOUBLE(num2)
  356. ZEND_PARSE_PARAMETERS_END();
  357. RETURN_DOUBLE(atan2(num1, num2));
  358. }
  359. /* }}} */
  360. /* {{{ proto float sinh(float number)
  361. Returns the hyperbolic sine of the number, defined as (exp(number) - exp(-number))/2 */
  362. PHP_FUNCTION(sinh)
  363. {
  364. double num;
  365. ZEND_PARSE_PARAMETERS_START(1, 1)
  366. Z_PARAM_DOUBLE(num)
  367. ZEND_PARSE_PARAMETERS_END();
  368. RETURN_DOUBLE(sinh(num));
  369. }
  370. /* }}} */
  371. /* {{{ proto float cosh(float number)
  372. Returns the hyperbolic cosine of the number, defined as (exp(number) + exp(-number))/2 */
  373. PHP_FUNCTION(cosh)
  374. {
  375. double num;
  376. ZEND_PARSE_PARAMETERS_START(1, 1)
  377. Z_PARAM_DOUBLE(num)
  378. ZEND_PARSE_PARAMETERS_END();
  379. RETURN_DOUBLE(cosh(num));
  380. }
  381. /* }}} */
  382. /* {{{ proto float tanh(float number)
  383. Returns the hyperbolic tangent of the number, defined as sinh(number)/cosh(number) */
  384. PHP_FUNCTION(tanh)
  385. {
  386. double num;
  387. ZEND_PARSE_PARAMETERS_START(1, 1)
  388. Z_PARAM_DOUBLE(num)
  389. ZEND_PARSE_PARAMETERS_END();
  390. RETURN_DOUBLE(tanh(num));
  391. }
  392. /* }}} */
  393. /* {{{ proto float asinh(float number)
  394. Returns the inverse hyperbolic sine of the number, i.e. the value whose hyperbolic sine is number */
  395. PHP_FUNCTION(asinh)
  396. {
  397. double num;
  398. ZEND_PARSE_PARAMETERS_START(1, 1)
  399. Z_PARAM_DOUBLE(num)
  400. ZEND_PARSE_PARAMETERS_END();
  401. RETURN_DOUBLE(asinh(num));
  402. }
  403. /* }}} */
  404. /* {{{ proto float acosh(float number)
  405. Returns the inverse hyperbolic cosine of the number, i.e. the value whose hyperbolic cosine is number */
  406. PHP_FUNCTION(acosh)
  407. {
  408. double num;
  409. ZEND_PARSE_PARAMETERS_START(1, 1)
  410. Z_PARAM_DOUBLE(num)
  411. ZEND_PARSE_PARAMETERS_END();
  412. RETURN_DOUBLE(acosh(num));
  413. }
  414. /* }}} */
  415. /* {{{ proto float atanh(float number)
  416. Returns the inverse hyperbolic tangent of the number, i.e. the value whose hyperbolic tangent is number */
  417. PHP_FUNCTION(atanh)
  418. {
  419. double num;
  420. ZEND_PARSE_PARAMETERS_START(1, 1)
  421. Z_PARAM_DOUBLE(num)
  422. ZEND_PARSE_PARAMETERS_END();
  423. RETURN_DOUBLE(atanh(num));
  424. }
  425. /* }}} */
  426. /* {{{ proto float pi(void)
  427. Returns an approximation of pi */
  428. PHP_FUNCTION(pi)
  429. {
  430. ZEND_PARSE_PARAMETERS_NONE();
  431. RETURN_DOUBLE(M_PI);
  432. }
  433. /* }}} */
  434. /* {{{ proto bool is_finite(float val)
  435. Returns whether argument is finite */
  436. PHP_FUNCTION(is_finite)
  437. {
  438. double dval;
  439. ZEND_PARSE_PARAMETERS_START(1, 1)
  440. Z_PARAM_DOUBLE(dval)
  441. ZEND_PARSE_PARAMETERS_END();
  442. RETURN_BOOL(zend_finite(dval));
  443. }
  444. /* }}} */
  445. /* {{{ proto bool is_infinite(float val)
  446. Returns whether argument is infinite */
  447. PHP_FUNCTION(is_infinite)
  448. {
  449. double dval;
  450. ZEND_PARSE_PARAMETERS_START(1, 1)
  451. Z_PARAM_DOUBLE(dval)
  452. ZEND_PARSE_PARAMETERS_END();
  453. RETURN_BOOL(zend_isinf(dval));
  454. }
  455. /* }}} */
  456. /* {{{ proto bool is_nan(float val)
  457. Returns whether argument is not a number */
  458. PHP_FUNCTION(is_nan)
  459. {
  460. double dval;
  461. ZEND_PARSE_PARAMETERS_START(1, 1)
  462. Z_PARAM_DOUBLE(dval)
  463. ZEND_PARSE_PARAMETERS_END();
  464. RETURN_BOOL(zend_isnan(dval));
  465. }
  466. /* }}} */
  467. /* {{{ proto number pow(number base, number exponent)
  468. Returns base raised to the power of exponent. Returns integer result when possible */
  469. PHP_FUNCTION(pow)
  470. {
  471. zval *zbase, *zexp;
  472. ZEND_PARSE_PARAMETERS_START(2, 2)
  473. Z_PARAM_ZVAL(zbase)
  474. Z_PARAM_ZVAL(zexp)
  475. ZEND_PARSE_PARAMETERS_END();
  476. pow_function(return_value, zbase, zexp);
  477. }
  478. /* }}} */
  479. /* {{{ proto float exp(float number)
  480. Returns e raised to the power of the number */
  481. PHP_FUNCTION(exp)
  482. {
  483. double num;
  484. ZEND_PARSE_PARAMETERS_START(1, 1)
  485. Z_PARAM_DOUBLE(num)
  486. ZEND_PARSE_PARAMETERS_END();
  487. RETURN_DOUBLE(exp(num));
  488. }
  489. /* }}} */
  490. /* {{{ proto float expm1(float number)
  491. Returns exp(number) - 1, computed in a way that accurate even when the value of number is close to zero
  492. */
  493. PHP_FUNCTION(expm1)
  494. {
  495. double num;
  496. ZEND_PARSE_PARAMETERS_START(1, 1)
  497. Z_PARAM_DOUBLE(num)
  498. ZEND_PARSE_PARAMETERS_END();
  499. RETURN_DOUBLE(expm1(num));
  500. }
  501. /* }}} */
  502. /* {{{ proto float log1p(float number)
  503. Returns log(1 + number), computed in a way that accurate even when the value of number is close to zero
  504. */
  505. PHP_FUNCTION(log1p)
  506. {
  507. double num;
  508. ZEND_PARSE_PARAMETERS_START(1, 1)
  509. Z_PARAM_DOUBLE(num)
  510. ZEND_PARSE_PARAMETERS_END();
  511. RETURN_DOUBLE(log1p(num));
  512. }
  513. /* }}} */
  514. /* {{{ proto float log(float number, [float base])
  515. Returns the natural logarithm of the number, or the base log if base is specified */
  516. PHP_FUNCTION(log)
  517. {
  518. double num, base = 0;
  519. ZEND_PARSE_PARAMETERS_START(1, 2)
  520. Z_PARAM_DOUBLE(num)
  521. Z_PARAM_OPTIONAL
  522. Z_PARAM_DOUBLE(base)
  523. ZEND_PARSE_PARAMETERS_END();
  524. if (ZEND_NUM_ARGS() == 1) {
  525. RETURN_DOUBLE(log(num));
  526. }
  527. if (base == 2.0) {
  528. RETURN_DOUBLE(log2(num));
  529. }
  530. if (base == 10.0) {
  531. RETURN_DOUBLE(log10(num));
  532. }
  533. if (base == 1.0) {
  534. RETURN_DOUBLE(ZEND_NAN);
  535. }
  536. if (base <= 0.0) {
  537. zend_argument_value_error(2, "must be greater than 0");
  538. RETURN_THROWS();
  539. }
  540. RETURN_DOUBLE(log(num) / log(base));
  541. }
  542. /* }}} */
  543. /* {{{ proto float log10(float number)
  544. Returns the base-10 logarithm of the number */
  545. PHP_FUNCTION(log10)
  546. {
  547. double num;
  548. ZEND_PARSE_PARAMETERS_START(1, 1)
  549. Z_PARAM_DOUBLE(num)
  550. ZEND_PARSE_PARAMETERS_END();
  551. RETURN_DOUBLE(log10(num));
  552. }
  553. /* }}} */
  554. /* {{{ proto float sqrt(float number)
  555. Returns the square root of the number */
  556. PHP_FUNCTION(sqrt)
  557. {
  558. double num;
  559. ZEND_PARSE_PARAMETERS_START(1, 1)
  560. Z_PARAM_DOUBLE(num)
  561. ZEND_PARSE_PARAMETERS_END();
  562. RETURN_DOUBLE(sqrt(num));
  563. }
  564. /* }}} */
  565. /* {{{ proto float hypot(float num1, float num2)
  566. Returns sqrt(num1*num1 + num2*num2) */
  567. PHP_FUNCTION(hypot)
  568. {
  569. double num1, num2;
  570. ZEND_PARSE_PARAMETERS_START(2, 2)
  571. Z_PARAM_DOUBLE(num1)
  572. Z_PARAM_DOUBLE(num2)
  573. ZEND_PARSE_PARAMETERS_END();
  574. RETURN_DOUBLE(hypot(num1, num2));
  575. }
  576. /* }}} */
  577. /* {{{ proto float deg2rad(float number)
  578. Converts the number in degrees to the radian equivalent */
  579. PHP_FUNCTION(deg2rad)
  580. {
  581. double deg;
  582. ZEND_PARSE_PARAMETERS_START(1, 1)
  583. Z_PARAM_DOUBLE(deg)
  584. ZEND_PARSE_PARAMETERS_END();
  585. RETURN_DOUBLE((deg / 180.0) * M_PI);
  586. }
  587. /* }}} */
  588. /* {{{ proto float rad2deg(float number)
  589. Converts the radian number to the equivalent number in degrees */
  590. PHP_FUNCTION(rad2deg)
  591. {
  592. double rad;
  593. ZEND_PARSE_PARAMETERS_START(1, 1)
  594. Z_PARAM_DOUBLE(rad)
  595. ZEND_PARSE_PARAMETERS_END();
  596. RETURN_DOUBLE((rad / M_PI) * 180);
  597. }
  598. /* }}} */
  599. /* {{{ _php_math_basetolong */
  600. /*
  601. * Convert a string representation of a base(2-36) number to a long.
  602. */
  603. PHPAPI zend_long _php_math_basetolong(zval *arg, int base)
  604. {
  605. zend_long num = 0, digit, onum;
  606. zend_long i;
  607. char c, *s;
  608. if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) {
  609. return 0;
  610. }
  611. s = Z_STRVAL_P(arg);
  612. for (i = Z_STRLEN_P(arg); i > 0; i--) {
  613. c = *s++;
  614. digit = (c >= '0' && c <= '9') ? c - '0'
  615. : (c >= 'A' && c <= 'Z') ? c - 'A' + 10
  616. : (c >= 'a' && c <= 'z') ? c - 'a' + 10
  617. : base;
  618. if (digit >= base) {
  619. continue;
  620. }
  621. onum = num;
  622. num = num * base + digit;
  623. if (num > onum)
  624. continue;
  625. {
  626. php_error_docref(NULL, E_WARNING, "Number '%s' is too big to fit in long", s);
  627. return ZEND_LONG_MAX;
  628. }
  629. }
  630. return num;
  631. }
  632. /* }}} */
  633. /* {{{ _php_math_basetozval */
  634. /*
  635. * Convert a string representation of a base(2-36) number to a zval.
  636. */
  637. PHPAPI void _php_math_basetozval(zend_string *str, int base, zval *ret)
  638. {
  639. zend_long num = 0;
  640. double fnum = 0;
  641. int mode = 0;
  642. char c, *s, *e;
  643. zend_long cutoff;
  644. int cutlim;
  645. int invalidchars = 0;
  646. s = ZSTR_VAL(str);
  647. e = s + ZSTR_LEN(str);
  648. /* Skip leading whitespace */
  649. while (s < e && isspace(*s)) s++;
  650. /* Skip trailing whitespace */
  651. while (s < e && isspace(*(e-1))) e--;
  652. if (e - s >= 2) {
  653. if (base == 16 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) s += 2;
  654. if (base == 8 && s[0] == '0' && (s[1] == 'o' || s[1] == 'O')) s += 2;
  655. if (base == 2 && s[0] == '0' && (s[1] == 'b' || s[1] == 'B')) s += 2;
  656. }
  657. cutoff = ZEND_LONG_MAX / base;
  658. cutlim = ZEND_LONG_MAX % base;
  659. while (s < e) {
  660. c = *s++;
  661. /* might not work for EBCDIC */
  662. if (c >= '0' && c <= '9')
  663. c -= '0';
  664. else if (c >= 'A' && c <= 'Z')
  665. c -= 'A' - 10;
  666. else if (c >= 'a' && c <= 'z')
  667. c -= 'a' - 10;
  668. else {
  669. invalidchars++;
  670. continue;
  671. }
  672. if (c >= base) {
  673. invalidchars++;
  674. continue;
  675. }
  676. switch (mode) {
  677. case 0: /* Integer */
  678. if (num < cutoff || (num == cutoff && c <= cutlim)) {
  679. num = num * base + c;
  680. break;
  681. } else {
  682. fnum = (double)num;
  683. mode = 1;
  684. }
  685. /* fall-through */
  686. case 1: /* Float */
  687. fnum = fnum * base + c;
  688. }
  689. }
  690. if (invalidchars > 0) {
  691. zend_error(E_DEPRECATED, "Invalid characters passed for attempted conversion, these have been ignored");
  692. }
  693. if (mode == 1) {
  694. ZVAL_DOUBLE(ret, fnum);
  695. } else {
  696. ZVAL_LONG(ret, num);
  697. }
  698. }
  699. /* }}} */
  700. /* {{{ _php_math_longtobase */
  701. /*
  702. * Convert a long to a string containing a base(2-36) representation of
  703. * the number.
  704. */
  705. PHPAPI zend_string * _php_math_longtobase(zval *arg, int base)
  706. {
  707. static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  708. char buf[(sizeof(zend_ulong) << 3) + 1];
  709. char *ptr, *end;
  710. zend_ulong value;
  711. if (Z_TYPE_P(arg) != IS_LONG || base < 2 || base > 36) {
  712. return ZSTR_EMPTY_ALLOC();
  713. }
  714. value = Z_LVAL_P(arg);
  715. end = ptr = buf + sizeof(buf) - 1;
  716. *ptr = '\0';
  717. do {
  718. ZEND_ASSERT(ptr > buf);
  719. *--ptr = digits[value % base];
  720. value /= base;
  721. } while (value);
  722. return zend_string_init(ptr, end - ptr, 0);
  723. }
  724. /* }}} */
  725. /* {{{ _php_math_zvaltobase */
  726. /*
  727. * Convert a zval to a string containing a base(2-36) representation of
  728. * the number.
  729. */
  730. PHPAPI zend_string * _php_math_zvaltobase(zval *arg, int base)
  731. {
  732. static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  733. if ((Z_TYPE_P(arg) != IS_LONG && Z_TYPE_P(arg) != IS_DOUBLE) || base < 2 || base > 36) {
  734. return ZSTR_EMPTY_ALLOC();
  735. }
  736. if (Z_TYPE_P(arg) == IS_DOUBLE) {
  737. double fvalue = floor(Z_DVAL_P(arg)); /* floor it just in case */
  738. char *ptr, *end;
  739. char buf[(sizeof(double) << 3) + 1];
  740. /* Don't try to convert +/- infinity */
  741. if (fvalue == ZEND_INFINITY || fvalue == -ZEND_INFINITY) {
  742. php_error_docref(NULL, E_WARNING, "Number too large");
  743. return ZSTR_EMPTY_ALLOC();
  744. }
  745. end = ptr = buf + sizeof(buf) - 1;
  746. *ptr = '\0';
  747. do {
  748. *--ptr = digits[(int) fmod(fvalue, base)];
  749. fvalue /= base;
  750. } while (ptr > buf && fabs(fvalue) >= 1);
  751. return zend_string_init(ptr, end - ptr, 0);
  752. }
  753. return _php_math_longtobase(arg, base);
  754. }
  755. /* }}} */
  756. /* {{{ proto int|float bindec(string binary_number)
  757. Returns the decimal equivalent of the binary number */
  758. PHP_FUNCTION(bindec)
  759. {
  760. zend_string *arg;
  761. ZEND_PARSE_PARAMETERS_START(1, 1)
  762. Z_PARAM_STR(arg)
  763. ZEND_PARSE_PARAMETERS_END();
  764. _php_math_basetozval(arg, 2, return_value);
  765. }
  766. /* }}} */
  767. /* {{{ proto int|flat hexdec(string hexadecimal_number)
  768. Returns the decimal equivalent of the hexadecimal number */
  769. PHP_FUNCTION(hexdec)
  770. {
  771. zend_string *arg;
  772. ZEND_PARSE_PARAMETERS_START(1, 1)
  773. Z_PARAM_STR(arg)
  774. ZEND_PARSE_PARAMETERS_END();
  775. _php_math_basetozval(arg, 16, return_value);
  776. }
  777. /* }}} */
  778. /* {{{ proto int|float octdec(string octal_number)
  779. Returns the decimal equivalent of an octal string */
  780. PHP_FUNCTION(octdec)
  781. {
  782. zend_string *arg;
  783. ZEND_PARSE_PARAMETERS_START(1, 1)
  784. Z_PARAM_STR(arg)
  785. ZEND_PARSE_PARAMETERS_END();
  786. _php_math_basetozval(arg, 8, return_value);
  787. }
  788. /* }}} */
  789. /* {{{ proto string decbin(int decimal_number)
  790. Returns a string containing a binary representation of the number */
  791. PHP_FUNCTION(decbin)
  792. {
  793. zval *arg;
  794. zend_string *result;
  795. ZEND_PARSE_PARAMETERS_START(1, 1)
  796. Z_PARAM_ZVAL(arg)
  797. ZEND_PARSE_PARAMETERS_END();
  798. convert_to_long_ex(arg);
  799. result = _php_math_longtobase(arg, 2);
  800. RETURN_STR(result);
  801. }
  802. /* }}} */
  803. /* {{{ proto string decoct(int decimal_number)
  804. Returns a string containing an octal representation of the given number */
  805. PHP_FUNCTION(decoct)
  806. {
  807. zval *arg;
  808. zend_string *result;
  809. ZEND_PARSE_PARAMETERS_START(1, 1)
  810. Z_PARAM_ZVAL(arg)
  811. ZEND_PARSE_PARAMETERS_END();
  812. convert_to_long_ex(arg);
  813. result = _php_math_longtobase(arg, 8);
  814. RETURN_STR(result);
  815. }
  816. /* }}} */
  817. /* {{{ proto string dechex(int decimal_number)
  818. Returns a string containing a hexadecimal representation of the given number */
  819. PHP_FUNCTION(dechex)
  820. {
  821. zval *arg;
  822. zend_string *result;
  823. ZEND_PARSE_PARAMETERS_START(1, 1)
  824. Z_PARAM_ZVAL(arg)
  825. ZEND_PARSE_PARAMETERS_END();
  826. convert_to_long_ex(arg);
  827. result = _php_math_longtobase(arg, 16);
  828. RETURN_STR(result);
  829. }
  830. /* }}} */
  831. /* {{{ proto string|false base_convert(string number, int frombase, int tobase)
  832. Converts a number in a string from any base <= 36 to any base <= 36 */
  833. PHP_FUNCTION(base_convert)
  834. {
  835. zval *number, temp;
  836. zend_long frombase, tobase;
  837. zend_string *result;
  838. ZEND_PARSE_PARAMETERS_START(3, 3)
  839. Z_PARAM_ZVAL(number)
  840. Z_PARAM_LONG(frombase)
  841. Z_PARAM_LONG(tobase)
  842. ZEND_PARSE_PARAMETERS_END();
  843. if (!try_convert_to_string(number)) {
  844. RETURN_THROWS();
  845. }
  846. if (frombase < 2 || frombase > 36) {
  847. zend_argument_value_error(2, "must be between 2 and 36 (inclusive)");
  848. RETURN_THROWS();
  849. }
  850. if (tobase < 2 || tobase > 36) {
  851. zend_argument_value_error(3, "must be between 2 and 36 (inclusive)");
  852. RETURN_THROWS();
  853. }
  854. _php_math_basetozval(Z_STR_P(number), (int)frombase, &temp);
  855. result = _php_math_zvaltobase(&temp, (int)tobase);
  856. RETVAL_STR(result);
  857. }
  858. /* }}} */
  859. /* {{{ _php_math_number_format
  860. */
  861. PHPAPI zend_string *_php_math_number_format(double d, int dec, char dec_point, char thousand_sep)
  862. {
  863. return _php_math_number_format_ex(d, dec, &dec_point, 1, &thousand_sep, 1);
  864. }
  865. PHPAPI zend_string *_php_math_number_format_ex(double d, int dec, char *dec_point,
  866. size_t dec_point_len, char *thousand_sep, size_t thousand_sep_len)
  867. {
  868. zend_string *res;
  869. zend_string *tmpbuf;
  870. char *s, *t; /* source, target */
  871. char *dp;
  872. size_t integral;
  873. size_t reslen = 0;
  874. int count = 0;
  875. int is_negative=0;
  876. if (d < 0) {
  877. is_negative = 1;
  878. d = -d;
  879. }
  880. dec = MAX(0, dec);
  881. d = _php_math_round(d, dec, PHP_ROUND_HALF_UP);
  882. tmpbuf = strpprintf(0, "%.*F", dec, d);
  883. if (tmpbuf == NULL) {
  884. return NULL;
  885. } else if (!isdigit((int)ZSTR_VAL(tmpbuf)[0])) {
  886. return tmpbuf;
  887. }
  888. /* Check if the number is no longer negative after rounding */
  889. if (is_negative && d == 0) {
  890. is_negative = 0;
  891. }
  892. /* find decimal point, if expected */
  893. if (dec) {
  894. dp = strpbrk(ZSTR_VAL(tmpbuf), ".,");
  895. } else {
  896. dp = NULL;
  897. }
  898. /* calculate the length of the return buffer */
  899. if (dp) {
  900. integral = (dp - ZSTR_VAL(tmpbuf));
  901. } else {
  902. /* no decimal point was found */
  903. integral = ZSTR_LEN(tmpbuf);
  904. }
  905. /* allow for thousand separators */
  906. if (thousand_sep) {
  907. integral = zend_safe_addmult((integral-1)/3, thousand_sep_len, integral, "number formatting");
  908. }
  909. reslen = integral;
  910. if (dec) {
  911. reslen += dec;
  912. if (dec_point) {
  913. reslen = zend_safe_addmult(reslen, 1, dec_point_len, "number formatting");
  914. }
  915. }
  916. /* add a byte for minus sign */
  917. if (is_negative) {
  918. reslen++;
  919. }
  920. res = zend_string_alloc(reslen, 0);
  921. s = ZSTR_VAL(tmpbuf) + ZSTR_LEN(tmpbuf) - 1;
  922. t = ZSTR_VAL(res) + reslen;
  923. *t-- = '\0';
  924. /* copy the decimal places.
  925. * Take care, as the sprintf implementation may return less places than
  926. * we requested due to internal buffer limitations */
  927. if (dec) {
  928. size_t declen = (dp ? s - dp : 0);
  929. size_t topad = (size_t)dec > declen ? dec - declen : 0;
  930. /* pad with '0's */
  931. while (topad--) {
  932. *t-- = '0';
  933. }
  934. if (dp) {
  935. s -= declen + 1; /* +1 to skip the point */
  936. t -= declen;
  937. /* now copy the chars after the point */
  938. memcpy(t + 1, dp + 1, declen);
  939. }
  940. /* add decimal point */
  941. if (dec_point) {
  942. t -= dec_point_len;
  943. memcpy(t + 1, dec_point, dec_point_len);
  944. }
  945. }
  946. /* copy the numbers before the decimal point, adding thousand
  947. * separator every three digits */
  948. while (s >= ZSTR_VAL(tmpbuf)) {
  949. *t-- = *s--;
  950. if (thousand_sep && (++count%3)==0 && s >= ZSTR_VAL(tmpbuf)) {
  951. t -= thousand_sep_len;
  952. memcpy(t + 1, thousand_sep, thousand_sep_len);
  953. }
  954. }
  955. /* and a minus sign, if needed */
  956. if (is_negative) {
  957. *t-- = '-';
  958. }
  959. ZSTR_LEN(res) = reslen;
  960. zend_string_release_ex(tmpbuf, 0);
  961. return res;
  962. }
  963. /* {{{ proto string number_format(float number [, int num_decimal_places [, string dec_separator, string thousands_separator]])
  964. Formats a number with grouped thousands */
  965. PHP_FUNCTION(number_format)
  966. {
  967. double num;
  968. zend_long dec = 0;
  969. char *thousand_sep = NULL, *dec_point = NULL;
  970. char thousand_sep_chr = ',', dec_point_chr = '.';
  971. size_t thousand_sep_len = 0, dec_point_len = 0;
  972. ZEND_PARSE_PARAMETERS_START(1, 4)
  973. Z_PARAM_DOUBLE(num)
  974. Z_PARAM_OPTIONAL
  975. Z_PARAM_LONG(dec)
  976. Z_PARAM_STRING_OR_NULL(dec_point, dec_point_len)
  977. Z_PARAM_STRING_OR_NULL(thousand_sep, thousand_sep_len)
  978. ZEND_PARSE_PARAMETERS_END();
  979. switch(ZEND_NUM_ARGS()) {
  980. case 1:
  981. RETURN_STR(_php_math_number_format(num, 0, dec_point_chr, thousand_sep_chr));
  982. break;
  983. case 2:
  984. RETURN_STR(_php_math_number_format(num, (int)dec, dec_point_chr, thousand_sep_chr));
  985. break;
  986. case 4:
  987. if (dec_point == NULL) {
  988. dec_point = &dec_point_chr;
  989. dec_point_len = 1;
  990. }
  991. if (thousand_sep == NULL) {
  992. thousand_sep = &thousand_sep_chr;
  993. thousand_sep_len = 1;
  994. }
  995. RETVAL_STR(_php_math_number_format_ex(num, (int)dec,
  996. dec_point, dec_point_len, thousand_sep, thousand_sep_len));
  997. break;
  998. default:
  999. WRONG_PARAM_COUNT;
  1000. }
  1001. }
  1002. /* }}} */
  1003. /* {{{ proto float fmod(float x, float y)
  1004. Returns the remainder of dividing x by y as a float */
  1005. PHP_FUNCTION(fmod)
  1006. {
  1007. double num1, num2;
  1008. ZEND_PARSE_PARAMETERS_START(2, 2)
  1009. Z_PARAM_DOUBLE(num1)
  1010. Z_PARAM_DOUBLE(num2)
  1011. ZEND_PARSE_PARAMETERS_END();
  1012. RETURN_DOUBLE(fmod(num1, num2));
  1013. }
  1014. /* }}} */
  1015. /* {{{ proto float fdiv(float dividend, float divisor)
  1016. Perform floating-point division of dividend / divisor
  1017. with IEEE-754 semantics for division by zero. */
  1018. #ifdef __clang__
  1019. __attribute__((no_sanitize("float-divide-by-zero")))
  1020. #endif
  1021. PHP_FUNCTION(fdiv)
  1022. {
  1023. double dividend, divisor;
  1024. ZEND_PARSE_PARAMETERS_START(2, 2)
  1025. Z_PARAM_DOUBLE(dividend)
  1026. Z_PARAM_DOUBLE(divisor)
  1027. ZEND_PARSE_PARAMETERS_END();
  1028. RETURN_DOUBLE(dividend / divisor);
  1029. }
  1030. /* }}} */
  1031. /* {{{ proto int intdiv(int dividend, int divisor)
  1032. Returns the integer quotient of the division of dividend by divisor */
  1033. PHP_FUNCTION(intdiv)
  1034. {
  1035. zend_long dividend, divisor;
  1036. ZEND_PARSE_PARAMETERS_START(2, 2)
  1037. Z_PARAM_LONG(dividend)
  1038. Z_PARAM_LONG(divisor)
  1039. ZEND_PARSE_PARAMETERS_END();
  1040. if (divisor == 0) {
  1041. zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Division by zero");
  1042. RETURN_THROWS();
  1043. } else if (divisor == -1 && dividend == ZEND_LONG_MIN) {
  1044. /* Prevent overflow error/crash ... really should not happen:
  1045. We don't return a float here as that violates function contract */
  1046. zend_throw_exception_ex(zend_ce_arithmetic_error, 0, "Division of PHP_INT_MIN by -1 is not an integer");
  1047. RETURN_THROWS();
  1048. }
  1049. RETURN_LONG(dividend / divisor);
  1050. }
  1051. /* }}} */