PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ext/standard/math.c

http://github.com/infusion/PHP
C | 1439 lines | 952 code | 205 blank | 282 comment | 326 complexity | 40e2b1e5b751857a532e8349bb9f3a81 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2011 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Jim Winstead <jimw@php.net> |
  16. | Stig S�ther Bakken <ssb@php.net> |
  17. | Zeev Suraski <zeev@zend.com> |
  18. | PHP 4.0 patches by Thies C. Arntzen <thies@thieso.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. /* $Id: math.c 306939 2011-01-01 02:19:59Z felipe $ */
  22. #include "php.h"
  23. #include "php_math.h"
  24. #include "zend_multiply.h"
  25. #include <math.h>
  26. #include <float.h>
  27. #include <stdlib.h>
  28. #include "basic_functions.h"
  29. /* {{{ php_intlog10abs
  30. Returns floor(log10(fabs(val))), uses fast binary search */
  31. static inline int php_intlog10abs(double value) {
  32. int result;
  33. value = fabs(value);
  34. if (value < 1e-8 || value > 1e23) {
  35. result = (int)floor(log10(value));
  36. } else {
  37. static const double values[] = {
  38. 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1,
  39. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,
  40. 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15,
  41. 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
  42. /* Do a binary search with 5 steps */
  43. result = 16;
  44. if (value < values[result]) {
  45. result -= 8;
  46. } else {
  47. result += 8;
  48. }
  49. if (value < values[result]) {
  50. result -= 4;
  51. } else {
  52. result += 4;
  53. }
  54. if (value < values[result]) {
  55. result -= 2;
  56. } else {
  57. result += 2;
  58. }
  59. if (value < values[result]) {
  60. result -= 1;
  61. } else {
  62. result += 1;
  63. }
  64. if (value < values[result]) {
  65. result -= 1;
  66. }
  67. result -= 8;
  68. }
  69. return result;
  70. }
  71. /* }}} */
  72. /* {{{ php_intpow10
  73. Returns pow(10.0, (double)power), uses fast lookup table for exact powers */
  74. static inline double php_intpow10(int power) {
  75. static const double powers[] = {
  76. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7,
  77. 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15,
  78. 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
  79. /* Not in lookup table */
  80. if (power < 0 || power > 22) {
  81. return pow(10.0, (double)power);
  82. }
  83. return powers[power];
  84. }
  85. /* }}} */
  86. /* {{{ php_round_helper
  87. Actually performs the rounding of a value to integer in a certain mode */
  88. static inline double php_round_helper(double value, int mode) {
  89. double tmp_value;
  90. if (value >= 0.0) {
  91. tmp_value = floor(value + 0.5);
  92. if ((mode == PHP_ROUND_HALF_DOWN && value == (-0.5 + tmp_value)) ||
  93. (mode == PHP_ROUND_HALF_EVEN && value == (0.5 + 2 * floor(tmp_value/2.0))) ||
  94. (mode == PHP_ROUND_HALF_ODD && value == (0.5 + 2 * floor(tmp_value/2.0) - 1.0)))
  95. {
  96. tmp_value = tmp_value - 1.0;
  97. }
  98. } else {
  99. tmp_value = ceil(value - 0.5);
  100. if ((mode == PHP_ROUND_HALF_DOWN && value == (0.5 + tmp_value)) ||
  101. (mode == PHP_ROUND_HALF_EVEN && value == (-0.5 + 2 * ceil(tmp_value/2.0))) ||
  102. (mode == PHP_ROUND_HALF_ODD && value == (-0.5 + 2 * ceil(tmp_value/2.0) + 1.0)))
  103. {
  104. tmp_value = tmp_value + 1.0;
  105. }
  106. }
  107. return tmp_value;
  108. }
  109. /* }}} */
  110. /* {{{ _php_math_round */
  111. /*
  112. * Rounds a number to a certain number of decimal places in a certain rounding
  113. * mode. For the specifics of the algorithm, see http://wiki.php.net/rfc/rounding
  114. */
  115. PHPAPI double _php_math_round(double value, int places, int mode) {
  116. double f1, f2;
  117. double tmp_value;
  118. int precision_places;
  119. if ((precision_places = php_intlog10abs(value)) > 0) {
  120. precision_places = 14 - php_intlog10abs(value);
  121. } else {
  122. precision_places = 14;
  123. }
  124. f1 = php_intpow10(abs(places));
  125. /* If the decimal precision guaranteed by FP arithmetic is higher than
  126. the requested places BUT is small enough to make sure a non-zero value
  127. is returned, pre-round the result to the precision */
  128. if (precision_places > places && precision_places - places < 15) {
  129. f2 = php_intpow10(abs(precision_places));
  130. if (precision_places >= 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. /* now correctly move the decimal point */
  139. f2 = php_intpow10(abs(places - precision_places));
  140. /* because places < precision_places */
  141. tmp_value = tmp_value / f2;
  142. } else {
  143. /* adjust the value */
  144. if (places >= 0) {
  145. tmp_value = value * f1;
  146. } else {
  147. tmp_value = value / f1;
  148. }
  149. /* This value is beyond our precision, so rounding it is pointless */
  150. if (fabs(tmp_value) >= 1e15) {
  151. return value;
  152. }
  153. }
  154. /* round the temp value */
  155. tmp_value = php_round_helper(tmp_value, mode);
  156. /* see if it makes sense to use simple division to round the value */
  157. if (abs(places) < 23) {
  158. if (places > 0) {
  159. tmp_value = tmp_value / f1;
  160. } else {
  161. tmp_value = tmp_value * f1;
  162. }
  163. } else {
  164. /* Simple division can't be used since that will cause wrong results.
  165. Instead, the number is converted to a string and back again using
  166. strtod(). strtod() will return the nearest possible FP value for
  167. that string. */
  168. /* 40 Bytes should be more than enough for this format string. The
  169. float won't be larger than 1e15 anyway. But just in case, use
  170. snprintf() and make sure the buffer is zero-terminated */
  171. char buf[40];
  172. snprintf(buf, 39, "%15fe%d", tmp_value, -places);
  173. buf[39] = '\0';
  174. tmp_value = zend_strtod(buf, NULL);
  175. /* couldn't convert to string and back */
  176. if (!zend_finite(tmp_value) || zend_isnan(tmp_value)) {
  177. tmp_value = value;
  178. }
  179. }
  180. return tmp_value;
  181. }
  182. /* }}} */
  183. /* {{{ php_asinh
  184. */
  185. static double php_asinh(double z)
  186. {
  187. #ifdef HAVE_ASINH
  188. return(asinh(z));
  189. #else
  190. return(log(z + sqrt(1 + pow(z, 2))) / log(M_E));
  191. #endif
  192. }
  193. /* }}} */
  194. /* {{{ php_acosh
  195. */
  196. static double php_acosh(double x)
  197. {
  198. #ifdef HAVE_ACOSH
  199. return(acosh(x));
  200. #else
  201. return(log(x + sqrt(x * x - 1)));
  202. #endif
  203. }
  204. /* }}} */
  205. /* {{{ php_atanh
  206. */
  207. static double php_atanh(double z)
  208. {
  209. #ifdef HAVE_ATANH
  210. return(atanh(z));
  211. #else
  212. return(0.5 * log((1 + z) / (1 - z)));
  213. #endif
  214. }
  215. /* }}} */
  216. /* {{{ php_log1p
  217. */
  218. static double php_log1p(double x)
  219. {
  220. #ifdef HAVE_LOG1P
  221. return(log1p(x));
  222. #else
  223. return(log(1 + x));
  224. #endif
  225. }
  226. /* }}} */
  227. /* {{{ php_expm1
  228. */
  229. static double php_expm1(double x)
  230. {
  231. #if !defined(PHP_WIN32) && !defined(NETWARE)
  232. return(expm1(x));
  233. #else
  234. return(exp(x) - 1);
  235. #endif
  236. }
  237. /* }}}*/
  238. /* {{{ proto int abs(int number)
  239. Return the absolute value of the number */
  240. PHP_FUNCTION(abs)
  241. {
  242. zval **value;
  243. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &value) == FAILURE) {
  244. return;
  245. }
  246. convert_scalar_to_number_ex(value);
  247. if (Z_TYPE_PP(value) == IS_DOUBLE) {
  248. RETURN_DOUBLE(fabs(Z_DVAL_PP(value)));
  249. } else if (Z_TYPE_PP(value) == IS_LONG) {
  250. if (Z_LVAL_PP(value) == LONG_MIN) {
  251. RETURN_DOUBLE(-(double)LONG_MIN);
  252. } else {
  253. RETURN_LONG(Z_LVAL_PP(value) < 0 ? -Z_LVAL_PP(value) : Z_LVAL_PP(value));
  254. }
  255. }
  256. RETURN_FALSE;
  257. }
  258. /* }}} */
  259. /* {{{ proto float ceil(float number)
  260. Returns the next highest integer value of the number */
  261. PHP_FUNCTION(ceil)
  262. {
  263. zval **value;
  264. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &value) == FAILURE) {
  265. return;
  266. }
  267. convert_scalar_to_number_ex(value);
  268. if (Z_TYPE_PP(value) == IS_DOUBLE) {
  269. RETURN_DOUBLE(ceil(Z_DVAL_PP(value)));
  270. } else if (Z_TYPE_PP(value) == IS_LONG) {
  271. convert_to_double_ex(value);
  272. RETURN_DOUBLE(Z_DVAL_PP(value));
  273. }
  274. RETURN_FALSE;
  275. }
  276. /* }}} */
  277. /* {{{ proto float floor(float number)
  278. Returns the next lowest integer value from the number */
  279. PHP_FUNCTION(floor)
  280. {
  281. zval **value;
  282. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &value) == FAILURE) {
  283. return;
  284. }
  285. convert_scalar_to_number_ex(value);
  286. if (Z_TYPE_PP(value) == IS_DOUBLE) {
  287. RETURN_DOUBLE(floor(Z_DVAL_PP(value)));
  288. } else if (Z_TYPE_PP(value) == IS_LONG) {
  289. convert_to_double_ex(value);
  290. RETURN_DOUBLE(Z_DVAL_PP(value));
  291. }
  292. RETURN_FALSE;
  293. }
  294. /* }}} */
  295. /* {{{ proto float round(float number [, int precision [, int mode]])
  296. Returns the number rounded to specified precision */
  297. PHP_FUNCTION(round)
  298. {
  299. zval **value;
  300. int places = 0;
  301. long precision = 0;
  302. long mode = PHP_ROUND_HALF_UP;
  303. double return_val;
  304. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|ll", &value, &precision, &mode) == FAILURE) {
  305. return;
  306. }
  307. if (ZEND_NUM_ARGS() >= 2) {
  308. places = (int) precision;
  309. }
  310. convert_scalar_to_number_ex(value);
  311. switch (Z_TYPE_PP(value)) {
  312. case IS_LONG:
  313. /* Simple case - long that doesn't need to be rounded. */
  314. if (places >= 0) {
  315. RETURN_DOUBLE((double) Z_LVAL_PP(value));
  316. }
  317. /* break omitted intentionally */
  318. case IS_DOUBLE:
  319. return_val = (Z_TYPE_PP(value) == IS_LONG) ? (double)Z_LVAL_PP(value) : Z_DVAL_PP(value);
  320. return_val = _php_math_round(return_val, places, mode);
  321. RETURN_DOUBLE(return_val);
  322. break;
  323. default:
  324. RETURN_FALSE;
  325. break;
  326. }
  327. }
  328. /* }}} */
  329. /* {{{ proto float sin(float number)
  330. Returns the sine of the number in radians */
  331. PHP_FUNCTION(sin)
  332. {
  333. double num;
  334. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  335. return;
  336. }
  337. RETURN_DOUBLE(sin(num));
  338. }
  339. /* }}} */
  340. /* {{{ proto float cos(float number)
  341. Returns the cosine of the number in radians */
  342. PHP_FUNCTION(cos)
  343. {
  344. double num;
  345. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  346. return;
  347. }
  348. RETURN_DOUBLE(cos(num));
  349. }
  350. /* }}} */
  351. /* {{{ proto float tan(float number)
  352. Returns the tangent of the number in radians */
  353. PHP_FUNCTION(tan)
  354. {
  355. double num;
  356. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  357. return;
  358. }
  359. RETURN_DOUBLE(tan(num));
  360. }
  361. /* }}} */
  362. /* {{{ proto float asin(float number)
  363. Returns the arc sine of the number in radians */
  364. PHP_FUNCTION(asin)
  365. {
  366. double num;
  367. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  368. return;
  369. }
  370. RETURN_DOUBLE(asin(num));
  371. }
  372. /* }}} */
  373. /* {{{ proto float acos(float number)
  374. Return the arc cosine of the number in radians */
  375. PHP_FUNCTION(acos)
  376. {
  377. double num;
  378. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  379. return;
  380. }
  381. RETURN_DOUBLE(acos(num));
  382. }
  383. /* }}} */
  384. /* {{{ proto float atan(float number)
  385. Returns the arc tangent of the number in radians */
  386. PHP_FUNCTION(atan)
  387. {
  388. double num;
  389. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  390. return;
  391. }
  392. RETURN_DOUBLE(atan(num));
  393. }
  394. /* }}} */
  395. /* {{{ proto float atan2(float y, float x)
  396. Returns the arc tangent of y/x, with the resulting quadrant determined by the signs of y and x */
  397. PHP_FUNCTION(atan2)
  398. {
  399. double num1, num2;
  400. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd", &num1, &num2) == FAILURE) {
  401. return;
  402. }
  403. RETURN_DOUBLE(atan2(num1, num2));
  404. }
  405. /* }}} */
  406. /* {{{ proto float sinh(float number)
  407. Returns the hyperbolic sine of the number, defined as (exp(number) - exp(-number))/2 */
  408. PHP_FUNCTION(sinh)
  409. {
  410. double num;
  411. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  412. return;
  413. }
  414. RETURN_DOUBLE(sinh(num));
  415. }
  416. /* }}} */
  417. /* {{{ proto float cosh(float number)
  418. Returns the hyperbolic cosine of the number, defined as (exp(number) + exp(-number))/2 */
  419. PHP_FUNCTION(cosh)
  420. {
  421. double num;
  422. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  423. return;
  424. }
  425. RETURN_DOUBLE(cosh(num));
  426. }
  427. /* }}} */
  428. /* {{{ proto float tanh(float number)
  429. Returns the hyperbolic tangent of the number, defined as sinh(number)/cosh(number) */
  430. PHP_FUNCTION(tanh)
  431. {
  432. double num;
  433. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  434. return;
  435. }
  436. RETURN_DOUBLE(tanh(num));
  437. }
  438. /* }}} */
  439. /* {{{ proto float asinh(float number)
  440. Returns the inverse hyperbolic sine of the number, i.e. the value whose hyperbolic sine is number */
  441. PHP_FUNCTION(asinh)
  442. {
  443. double num;
  444. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  445. return;
  446. }
  447. RETURN_DOUBLE(php_asinh(num));
  448. }
  449. /* }}} */
  450. /* {{{ proto float acosh(float number)
  451. Returns the inverse hyperbolic cosine of the number, i.e. the value whose hyperbolic cosine is number */
  452. PHP_FUNCTION(acosh)
  453. {
  454. double num;
  455. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  456. return;
  457. }
  458. RETURN_DOUBLE(php_acosh(num));
  459. }
  460. /* }}} */
  461. /* {{{ proto float atanh(float number)
  462. Returns the inverse hyperbolic tangent of the number, i.e. the value whose hyperbolic tangent is number */
  463. PHP_FUNCTION(atanh)
  464. {
  465. double num;
  466. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  467. return;
  468. }
  469. RETURN_DOUBLE(php_atanh(num));
  470. }
  471. /* }}} */
  472. /* {{{ proto float pi(void)
  473. Returns an approximation of pi */
  474. PHP_FUNCTION(pi)
  475. {
  476. RETURN_DOUBLE(M_PI);
  477. }
  478. /* }}} */
  479. /* {{{ proto bool is_finite(float val)
  480. Returns whether argument is finite */
  481. PHP_FUNCTION(is_finite)
  482. {
  483. double dval;
  484. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dval) == FAILURE) {
  485. return;
  486. }
  487. RETURN_BOOL(zend_finite(dval));
  488. }
  489. /* }}} */
  490. /* {{{ proto bool is_infinite(float val)
  491. Returns whether argument is infinite */
  492. PHP_FUNCTION(is_infinite)
  493. {
  494. double dval;
  495. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dval) == FAILURE) {
  496. return;
  497. }
  498. RETURN_BOOL(zend_isinf(dval));
  499. }
  500. /* }}} */
  501. /* {{{ proto bool is_nan(float val)
  502. Returns whether argument is not a number */
  503. PHP_FUNCTION(is_nan)
  504. {
  505. double dval;
  506. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dval) == FAILURE) {
  507. return;
  508. }
  509. RETURN_BOOL(zend_isnan(dval));
  510. }
  511. /* }}} */
  512. /* {{{ proto number pow(number base, number exponent)
  513. Returns base raised to the power of exponent. Returns integer result when possible */
  514. PHP_FUNCTION(pow)
  515. {
  516. zval *zbase, *zexp;
  517. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/z/", &zbase, &zexp) == FAILURE) {
  518. return;
  519. }
  520. /* make sure we're dealing with numbers */
  521. convert_scalar_to_number(zbase TSRMLS_CC);
  522. convert_scalar_to_number(zexp TSRMLS_CC);
  523. /* if both base and exponent were longs, we'll try to get a long out */
  524. if (Z_TYPE_P(zbase) == IS_LONG && Z_TYPE_P(zexp) == IS_LONG && Z_LVAL_P(zexp) >= 0) {
  525. long l1 = 1, l2 = Z_LVAL_P(zbase), i = Z_LVAL_P(zexp);
  526. if (i == 0) {
  527. RETURN_LONG(1L);
  528. } else if (l2 == 0) {
  529. RETURN_LONG(0);
  530. }
  531. /* calculate pow(long,long) in O(log exp) operations, bail if overflow */
  532. while (i >= 1) {
  533. int overflow;
  534. double dval = 0.0;
  535. if (i % 2) {
  536. --i;
  537. ZEND_SIGNED_MULTIPLY_LONG(l1,l2,l1,dval,overflow);
  538. if (overflow) RETURN_DOUBLE(dval * pow(l2,i));
  539. } else {
  540. i /= 2;
  541. ZEND_SIGNED_MULTIPLY_LONG(l2,l2,l2,dval,overflow);
  542. if (overflow) RETURN_DOUBLE((double)l1 * pow(dval,i));
  543. }
  544. if (i == 0) {
  545. RETURN_LONG(l1);
  546. }
  547. }
  548. }
  549. convert_to_double(zbase);
  550. convert_to_double(zexp);
  551. RETURN_DOUBLE(pow(Z_DVAL_P(zbase), Z_DVAL_P(zexp)));
  552. }
  553. /* }}} */
  554. /* {{{ proto float exp(float number)
  555. Returns e raised to the power of the number */
  556. PHP_FUNCTION(exp)
  557. {
  558. double num;
  559. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  560. return;
  561. }
  562. RETURN_DOUBLE(exp(num));
  563. }
  564. /* }}} */
  565. /* {{{ proto float expm1(float number)
  566. Returns exp(number) - 1, computed in a way that accurate even when the value of number is close to zero */
  567. /*
  568. WARNING: this function is expermental: it could change its name or
  569. disappear in the next version of PHP!
  570. */
  571. PHP_FUNCTION(expm1)
  572. {
  573. double num;
  574. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  575. return;
  576. }
  577. RETURN_DOUBLE(php_expm1(num));
  578. }
  579. /* }}} */
  580. /* {{{ proto float log1p(float number)
  581. Returns log(1 + number), computed in a way that accurate even when the value of number is close to zero */
  582. /*
  583. WARNING: this function is expermental: it could change its name or
  584. disappear in the next version of PHP!
  585. */
  586. PHP_FUNCTION(log1p)
  587. {
  588. double num;
  589. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  590. return;
  591. }
  592. RETURN_DOUBLE(php_log1p(num));
  593. }
  594. /* }}} */
  595. /* {{{ proto float log(float number, [float base])
  596. Returns the natural logarithm of the number, or the base log if base is specified */
  597. PHP_FUNCTION(log)
  598. {
  599. double num, base = 0;
  600. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|d", &num, &base) == FAILURE) {
  601. return;
  602. }
  603. if (ZEND_NUM_ARGS() == 1) {
  604. RETURN_DOUBLE(log(num));
  605. }
  606. if (base <= 0.0) {
  607. php_error_docref(NULL TSRMLS_CC, E_WARNING, "base must be greater than 0");
  608. RETURN_FALSE;
  609. }
  610. if (base == 1) {
  611. RETURN_DOUBLE(php_get_nan());
  612. } else {
  613. RETURN_DOUBLE(log(num) / log(base));
  614. }
  615. }
  616. /* }}} */
  617. /* {{{ proto float log10(float number)
  618. Returns the base-10 logarithm of the number */
  619. PHP_FUNCTION(log10)
  620. {
  621. double num;
  622. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  623. return;
  624. }
  625. RETURN_DOUBLE(log10(num));
  626. }
  627. /* }}} */
  628. /* {{{ proto float sqrt(float number)
  629. Returns the square root of the number */
  630. PHP_FUNCTION(sqrt)
  631. {
  632. double num;
  633. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
  634. return;
  635. }
  636. RETURN_DOUBLE(sqrt(num));
  637. }
  638. /* }}} */
  639. /* {{{ proto float hypot(float num1, float num2)
  640. Returns sqrt(num1*num1 + num2*num2) */
  641. PHP_FUNCTION(hypot)
  642. {
  643. double num1, num2;
  644. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd", &num1, &num2) == FAILURE) {
  645. return;
  646. }
  647. #if HAVE_HYPOT
  648. RETURN_DOUBLE(hypot(num1, num2));
  649. #elif defined(_MSC_VER)
  650. RETURN_DOUBLE(_hypot(num1, num2));
  651. #else
  652. RETURN_DOUBLE(sqrt((num1 * num1) + (num2 * num2)));
  653. #endif
  654. }
  655. /* }}} */
  656. /* {{{ proto float deg2rad(float number)
  657. Converts the number in degrees to the radian equivalent */
  658. PHP_FUNCTION(deg2rad)
  659. {
  660. double deg;
  661. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &deg) == FAILURE) {
  662. return;
  663. }
  664. RETURN_DOUBLE((deg / 180.0) * M_PI);
  665. }
  666. /* }}} */
  667. /* {{{ proto float rad2deg(float number)
  668. Converts the radian number to the equivalent number in degrees */
  669. PHP_FUNCTION(rad2deg)
  670. {
  671. double rad;
  672. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &rad) == FAILURE) {
  673. return;
  674. }
  675. RETURN_DOUBLE((rad / M_PI) * 180);
  676. }
  677. /* }}} */
  678. /* {{{ _php_math_basetolong */
  679. /*
  680. * Convert a string representation of a base(2-36) number to a long.
  681. */
  682. PHPAPI long _php_math_basetolong(zval *arg, int base)
  683. {
  684. long num = 0, digit, onum;
  685. int i;
  686. char c, *s;
  687. if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) {
  688. return 0;
  689. }
  690. s = Z_STRVAL_P(arg);
  691. for (i = Z_STRLEN_P(arg); i > 0; i--) {
  692. c = *s++;
  693. digit = (c >= '0' && c <= '9') ? c - '0'
  694. : (c >= 'A' && c <= 'Z') ? c - 'A' + 10
  695. : (c >= 'a' && c <= 'z') ? c - 'a' + 10
  696. : base;
  697. if (digit >= base) {
  698. continue;
  699. }
  700. onum = num;
  701. num = num * base + digit;
  702. if (num > onum)
  703. continue;
  704. {
  705. TSRMLS_FETCH();
  706. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number '%s' is too big to fit in long", s);
  707. return LONG_MAX;
  708. }
  709. }
  710. return num;
  711. }
  712. /* }}} */
  713. /* {{{ _php_math_basetozval */
  714. /*
  715. * Convert a string representation of a base(2-36) number to a zval.
  716. */
  717. PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
  718. {
  719. long num = 0;
  720. double fnum = 0;
  721. int i;
  722. int mode = 0;
  723. char c, *s;
  724. long cutoff;
  725. int cutlim;
  726. if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) {
  727. return FAILURE;
  728. }
  729. s = Z_STRVAL_P(arg);
  730. cutoff = LONG_MAX / base;
  731. cutlim = LONG_MAX % base;
  732. for (i = Z_STRLEN_P(arg); i > 0; i--) {
  733. c = *s++;
  734. /* might not work for EBCDIC */
  735. if (c >= '0' && c <= '9')
  736. c -= '0';
  737. else if (c >= 'A' && c <= 'Z')
  738. c -= 'A' - 10;
  739. else if (c >= 'a' && c <= 'z')
  740. c -= 'a' - 10;
  741. else
  742. continue;
  743. if (c >= base)
  744. continue;
  745. switch (mode) {
  746. case 0: /* Integer */
  747. if (num < cutoff || (num == cutoff && c <= cutlim)) {
  748. num = num * base + c;
  749. break;
  750. } else {
  751. fnum = num;
  752. mode = 1;
  753. }
  754. /* fall-through */
  755. case 1: /* Float */
  756. fnum = fnum * base + c;
  757. }
  758. }
  759. if (mode == 1) {
  760. ZVAL_DOUBLE(ret, fnum);
  761. } else {
  762. ZVAL_LONG(ret, num);
  763. }
  764. return SUCCESS;
  765. }
  766. /* }}} */
  767. /* {{{ _php_math_longtobase */
  768. /*
  769. * Convert a long to a string containing a base(2-36) representation of
  770. * the number.
  771. */
  772. PHPAPI char * _php_math_longtobase(zval *arg, int base)
  773. {
  774. static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  775. char buf[(sizeof(unsigned long) << 3) + 1];
  776. char *ptr, *end;
  777. unsigned long value;
  778. if (Z_TYPE_P(arg) != IS_LONG || base < 2 || base > 36) {
  779. return STR_EMPTY_ALLOC();
  780. }
  781. value = Z_LVAL_P(arg);
  782. end = ptr = buf + sizeof(buf) - 1;
  783. *ptr = '\0';
  784. do {
  785. *--ptr = digits[value % base];
  786. value /= base;
  787. } while (ptr > buf && value);
  788. return estrndup(ptr, end - ptr);
  789. }
  790. /* }}} */
  791. /* {{{ _php_math_zvaltobase */
  792. /*
  793. * Convert a zval to a string containing a base(2-36) representation of
  794. * the number.
  795. */
  796. PHPAPI char * _php_math_zvaltobase(zval *arg, int base TSRMLS_DC)
  797. {
  798. static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
  799. if ((Z_TYPE_P(arg) != IS_LONG && Z_TYPE_P(arg) != IS_DOUBLE) || base < 2 || base > 36) {
  800. return STR_EMPTY_ALLOC();
  801. }
  802. if (Z_TYPE_P(arg) == IS_DOUBLE) {
  803. double fvalue = floor(Z_DVAL_P(arg)); /* floor it just in case */
  804. char *ptr, *end;
  805. char buf[(sizeof(double) << 3) + 1];
  806. /* Don't try to convert +/- infinity */
  807. if (fvalue == HUGE_VAL || fvalue == -HUGE_VAL) {
  808. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number too large");
  809. return STR_EMPTY_ALLOC();
  810. }
  811. end = ptr = buf + sizeof(buf) - 1;
  812. *ptr = '\0';
  813. do {
  814. *--ptr = digits[(int) fmod(fvalue, base)];
  815. fvalue /= base;
  816. } while (ptr > buf && fabs(fvalue) >= 1);
  817. return estrndup(ptr, end - ptr);
  818. }
  819. return _php_math_longtobase(arg, base);
  820. }
  821. /* }}} */
  822. /* {{{ proto int bindec(string binary_number)
  823. Returns the decimal equivalent of the binary number */
  824. PHP_FUNCTION(bindec)
  825. {
  826. zval **arg;
  827. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
  828. return;
  829. }
  830. convert_to_string_ex(arg);
  831. if (_php_math_basetozval(*arg, 2, return_value) == FAILURE) {
  832. RETURN_FALSE;
  833. }
  834. }
  835. /* }}} */
  836. /* {{{ proto int hexdec(string hexadecimal_number)
  837. Returns the decimal equivalent of the hexadecimal number */
  838. PHP_FUNCTION(hexdec)
  839. {
  840. zval **arg;
  841. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
  842. return;
  843. }
  844. convert_to_string_ex(arg);
  845. if (_php_math_basetozval(*arg, 16, return_value) == FAILURE) {
  846. RETURN_FALSE;
  847. }
  848. }
  849. /* }}} */
  850. /* {{{ proto int octdec(string octal_number)
  851. Returns the decimal equivalent of an octal string */
  852. PHP_FUNCTION(octdec)
  853. {
  854. zval **arg;
  855. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
  856. return;
  857. }
  858. convert_to_string_ex(arg);
  859. if (_php_math_basetozval(*arg, 8, return_value) == FAILURE) {
  860. RETURN_FALSE;
  861. }
  862. }
  863. /* }}} */
  864. /* {{{ proto string decbin(int decimal_number)
  865. Returns a string containing a binary representation of the number */
  866. PHP_FUNCTION(decbin)
  867. {
  868. zval **arg;
  869. char *result;
  870. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
  871. return;
  872. }
  873. convert_to_long_ex(arg);
  874. result = _php_math_longtobase(*arg, 2);
  875. RETURN_STRING(result, 0);
  876. }
  877. /* }}} */
  878. /* {{{ proto string decoct(int decimal_number)
  879. Returns a string containing an octal representation of the given number */
  880. PHP_FUNCTION(decoct)
  881. {
  882. zval **arg;
  883. char *result;
  884. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
  885. return;
  886. }
  887. convert_to_long_ex(arg);
  888. result = _php_math_longtobase(*arg, 8);
  889. RETURN_STRING(result, 0);
  890. }
  891. /* }}} */
  892. /* {{{ proto string dechex(int decimal_number)
  893. Returns a string containing a hexadecimal representation of the given number */
  894. PHP_FUNCTION(dechex)
  895. {
  896. zval **arg;
  897. char *result;
  898. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
  899. return;
  900. }
  901. convert_to_long_ex(arg);
  902. result = _php_math_longtobase(*arg, 16);
  903. RETURN_STRING(result, 0);
  904. }
  905. /* }}} */
  906. /* {{{ proto string base_convert(string number, int frombase, int tobase)
  907. Converts a number in a string from any base <= 36 to any base <= 36 */
  908. PHP_FUNCTION(base_convert)
  909. {
  910. zval **number, temp;
  911. long frombase, tobase;
  912. char *result;
  913. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zll", &number, &frombase, &tobase) == FAILURE) {
  914. return;
  915. }
  916. convert_to_string_ex(number);
  917. if (frombase < 2 || frombase > 36) {
  918. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid `from base' (%ld)", frombase);
  919. RETURN_FALSE;
  920. }
  921. if (tobase < 2 || tobase > 36) {
  922. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid `to base' (%ld)", tobase);
  923. RETURN_FALSE;
  924. }
  925. if(_php_math_basetozval(*number, frombase, &temp) == FAILURE) {
  926. RETURN_FALSE;
  927. }
  928. result = _php_math_zvaltobase(&temp, tobase TSRMLS_CC);
  929. RETVAL_STRING(result, 0);
  930. }
  931. /* }}} */
  932. /* {{{ _php_math_number_format
  933. */
  934. PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char thousand_sep)
  935. {
  936. char *tmpbuf = NULL, *resbuf;
  937. char *s, *t; /* source, target */
  938. char *dp;
  939. int integral;
  940. int tmplen, reslen=0;
  941. int count=0;
  942. int is_negative=0;
  943. if (d < 0) {
  944. is_negative = 1;
  945. d = -d;
  946. }
  947. dec = MAX(0, dec);
  948. d = _php_math_round(d, dec, PHP_ROUND_HALF_UP);
  949. tmplen = spprintf(&tmpbuf, 0, "%.*F", dec, d);
  950. if (tmpbuf == NULL || !isdigit((int)tmpbuf[0])) {
  951. return tmpbuf;
  952. }
  953. /* find decimal point, if expected */
  954. if (dec) {
  955. dp = strpbrk(tmpbuf, ".,");
  956. } else {
  957. dp = NULL;
  958. }
  959. /* calculate the length of the return buffer */
  960. if (dp) {
  961. integral = dp - tmpbuf;
  962. } else {
  963. /* no decimal point was found */
  964. integral = tmplen;
  965. }
  966. /* allow for thousand separators */
  967. if (thousand_sep) {
  968. integral += (integral-1) / 3;
  969. }
  970. reslen = integral;
  971. if (dec) {
  972. reslen += dec;
  973. if (dec_point) {
  974. reslen++;
  975. }
  976. }
  977. /* add a byte for minus sign */
  978. if (is_negative) {
  979. reslen++;
  980. }
  981. resbuf = (char *) emalloc(reslen+1); /* +1 for NUL terminator */
  982. s = tmpbuf+tmplen-1;
  983. t = resbuf+reslen;
  984. *t-- = '\0';
  985. /* copy the decimal places.
  986. * Take care, as the sprintf implementation may return less places than
  987. * we requested due to internal buffer limitations */
  988. if (dec) {
  989. int declen = dp ? s - dp : 0;
  990. int topad = dec > declen ? dec - declen : 0;
  991. /* pad with '0's */
  992. while (topad--) {
  993. *t-- = '0';
  994. }
  995. if (dp) {
  996. s -= declen + 1; /* +1 to skip the point */
  997. t -= declen;
  998. /* now copy the chars after the point */
  999. memcpy(t + 1, dp + 1, declen);
  1000. }
  1001. /* add decimal point */
  1002. if (dec_point) {
  1003. *t-- = dec_point;
  1004. }
  1005. }
  1006. /* copy the numbers before the decimal point, adding thousand
  1007. * separator every three digits */
  1008. while(s >= tmpbuf) {
  1009. *t-- = *s--;
  1010. if (thousand_sep && (++count%3)==0 && s>=tmpbuf) {
  1011. *t-- = thousand_sep;
  1012. }
  1013. }
  1014. /* and a minus sign, if needed */
  1015. if (is_negative) {
  1016. *t-- = '-';
  1017. }
  1018. efree(tmpbuf);
  1019. return resbuf;
  1020. }
  1021. /* }}} */
  1022. /* {{{ proto string number_format(float number [, int num_decimal_places [, string dec_seperator, string thousands_seperator]])
  1023. Formats a number with grouped thousands */
  1024. PHP_FUNCTION(number_format)
  1025. {
  1026. double num;
  1027. long dec = 0;
  1028. char *thousand_sep = NULL, *dec_point = NULL;
  1029. char thousand_sep_chr = ',', dec_point_chr = '.';
  1030. int thousand_sep_len = 0, dec_point_len = 0;
  1031. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|ls!s!", &num, &dec, &dec_point, &dec_point_len, &thousand_sep, &thousand_sep_len) == FAILURE) {
  1032. return;
  1033. }
  1034. switch(ZEND_NUM_ARGS()) {
  1035. case 1:
  1036. RETURN_STRING(_php_math_number_format(num, 0, dec_point_chr, thousand_sep_chr), 0);
  1037. break;
  1038. case 2:
  1039. RETURN_STRING(_php_math_number_format(num, dec, dec_point_chr, thousand_sep_chr), 0);
  1040. break;
  1041. case 4:
  1042. if (dec_point != NULL) {
  1043. if (dec_point_len) {
  1044. dec_point_chr = dec_point[0];
  1045. } else {
  1046. dec_point_chr = 0;
  1047. }
  1048. }
  1049. if (thousand_sep != NULL) {
  1050. if (thousand_sep_len) {
  1051. thousand_sep_chr = thousand_sep[0];
  1052. } else {
  1053. thousand_sep_chr = 0;
  1054. }
  1055. }
  1056. RETURN_STRING(_php_math_number_format(num, dec, dec_point_chr, thousand_sep_chr), 0);
  1057. break;
  1058. default:
  1059. WRONG_PARAM_COUNT;
  1060. break;
  1061. }
  1062. }
  1063. /* }}} */
  1064. /* {{{ proto float fmod(float x, float y)
  1065. Returns the remainder of dividing x by y as a float */
  1066. PHP_FUNCTION(fmod)
  1067. {
  1068. double num1, num2;
  1069. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd", &num1, &num2) == FAILURE) {
  1070. return;
  1071. }
  1072. RETURN_DOUBLE(fmod(num1, num2));
  1073. }
  1074. /* }}} */
  1075. /* {{{ proto int xround(int n)
  1076. Round to the next power of 10 */
  1077. PHP_FUNCTION(xround)
  1078. {
  1079. double d;
  1080. long n, x = 1;
  1081. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &d) == FAILURE) {
  1082. return;
  1083. }
  1084. n = (long) d;
  1085. n = n + (n < d) - (d < n);
  1086. if (n < 0) {
  1087. n = -n;
  1088. x = -1;
  1089. }
  1090. if (n > 100000) {
  1091. if (n > 10000000) {
  1092. if (n <= 100000000) RETURN_LONG(x * 100000000);
  1093. RETURN_LONG(x * 1000000000);
  1094. } else {
  1095. if (n <= 1000000) RETURN_LONG(x * 1000000);
  1096. RETURN_LONG(x * 10000000);
  1097. }
  1098. } else {
  1099. if (n > 1000) {
  1100. if (n <= 10000) RETURN_LONG(x * 10000);
  1101. RETURN_LONG(x * 100000);
  1102. } else {
  1103. if (n <= 10) {
  1104. if (n <= 1) RETURN_LONG(x);
  1105. RETURN_LONG(x * 10);
  1106. }
  1107. if (n <= 100) RETURN_LONG(x * 100);
  1108. RETURN_LONG(x * 1000);
  1109. }
  1110. }
  1111. }
  1112. /* }}} */
  1113. /* {{{ proto int sgn(mixed n)
  1114. Returns the sign of a number */
  1115. PHP_FUNCTION(sgn)
  1116. {
  1117. zval *x, p, n, null;
  1118. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &x) == FAILURE) {
  1119. return;
  1120. }
  1121. Z_TYPE(null) = IS_LONG;
  1122. Z_LVAL(null) = 0;
  1123. is_smaller_function(&p, &null, x TSRMLS_CC);
  1124. is_smaller_function(&n, x, &null TSRMLS_CC);
  1125. RETURN_LONG(Z_LVAL(p) - Z_LVAL(n));
  1126. }
  1127. /* }}} */
  1128. /* {{{ proto int sigfig(double n, int figs)
  1129. Calculates the significant figures of a number */
  1130. PHP_FUNCTION(sigfig)
  1131. {
  1132. long figs;
  1133. double value, pow_exp, pow_sig;
  1134. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dl", &value, &figs) == FAILURE) {
  1135. return;
  1136. }
  1137. if (figs < 0 || figs > 10) RETURN_FALSE;
  1138. pow_exp = php_intpow10((int) log10(value) + 1);
  1139. pow_sig = php_intpow10(figs);
  1140. RETURN_DOUBLE((int) ((value / pow_exp) * pow_sig + 0.5) / pow_sig * pow_exp);
  1141. }
  1142. /* }}} */
  1143. /* {{{ proto int bround(int n, int b)
  1144. Round to the next multiple of a base */
  1145. PHP_FUNCTION(bround)
  1146. {
  1147. double n, b;
  1148. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd", &n, &b) == FAILURE) {
  1149. return;
  1150. }
  1151. if (0 == b) {
  1152. RETURN_FALSE;
  1153. }
  1154. RETURN_DOUBLE(ceil(n / b) * b);
  1155. }
  1156. /* }}} */
  1157. /* {{{ proto int bound(mixed n, mixed min, mixed max)
  1158. Limits a number to a specified lower min and a upper max value */
  1159. PHP_FUNCTION(bound)
  1160. {
  1161. zval *num, *min, *max, result;
  1162. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zz|z", &num, &min, &max) == FAILURE) {
  1163. return;
  1164. }
  1165. if (2 == ZEND_NUM_ARGS()) {
  1166. // max < num -> max
  1167. if (Z_TYPE_P(min) != IS_NULL) {
  1168. is_smaller_function(&result, min, num TSRMLS_CC);
  1169. if (Z_LVAL(result)) {
  1170. RETURN_ZVAL(min, 1, 0);
  1171. }
  1172. }
  1173. RETURN_ZVAL(num, 1, 0);
  1174. }
  1175. // max < min -> min
  1176. if (min != NULL && Z_TYPE_P(min) != IS_NULL && max != NULL && Z_TYPE_P(max) != IS_NULL) {
  1177. is_smaller_function(&result, max, min TSRMLS_CC);
  1178. if (Z_LVAL(result)) {
  1179. RETURN_ZVAL(min, 1, 0);
  1180. }
  1181. }
  1182. // num < min -> min
  1183. if (min !=NULL && Z_TYPE_P(min) != IS_NULL) {
  1184. is_smaller_function(&result, num, min TSRMLS_CC);
  1185. if (Z_LVAL(result)) {
  1186. RETURN_ZVAL(min, 1, 0);
  1187. }
  1188. }
  1189. // max < num -> max
  1190. if (max != NULL && Z_TYPE_P(max) != IS_NULL) {
  1191. is_smaller_function(&result, max, num TSRMLS_CC);
  1192. if (Z_LVAL(result)) {
  1193. RETURN_ZVAL(max, 1, 0);
  1194. }
  1195. }
  1196. // -> num
  1197. RETURN_ZVAL(num, 1, 0);
  1198. }
  1199. /* }}} */
  1200. /* {{{ proto int gpp(int n)
  1201. Returns the greatest proper power of an integer */
  1202. /*
  1203. WARNING: this function is expermental: it could change its name or
  1204. disappear in the next version of PHP!
  1205. */
  1206. PHP_FUNCTION(gpp)
  1207. {
  1208. long x;
  1209. register long f = 1, n = 1, nn;
  1210. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &x) == FAILURE) {
  1211. return;
  1212. }
  1213. if (x <= 0) {
  1214. RETURN_LONG(1);
  1215. }
  1216. for (;; n++) {
  1217. if ((nn = n * n) >= x) {
  1218. break;
  1219. }
  1220. if (0 == (x % nn)) {
  1221. f = n;
  1222. }
  1223. }
  1224. RETURN_LONG(f);
  1225. }
  1226. /* }}} */
  1227. /*
  1228. * Local variables:
  1229. * tab-width: 4
  1230. * c-basic-offset: 4
  1231. * End:
  1232. * vim600: fdm=marker
  1233. * vim: noet sw=4 ts=4
  1234. */