1999-04-17 08:37:12 +08:00
|
|
|
|
/*
|
|
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
|
| PHP Version 5 |
|
1999-04-17 08:37:12 +08:00
|
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
|
| Copyright (c) 1997-2006 The PHP Group |
|
1999-04-17 08:37:12 +08:00
|
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
1999-10-14 03:43:36 +08:00
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:51:34 +08:00
|
|
|
|
| http://www.php.net/license/3_01.txt |
|
1999-10-14 03:43:36 +08:00
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
1999-04-17 08:37:12 +08:00
|
|
|
|
+----------------------------------------------------------------------+
|
2002-02-28 16:29:35 +08:00
|
|
|
|
| Authors: Jim Winstead <jimw@php.net> |
|
2003-03-18 20:06:09 +08:00
|
|
|
|
| Stig S<EFBFBD>ther Bakken <ssb@php.net> |
|
1999-08-01 00:20:06 +08:00
|
|
|
|
| Zeev Suraski <zeev@zend.com> |
|
2002-02-28 16:29:35 +08:00
|
|
|
|
| PHP 4.0 patches by Thies C. Arntzen <thies@thieso.net> |
|
1999-04-17 08:37:12 +08:00
|
|
|
|
+----------------------------------------------------------------------+
|
1999-10-14 03:43:36 +08:00
|
|
|
|
*/
|
|
|
|
|
|
2000-07-24 09:40:02 +08:00
|
|
|
|
/* $Id$ */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
|
|
#include "php.h"
|
2000-06-14 00:31:57 +08:00
|
|
|
|
#include "php_math.h"
|
2004-03-10 23:57:49 +08:00
|
|
|
|
#include "zend_multiply.h"
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
|
|
#include <math.h>
|
2000-04-15 21:53:32 +08:00
|
|
|
|
#include <float.h>
|
2002-10-15 22:51:01 +08:00
|
|
|
|
#include <stdlib.h>
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
|
|
#ifndef M_PI
|
|
|
|
|
#define M_PI 3.14159265358979323846
|
|
|
|
|
#endif
|
|
|
|
|
|
2003-08-09 09:12:41 +08:00
|
|
|
|
#ifndef PHP_ROUND_FUZZ
|
|
|
|
|
# ifndef PHP_WIN32
|
|
|
|
|
# define PHP_ROUND_FUZZ 0.50000000001
|
|
|
|
|
# else
|
|
|
|
|
# define PHP_ROUND_FUZZ 0.5
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
2003-08-09 07:40:44 +08:00
|
|
|
|
|
2003-11-18 10:50:41 +08:00
|
|
|
|
#define PHP_ROUND_WITH_FUZZ(val, places) { \
|
|
|
|
|
double tmp_val=val, f = pow(10.0, (double) places); \
|
|
|
|
|
tmp_val *= f; \
|
|
|
|
|
if (tmp_val >= 0.0) { \
|
|
|
|
|
tmp_val = floor(tmp_val + PHP_ROUND_FUZZ); \
|
|
|
|
|
} else { \
|
|
|
|
|
tmp_val = ceil(tmp_val - PHP_ROUND_FUZZ); \
|
|
|
|
|
} \
|
|
|
|
|
tmp_val /= f; \
|
|
|
|
|
val = !zend_isnan(tmp_val) ? tmp_val : val; \
|
|
|
|
|
} \
|
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
/* {{{ proto int abs(int number)
|
|
|
|
|
Return the absolute value of the number */
|
1999-10-14 03:43:36 +08:00
|
|
|
|
PHP_FUNCTION(abs)
|
|
|
|
|
{
|
|
|
|
|
zval **value;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2005-05-08 00:08:03 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &value) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-15 14:31:40 +08:00
|
|
|
|
convert_scalar_to_number_ex(value);
|
|
|
|
|
|
2001-08-01 21:48:14 +08:00
|
|
|
|
if (Z_TYPE_PP(value) == IS_DOUBLE) {
|
|
|
|
|
RETURN_DOUBLE(fabs(Z_DVAL_PP(value)));
|
|
|
|
|
} else if (Z_TYPE_PP(value) == IS_LONG) {
|
2001-08-06 04:27:03 +08:00
|
|
|
|
if (Z_LVAL_PP(value) == LONG_MIN) {
|
|
|
|
|
RETURN_DOUBLE(-(double)LONG_MIN);
|
|
|
|
|
} else {
|
|
|
|
|
RETURN_LONG(Z_LVAL_PP(value) < 0 ? -Z_LVAL_PP(value) : Z_LVAL_PP(value));
|
|
|
|
|
}
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float ceil(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the next highest integer value of the number */
|
1999-10-14 03:43:36 +08:00
|
|
|
|
PHP_FUNCTION(ceil)
|
|
|
|
|
{
|
|
|
|
|
zval **value;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2005-05-08 00:08:03 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &value) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-15 14:31:40 +08:00
|
|
|
|
convert_scalar_to_number_ex(value);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2001-08-01 21:48:14 +08:00
|
|
|
|
if (Z_TYPE_PP(value) == IS_DOUBLE) {
|
|
|
|
|
RETURN_DOUBLE(ceil(Z_DVAL_PP(value)));
|
|
|
|
|
} else if (Z_TYPE_PP(value) == IS_LONG) {
|
2001-12-22 19:53:29 +08:00
|
|
|
|
convert_to_double_ex(value);
|
|
|
|
|
RETURN_DOUBLE(Z_DVAL_PP(value));
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float floor(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the next lowest integer value from the number */
|
2001-08-05 09:43:02 +08:00
|
|
|
|
PHP_FUNCTION(floor)
|
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **value;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2005-05-08 00:08:03 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &value) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-15 14:31:40 +08:00
|
|
|
|
convert_scalar_to_number_ex(value);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2001-08-01 21:48:14 +08:00
|
|
|
|
if (Z_TYPE_PP(value) == IS_DOUBLE) {
|
|
|
|
|
RETURN_DOUBLE(floor(Z_DVAL_PP(value)));
|
|
|
|
|
} else if (Z_TYPE_PP(value) == IS_LONG) {
|
2001-12-22 00:38:49 +08:00
|
|
|
|
convert_to_double_ex(value);
|
|
|
|
|
RETURN_DOUBLE(Z_DVAL_PP(value));
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float round(float number [, int precision])
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns the number rounded to specified precision */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(round)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
2000-05-17 20:50:04 +08:00
|
|
|
|
zval **value, **precision;
|
|
|
|
|
int places = 0;
|
2003-11-18 10:50:41 +08:00
|
|
|
|
double return_val;
|
2000-05-17 20:50:04 +08:00
|
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 2 ||
|
|
|
|
|
zend_get_parameters_ex(ZEND_NUM_ARGS(), &value, &precision) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-15 14:31:40 +08:00
|
|
|
|
|
2000-05-17 20:50:04 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() == 2) {
|
|
|
|
|
convert_to_long_ex(precision);
|
|
|
|
|
places = (int) Z_LVAL_PP(precision);
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-15 14:31:40 +08:00
|
|
|
|
convert_scalar_to_number_ex(value);
|
|
|
|
|
|
2000-05-17 20:50:04 +08:00
|
|
|
|
switch (Z_TYPE_PP(value)) {
|
|
|
|
|
case IS_LONG:
|
|
|
|
|
/* Simple case - long that doesn't need to be rounded. */
|
|
|
|
|
if (places >= 0) {
|
|
|
|
|
RETURN_DOUBLE((double) Z_LVAL_PP(value));
|
|
|
|
|
}
|
|
|
|
|
/* break omitted intentionally */
|
|
|
|
|
|
|
|
|
|
case IS_DOUBLE:
|
2000-05-17 20:51:37 +08:00
|
|
|
|
return_val = (Z_TYPE_PP(value) == IS_LONG) ?
|
|
|
|
|
(double)Z_LVAL_PP(value) : Z_DVAL_PP(value);
|
2000-05-17 20:50:04 +08:00
|
|
|
|
|
2003-11-18 10:50:41 +08:00
|
|
|
|
PHP_ROUND_WITH_FUZZ(return_val, places);
|
2000-05-17 20:50:04 +08:00
|
|
|
|
|
|
|
|
|
RETURN_DOUBLE(return_val);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
break;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float sin(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the sine of the number in radians */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(sin)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **num;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(num);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_DVAL_P(return_value) = sin(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float cos(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the cosine of the number in radians */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(cos)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **num;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(num);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_DVAL_P(return_value) = cos(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float tan(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the tangent of the number in radians */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(tan)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **num;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(num);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_DVAL_P(return_value) = tan(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float asin(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the arc sine of the number in radians */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(asin)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **num;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(num);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_DVAL_P(return_value) = asin(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float acos(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Return the arc cosine of the number in radians */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(acos)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **num;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(num);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_DVAL_P(return_value) = acos(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float atan(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the arc tangent of the number in radians */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(atan)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **num;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(num);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_DVAL_P(return_value) = atan(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float atan2(float y, float x)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the arc tangent of y/x, with the resulting quadrant determined by the signs of y and x */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(atan2)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **num1, **num2;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &num1, &num2) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(num1);
|
|
|
|
|
convert_to_double_ex(num2);
|
2001-08-12 01:03:37 +08:00
|
|
|
|
Z_DVAL_P(return_value) = atan2(Z_DVAL_PP(num1), Z_DVAL_PP(num2));
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float sinh(float number)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns the hyperbolic sine of the number, defined as (exp(number) - exp(-number))/2 */
|
2001-08-01 21:48:14 +08:00
|
|
|
|
PHP_FUNCTION(sinh)
|
|
|
|
|
{
|
|
|
|
|
zval **num;
|
|
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
Z_DVAL_P(return_value) = sinh(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float cosh(float number)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns the hyperbolic cosine of the number, defined as (exp(number) + exp(-number))/2 */
|
2001-08-01 21:48:14 +08:00
|
|
|
|
PHP_FUNCTION(cosh)
|
|
|
|
|
{
|
|
|
|
|
zval **num;
|
|
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
Z_DVAL_P(return_value) = cosh(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float tanh(float number)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns the hyperbolic tangent of the number, defined as sinh(number)/cosh(number) */
|
2001-08-01 21:48:14 +08:00
|
|
|
|
PHP_FUNCTION(tanh)
|
|
|
|
|
{
|
|
|
|
|
zval **num;
|
|
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
Z_DVAL_P(return_value) = tanh(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2001-08-02 00:55:24 +08:00
|
|
|
|
|
2002-09-06 15:44:30 +08:00
|
|
|
|
#if !defined(PHP_WIN32) && !defined(NETWARE)
|
2002-10-25 02:31:11 +08:00
|
|
|
|
#ifdef HAVE_ASINH
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float asinh(float number)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns the inverse hyperbolic sine of the number, i.e. the value whose hyperbolic sine is number */
|
2001-08-01 21:48:14 +08:00
|
|
|
|
PHP_FUNCTION(asinh)
|
|
|
|
|
{
|
|
|
|
|
zval **num;
|
|
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
Z_DVAL_P(return_value) = asinh(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2002-10-25 02:31:11 +08:00
|
|
|
|
#endif /* HAVE_ASINH */
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_ACOSH
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float acosh(float number)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns the inverse hyperbolic cosine of the number, i.e. the value whose hyperbolic cosine is number */
|
2001-08-01 21:48:14 +08:00
|
|
|
|
PHP_FUNCTION(acosh)
|
|
|
|
|
{
|
|
|
|
|
zval **num;
|
|
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
Z_DVAL_P(return_value) = acosh(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2002-10-25 02:31:11 +08:00
|
|
|
|
#endif /* HAVE_ACOSH */
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_ATANH
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float atanh(float number)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns the inverse hyperbolic tangent of the number, i.e. the value whose hyperbolic tangent is number */
|
2001-08-01 21:48:14 +08:00
|
|
|
|
PHP_FUNCTION(atanh)
|
|
|
|
|
{
|
|
|
|
|
zval **num;
|
|
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
Z_DVAL_P(return_value) = atanh(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2002-10-25 02:31:11 +08:00
|
|
|
|
#endif /* HAVE_ATANH */
|
|
|
|
|
#endif /* !defined(PHP_WIN32) && !defined(NETWARE) */
|
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float pi(void)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns an approximation of pi */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(pi)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_DVAL_P(return_value) = M_PI;
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2001-08-04 06:19:11 +08:00
|
|
|
|
|
2002-03-03 01:08:09 +08:00
|
|
|
|
/* {{{ proto bool is_finite(float val)
|
|
|
|
|
Returns whether argument is finite */
|
2002-01-10 07:59:05 +08:00
|
|
|
|
PHP_FUNCTION(is_finite)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
2001-08-04 21:11:17 +08:00
|
|
|
|
double dval;
|
2002-01-05 11:45:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dval) == FAILURE) {
|
|
|
|
|
return;
|
2001-08-04 06:19:11 +08:00
|
|
|
|
}
|
2002-01-27 15:41:20 +08:00
|
|
|
|
RETURN_BOOL(zend_finite(dval));
|
2002-01-05 11:45:11 +08:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2002-03-03 01:08:09 +08:00
|
|
|
|
/* {{{ proto bool is_infinite(float val)
|
|
|
|
|
Returns whether argument is infinite */
|
2002-01-10 07:59:05 +08:00
|
|
|
|
PHP_FUNCTION(is_infinite)
|
2002-01-05 11:45:11 +08:00
|
|
|
|
{
|
|
|
|
|
double dval;
|
|
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dval) == FAILURE) {
|
|
|
|
|
return;
|
2001-08-04 06:19:11 +08:00
|
|
|
|
}
|
2002-01-27 15:41:20 +08:00
|
|
|
|
RETURN_BOOL(zend_isinf(dval));
|
2002-01-05 11:45:11 +08:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2001-08-04 06:19:11 +08:00
|
|
|
|
|
2002-03-03 01:08:09 +08:00
|
|
|
|
/* {{{ proto bool is_nan(float val)
|
|
|
|
|
Returns whether argument is not a number */
|
2002-01-10 07:59:05 +08:00
|
|
|
|
PHP_FUNCTION(is_nan)
|
2002-01-05 11:45:11 +08:00
|
|
|
|
{
|
|
|
|
|
double dval;
|
2001-08-04 06:19:11 +08:00
|
|
|
|
|
2002-01-05 11:45:11 +08:00
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dval) == FAILURE) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2002-01-27 15:41:20 +08:00
|
|
|
|
RETURN_BOOL(zend_isnan(dval));
|
2002-01-05 11:45:11 +08:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2001-08-04 06:19:11 +08:00
|
|
|
|
|
2002-01-05 11:45:11 +08:00
|
|
|
|
/* {{{ proto number pow(number base, number exponent)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns base raised to the power of exponent. Returns integer result when possible */
|
2002-01-05 11:45:11 +08:00
|
|
|
|
PHP_FUNCTION(pow)
|
|
|
|
|
{
|
|
|
|
|
zval *zbase, *zexp;
|
2001-08-04 06:19:11 +08:00
|
|
|
|
|
2002-01-05 11:56:38 +08:00
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/z/", &zbase, &zexp) == FAILURE) {
|
2002-01-05 11:45:11 +08:00
|
|
|
|
return;
|
2001-08-04 06:19:11 +08:00
|
|
|
|
}
|
2002-01-05 11:45:11 +08:00
|
|
|
|
|
2002-03-11 07:46:43 +08:00
|
|
|
|
/* make sure we're dealing with numbers */
|
2002-03-11 13:43:23 +08:00
|
|
|
|
convert_scalar_to_number(zbase TSRMLS_CC);
|
|
|
|
|
convert_scalar_to_number(zexp TSRMLS_CC);
|
2001-08-04 06:19:11 +08:00
|
|
|
|
|
2002-03-11 07:46:43 +08:00
|
|
|
|
/* if both base and exponent were longs, we'll try to get a long out */
|
2004-03-10 23:57:49 +08:00
|
|
|
|
if (Z_TYPE_P(zbase) == IS_LONG && Z_TYPE_P(zexp) == IS_LONG && Z_LVAL_P(zexp) >= 0) {
|
|
|
|
|
long l1 = 1, l2 = Z_LVAL_P(zbase), i = Z_LVAL_P(zexp);
|
|
|
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
RETURN_LONG(1L);
|
|
|
|
|
} else if (l2 == 0) {
|
|
|
|
|
RETURN_LONG(0);
|
|
|
|
|
}
|
2001-08-04 06:19:11 +08:00
|
|
|
|
|
2004-03-10 23:57:49 +08:00
|
|
|
|
/* calculate pow(long,long) in O(log exp) operations, bail if overflow */
|
|
|
|
|
while (i >= 1) {
|
|
|
|
|
int overflow;
|
|
|
|
|
double dval;
|
|
|
|
|
|
|
|
|
|
if (i % 2) {
|
|
|
|
|
--i;
|
|
|
|
|
ZEND_SIGNED_MULTIPLY_LONG(l1,l2,l1,dval,overflow);
|
|
|
|
|
if (overflow) RETURN_DOUBLE(dval * pow(l2,i));
|
|
|
|
|
} else {
|
|
|
|
|
i /= 2;
|
|
|
|
|
ZEND_SIGNED_MULTIPLY_LONG(l2,l2,l2,dval,overflow);
|
|
|
|
|
if (overflow) RETURN_DOUBLE((double)l1 * pow(dval,i));
|
|
|
|
|
}
|
|
|
|
|
if (i == 0) {
|
|
|
|
|
RETURN_LONG(l1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-01-05 11:56:38 +08:00
|
|
|
|
convert_to_double(zbase);
|
|
|
|
|
convert_to_double(zexp);
|
2002-01-05 11:45:11 +08:00
|
|
|
|
|
2004-03-10 23:57:49 +08:00
|
|
|
|
RETURN_DOUBLE( pow(Z_DVAL_P(zbase),Z_DVAL_P(zexp)) );
|
2002-01-05 11:45:11 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2002-01-05 11:45:11 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float exp(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns e raised to the power of the number */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(exp)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **num;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(num);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_DVAL_P(return_value) = exp(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2002-09-06 15:44:30 +08:00
|
|
|
|
#if !defined(PHP_WIN32) && !defined(NETWARE)
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float expm1(float number)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns exp(number) - 1, computed in a way that accurate even when the value of number is close to zero */
|
|
|
|
|
/*
|
2001-08-16 03:08:59 +08:00
|
|
|
|
WARNING: this function is expermental: it could change its name or
|
|
|
|
|
disappear in the next version of PHP!
|
2002-01-05 15:54:49 +08:00
|
|
|
|
*/
|
2001-08-01 21:48:14 +08:00
|
|
|
|
PHP_FUNCTION(expm1)
|
|
|
|
|
{
|
|
|
|
|
zval **num;
|
|
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
Z_DVAL_P(return_value) = expm1(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LOG1P
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float log1p(float number)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns log(1 + number), computed in a way that accurate even when the value of number is close to zero */
|
|
|
|
|
/*
|
2001-08-16 03:08:59 +08:00
|
|
|
|
WARNING: this function is expermental: it could change its name or
|
|
|
|
|
disappear in the next version of PHP!
|
2002-01-05 15:54:49 +08:00
|
|
|
|
*/
|
2001-08-01 21:48:14 +08:00
|
|
|
|
PHP_FUNCTION(log1p)
|
|
|
|
|
{
|
|
|
|
|
zval **num;
|
|
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
Z_DVAL_P(return_value) = log1p(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
#endif /* HAVE_LOG1P */
|
|
|
|
|
#endif /* !defined(PHP_WIN32) && !defined(NETWARE) */
|
2001-08-02 00:55:24 +08:00
|
|
|
|
|
2002-11-11 13:21:35 +08:00
|
|
|
|
/* {{{ proto float log(float number, [float base])
|
|
|
|
|
Returns the natural logarithm of the number, or the base log if base is specified */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(log)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
2002-11-11 13:21:35 +08:00
|
|
|
|
zval **num, **base;
|
|
|
|
|
|
|
|
|
|
switch (ZEND_NUM_ARGS()) {
|
|
|
|
|
case 1:
|
|
|
|
|
if (zend_get_parameters_ex(1, &num) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
RETURN_DOUBLE(log(Z_DVAL_PP(num)));
|
|
|
|
|
case 2:
|
|
|
|
|
if (zend_get_parameters_ex(2, &num, &base) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
convert_to_double_ex(base);
|
|
|
|
|
|
|
|
|
|
if (Z_DVAL_PP(base) <= 0.0) {
|
2002-11-12 00:34:39 +08:00
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "base must be greater than 0");
|
2002-11-11 13:21:35 +08:00
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
|
|
|
|
RETURN_DOUBLE(log(Z_DVAL_PP(num)) / log(Z_DVAL_PP(base)));
|
|
|
|
|
default:
|
|
|
|
|
WRONG_PARAM_COUNT;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
2001-08-01 21:48:14 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float log10(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the base-10 logarithm of the number */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(log10)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **num;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(num);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_DVAL_P(return_value) = log10(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float sqrt(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the square root of the number */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(sqrt)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **num;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(num);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_DVAL_P(return_value) = sqrt(Z_DVAL_PP(num));
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
2001-08-02 00:55:24 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float hypot(float num1, float num2)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Returns sqrt(num1*num1 + num2*num2) */
|
2001-08-01 21:48:14 +08:00
|
|
|
|
PHP_FUNCTION(hypot)
|
|
|
|
|
{
|
|
|
|
|
zval **num1, **num2;
|
|
|
|
|
|
|
|
|
|
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &num1, &num2) == FAILURE) {
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num1);
|
|
|
|
|
convert_to_double_ex(num2);
|
2004-09-21 06:08:06 +08:00
|
|
|
|
#if HAVE_HYPOT
|
2001-09-26 17:21:58 +08:00
|
|
|
|
Z_DVAL_P(return_value) = hypot(Z_DVAL_PP(num1), Z_DVAL_PP(num2));
|
2004-09-21 06:08:06 +08:00
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
|
Z_DVAL_P(return_value) = _hypot(Z_DVAL_PP(num1), Z_DVAL_PP(num2));
|
|
|
|
|
#else
|
|
|
|
|
Z_DVAL_P(return_value) = sqrt((Z_DVAL_PP(num1) * Z_DVAL_PP(num1)) +
|
|
|
|
|
(Z_DVAL_PP(num2) * Z_DVAL_PP(num2)));
|
|
|
|
|
#endif
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2001-08-01 21:48:14 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float deg2rad(float number)
|
2000-02-24 16:39:02 +08:00
|
|
|
|
Converts the number in degrees to the radian equivalent */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(deg2rad)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **deg;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, °) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(deg);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
RETVAL_DOUBLE((Z_DVAL_PP(deg) / 180.0) * M_PI);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto float rad2deg(float number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Converts the radian number to the equivalent number in degrees */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(rad2deg)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **rad;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &rad) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_double_ex(rad);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
RETVAL_DOUBLE((Z_DVAL_PP(rad) / M_PI) * 180);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2005-05-08 00:08:03 +08:00
|
|
|
|
/* {{{ _php_math_basetolong */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
/*
|
|
|
|
|
* Convert a string representation of a base(2-36) number to a long.
|
|
|
|
|
*/
|
2005-05-08 00:08:03 +08:00
|
|
|
|
PHPAPI long _php_math_basetolong(zval *arg, int base)
|
|
|
|
|
{
|
2001-09-20 16:22:44 +08:00
|
|
|
|
long num = 0, digit, onum;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
int i;
|
|
|
|
|
char c, *s;
|
|
|
|
|
|
2001-08-01 21:48:14 +08:00
|
|
|
|
if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-08-01 21:48:14 +08:00
|
|
|
|
s = Z_STRVAL_P(arg);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2001-09-20 16:22:44 +08:00
|
|
|
|
for (i = Z_STRLEN_P(arg); i > 0; i--) {
|
|
|
|
|
c = *s++;
|
|
|
|
|
|
|
|
|
|
digit = (c >= '0' && c <= '9') ? c - '0'
|
|
|
|
|
: (c >= 'A' && c <= 'Z') ? c - 'A' + 10
|
|
|
|
|
: (c >= 'a' && c <= 'z') ? c - 'a' + 10
|
|
|
|
|
: base;
|
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
if (digit >= base) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2001-09-20 16:22:44 +08:00
|
|
|
|
|
|
|
|
|
onum = num;
|
|
|
|
|
num = num * base + digit;
|
|
|
|
|
if (num > onum)
|
|
|
|
|
continue;
|
|
|
|
|
|
2002-12-06 04:59:49 +08:00
|
|
|
|
{
|
|
|
|
|
TSRMLS_FETCH();
|
|
|
|
|
|
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number '%s' is too big to fit in long", s);
|
|
|
|
|
return LONG_MAX;
|
|
|
|
|
}
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return num;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2001-09-04 00:26:31 +08:00
|
|
|
|
/* {{{ _php_math_basetozval */
|
|
|
|
|
/*
|
|
|
|
|
* Convert a string representation of a base(2-36) number to a zval.
|
|
|
|
|
*/
|
2005-05-08 00:08:03 +08:00
|
|
|
|
PHPAPI int _php_math_basetozval(zval *arg, int base, zval *ret)
|
|
|
|
|
{
|
2002-10-04 10:21:33 +08:00
|
|
|
|
long num = 0;
|
2002-10-25 03:15:40 +08:00
|
|
|
|
double fnum = 0;
|
2001-09-04 00:26:31 +08:00
|
|
|
|
int i;
|
2001-09-19 18:57:50 +08:00
|
|
|
|
int mode = 0;
|
2001-09-04 00:26:31 +08:00
|
|
|
|
char c, *s;
|
2002-11-12 19:46:21 +08:00
|
|
|
|
long cutoff;
|
2002-10-04 10:21:33 +08:00
|
|
|
|
int cutlim;
|
2001-09-04 00:26:31 +08:00
|
|
|
|
|
|
|
|
|
if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) {
|
|
|
|
|
return FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s = Z_STRVAL_P(arg);
|
|
|
|
|
|
2002-10-04 10:21:33 +08:00
|
|
|
|
cutoff = LONG_MAX / base;
|
|
|
|
|
cutlim = LONG_MAX % base;
|
|
|
|
|
|
2001-09-19 19:03:58 +08:00
|
|
|
|
for (i = Z_STRLEN_P(arg); i > 0; i--) {
|
2001-09-19 18:57:50 +08:00
|
|
|
|
c = *s++;
|
|
|
|
|
|
2002-10-04 10:21:33 +08:00
|
|
|
|
/* might not work for EBCDIC */
|
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
|
c -= '0';
|
|
|
|
|
else if (c >= 'A' && c <= 'Z')
|
|
|
|
|
c -= 'A' - 10;
|
|
|
|
|
else if (c >= 'a' && c <= 'z')
|
|
|
|
|
c -= 'a' - 10;
|
|
|
|
|
else
|
|
|
|
|
continue;
|
2001-09-19 18:57:50 +08:00
|
|
|
|
|
2002-10-04 10:21:33 +08:00
|
|
|
|
if (c >= base)
|
2001-09-04 00:26:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2001-09-19 18:57:50 +08:00
|
|
|
|
switch (mode) {
|
|
|
|
|
case 0: /* Integer */
|
2002-10-04 10:21:33 +08:00
|
|
|
|
if (num < cutoff || (num == cutoff && c <= cutlim)) {
|
|
|
|
|
num = num * base + c;
|
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
fnum = num;
|
|
|
|
|
mode = 1;
|
|
|
|
|
}
|
2001-09-19 18:57:50 +08:00
|
|
|
|
/* fall-through */
|
|
|
|
|
case 1: /* Float */
|
2002-10-04 10:21:33 +08:00
|
|
|
|
fnum = fnum * base + c;
|
2001-09-19 18:57:50 +08:00
|
|
|
|
}
|
2001-09-04 00:26:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2001-09-19 18:57:50 +08:00
|
|
|
|
if (mode == 1) {
|
2001-09-04 00:26:31 +08:00
|
|
|
|
ZVAL_DOUBLE(ret, fnum);
|
|
|
|
|
} else {
|
|
|
|
|
ZVAL_LONG(ret, num);
|
|
|
|
|
}
|
|
|
|
|
return SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2005-05-08 00:08:03 +08:00
|
|
|
|
/* {{{ _php_math_longtobase */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
/*
|
|
|
|
|
* Convert a long to a string containing a base(2-36) representation of
|
|
|
|
|
* the number.
|
|
|
|
|
*/
|
2005-05-08 00:08:03 +08:00
|
|
|
|
PHPAPI char * _php_math_longtobase(zval *arg, int base)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
|
|
|
|
static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
2001-09-20 16:22:44 +08:00
|
|
|
|
char buf[(sizeof(unsigned long) << 3) + 1];
|
|
|
|
|
char *ptr, *end;
|
2000-09-18 05:11:55 +08:00
|
|
|
|
unsigned long value;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2001-08-01 21:48:14 +08:00
|
|
|
|
if (Z_TYPE_P(arg) != IS_LONG || base < 2 || base > 36) {
|
2004-07-19 15:19:50 +08:00
|
|
|
|
return STR_EMPTY_ALLOC();
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2001-08-01 21:48:14 +08:00
|
|
|
|
value = Z_LVAL_P(arg);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2001-09-20 16:22:44 +08:00
|
|
|
|
end = ptr = buf + sizeof(buf) - 1;
|
|
|
|
|
*ptr = '\0';
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
|
|
do {
|
2001-09-20 16:22:44 +08:00
|
|
|
|
*--ptr = digits[value % base];
|
1999-04-17 08:37:12 +08:00
|
|
|
|
value /= base;
|
2001-09-20 16:22:44 +08:00
|
|
|
|
} while (ptr > buf && value);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2001-09-20 16:22:44 +08:00
|
|
|
|
return estrndup(ptr, end - ptr);
|
|
|
|
|
}
|
2001-09-04 00:26:31 +08:00
|
|
|
|
/* }}} */
|
|
|
|
|
|
2005-05-08 00:08:03 +08:00
|
|
|
|
/* {{{ _php_math_zvaltobase */
|
2001-09-04 00:26:31 +08:00
|
|
|
|
/*
|
|
|
|
|
* Convert a zval to a string containing a base(2-36) representation of
|
|
|
|
|
* the number.
|
|
|
|
|
*/
|
2005-05-08 00:08:03 +08:00
|
|
|
|
PHPAPI char * _php_math_zvaltobase(zval *arg, int base TSRMLS_DC)
|
2001-09-04 00:26:31 +08:00
|
|
|
|
{
|
|
|
|
|
static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
|
|
|
if ((Z_TYPE_P(arg) != IS_LONG && Z_TYPE_P(arg) != IS_DOUBLE) || base < 2 || base > 36) {
|
2004-07-19 15:19:50 +08:00
|
|
|
|
return STR_EMPTY_ALLOC();
|
2001-09-04 00:26:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2001-09-20 16:22:44 +08:00
|
|
|
|
if (Z_TYPE_P(arg) == IS_DOUBLE) {
|
|
|
|
|
double fvalue = floor(Z_DVAL_P(arg)); /* floor it just in case */
|
|
|
|
|
char *ptr, *end;
|
|
|
|
|
char buf[(sizeof(double) << 3) + 1];
|
2001-09-04 00:26:31 +08:00
|
|
|
|
|
2002-06-18 08:04:33 +08:00
|
|
|
|
/* Don't try to convert +/- infinity */
|
|
|
|
|
if (fvalue == HUGE_VAL || fvalue == -HUGE_VAL) {
|
2002-08-24 09:19:28 +08:00
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number too large");
|
2004-07-19 15:19:50 +08:00
|
|
|
|
return STR_EMPTY_ALLOC();
|
2002-06-18 08:04:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2001-09-20 16:22:44 +08:00
|
|
|
|
end = ptr = buf + sizeof(buf) - 1;
|
|
|
|
|
*ptr = '\0';
|
2001-09-04 00:26:31 +08:00
|
|
|
|
|
2001-09-20 16:22:44 +08:00
|
|
|
|
do {
|
|
|
|
|
*--ptr = digits[(int) fmod(fvalue, base)];
|
|
|
|
|
fvalue /= base;
|
|
|
|
|
} while (ptr > buf && fabs(fvalue) >= 1);
|
2001-09-04 00:26:31 +08:00
|
|
|
|
|
2001-09-20 16:22:44 +08:00
|
|
|
|
return estrndup(ptr, end - ptr);
|
2001-09-04 00:26:31 +08:00
|
|
|
|
}
|
2001-09-20 16:22:44 +08:00
|
|
|
|
|
|
|
|
|
return _php_math_longtobase(arg, base);
|
2001-09-04 00:26:31 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
/* {{{ proto int bindec(string binary_number)
|
|
|
|
|
Returns the decimal equivalent of the binary number */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(bindec)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **arg;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_string_ex(arg);
|
2001-09-04 00:26:31 +08:00
|
|
|
|
if(_php_math_basetozval(*arg, 2, return_value) != SUCCESS) {
|
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2000-07-09 22:14:51 +08:00
|
|
|
|
/* {{{ proto int hexdec(string hexadecimal_number)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Returns the decimal equivalent of the hexadecimal number */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(hexdec)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **arg;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_string_ex(arg);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2001-09-04 00:26:31 +08:00
|
|
|
|
if(_php_math_basetozval(*arg, 16, return_value) != SUCCESS) {
|
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
/* {{{ proto int octdec(string octal_number)
|
|
|
|
|
Returns the decimal equivalent of an octal string */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(octdec)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **arg;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_string_ex(arg);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2001-09-04 00:26:31 +08:00
|
|
|
|
if(_php_math_basetozval(*arg, 8, return_value) != SUCCESS) {
|
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
/* {{{ proto string decbin(int decimal_number)
|
|
|
|
|
Returns a string containing a binary representation of the number */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(decbin)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **arg;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
char *result;
|
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_long_ex(arg);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
1999-10-14 03:43:36 +08:00
|
|
|
|
result = _php_math_longtobase(*arg, 2);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_TYPE_P(return_value) = IS_STRING;
|
|
|
|
|
Z_STRLEN_P(return_value) = strlen(result);
|
|
|
|
|
Z_STRVAL_P(return_value) = result;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
/* {{{ proto string decoct(int decimal_number)
|
|
|
|
|
Returns a string containing an octal representation of the given number */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(decoct)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **arg;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
char *result;
|
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_long_ex(arg);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
1999-10-14 03:43:36 +08:00
|
|
|
|
result = _php_math_longtobase(*arg, 8);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_TYPE_P(return_value) = IS_STRING;
|
|
|
|
|
Z_STRLEN_P(return_value) = strlen(result);
|
|
|
|
|
Z_STRVAL_P(return_value) = result;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
/* {{{ proto string dechex(int decimal_number)
|
|
|
|
|
Returns a string containing a hexadecimal representation of the given number */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(dechex)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **arg;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
char *result;
|
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
|
|
|
|
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_long_ex(arg);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
1999-10-14 03:43:36 +08:00
|
|
|
|
result = _php_math_longtobase(*arg, 16);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
Z_TYPE_P(return_value) = IS_STRING;
|
|
|
|
|
Z_STRLEN_P(return_value) = strlen(result);
|
|
|
|
|
Z_STRVAL_P(return_value) = result;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
/* {{{ proto string base_convert(string number, int frombase, int tobase)
|
2002-01-05 15:54:49 +08:00
|
|
|
|
Converts a number in a string from any base <= 36 to any base <= 36 */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(base_convert)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
1999-10-14 03:43:36 +08:00
|
|
|
|
zval **number, **frombase, **tobase, temp;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
char *result;
|
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &number, &frombase, &tobase) == FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
convert_to_string_ex(number);
|
|
|
|
|
convert_to_long_ex(frombase);
|
|
|
|
|
convert_to_long_ex(tobase);
|
2001-08-01 21:48:14 +08:00
|
|
|
|
if (Z_LVAL_PP(frombase) < 2 || Z_LVAL_PP(frombase) > 36) {
|
2003-08-29 00:49:57 +08:00
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid `from base' (%ld)", Z_LVAL_PP(frombase));
|
1999-04-17 08:37:12 +08:00
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
2001-08-01 21:48:14 +08:00
|
|
|
|
if (Z_LVAL_PP(tobase) < 2 || Z_LVAL_PP(tobase) > 36) {
|
2003-08-29 00:49:57 +08:00
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid `to base' (%ld)", Z_LVAL_PP(tobase));
|
1999-04-17 08:37:12 +08:00
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
2001-09-04 00:26:31 +08:00
|
|
|
|
|
|
|
|
|
if(_php_math_basetozval(*number, Z_LVAL_PP(frombase), &temp) != SUCCESS) {
|
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
2002-06-18 08:14:52 +08:00
|
|
|
|
result = _php_math_zvaltobase(&temp, Z_LVAL_PP(tobase) TSRMLS_CC);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
RETVAL_STRING(result, 0);
|
1999-10-14 03:43:36 +08:00
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2005-05-08 00:08:03 +08:00
|
|
|
|
/* {{{ _php_math_number_format
|
|
|
|
|
*/
|
2001-12-03 13:12:48 +08:00
|
|
|
|
PHPAPI char *_php_math_number_format(double d, int dec, char dec_point, char thousand_sep)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
2003-01-09 23:44:49 +08:00
|
|
|
|
char *tmpbuf = NULL, *resbuf;
|
2001-08-12 01:03:37 +08:00
|
|
|
|
char *s, *t; /* source, target */
|
2003-01-09 23:44:49 +08:00
|
|
|
|
char *dp;
|
|
|
|
|
int integral;
|
2001-08-12 01:03:37 +08:00
|
|
|
|
int tmplen, reslen=0;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
int count=0;
|
|
|
|
|
int is_negative=0;
|
2003-11-18 10:50:41 +08:00
|
|
|
|
|
2003-01-09 23:44:49 +08:00
|
|
|
|
if (d < 0) {
|
|
|
|
|
is_negative = 1;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
d = -d;
|
|
|
|
|
}
|
2004-04-30 21:26:01 +08:00
|
|
|
|
if (!dec_point && dec > 0) {
|
|
|
|
|
d *= pow(10, dec);
|
|
|
|
|
dec = 0;
|
|
|
|
|
} else {
|
|
|
|
|
dec = MAX(0, dec);
|
|
|
|
|
}
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
2003-11-18 10:50:41 +08:00
|
|
|
|
PHP_ROUND_WITH_FUZZ(d, dec);
|
|
|
|
|
|
2003-01-09 23:44:49 +08:00
|
|
|
|
tmplen = spprintf(&tmpbuf, 0, "%.*f", dec, d);
|
|
|
|
|
|
|
|
|
|
if (tmpbuf == NULL || !isdigit((int)tmpbuf[0])) {
|
1999-08-01 00:20:06 +08:00
|
|
|
|
return tmpbuf;
|
|
|
|
|
}
|
|
|
|
|
|
2003-01-09 23:44:49 +08:00
|
|
|
|
/* calculate the length of the return buffer */
|
|
|
|
|
dp = strchr(tmpbuf, '.');
|
|
|
|
|
if (dp) {
|
|
|
|
|
integral = dp - tmpbuf;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
} else {
|
2003-01-09 23:44:49 +08:00
|
|
|
|
/* no decimal point was found */
|
|
|
|
|
integral = tmplen;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* allow for thousand separators */
|
|
|
|
|
if (thousand_sep) {
|
2003-01-16 21:19:50 +08:00
|
|
|
|
integral += (integral-1) / 3;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
2003-01-09 23:44:49 +08:00
|
|
|
|
|
2003-01-10 21:32:24 +08:00
|
|
|
|
reslen = integral;
|
|
|
|
|
|
|
|
|
|
if (dec) {
|
|
|
|
|
reslen += 1 + dec;
|
|
|
|
|
}
|
2003-01-09 23:44:49 +08:00
|
|
|
|
|
|
|
|
|
/* add a byte for minus sign */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
if (is_negative) {
|
|
|
|
|
reslen++;
|
|
|
|
|
}
|
2003-01-09 23:44:49 +08:00
|
|
|
|
resbuf = (char *) emalloc(reslen+1); /* +1 for NUL terminator */
|
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
s = tmpbuf+tmplen-1;
|
|
|
|
|
t = resbuf+reslen;
|
2003-01-09 23:44:49 +08:00
|
|
|
|
*t-- = '\0';
|
|
|
|
|
|
|
|
|
|
/* copy the decimal places.
|
|
|
|
|
* Take care, as the sprintf implementation may return less places than
|
|
|
|
|
* we requested due to internal buffer limitations */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
if (dec) {
|
2003-01-09 23:44:49 +08:00
|
|
|
|
int declen = dp ? strlen(dp+1) : 0;
|
|
|
|
|
int topad = declen > 0 ? dec - declen : 0;
|
|
|
|
|
|
|
|
|
|
/* pad with '0's */
|
2003-01-10 21:32:24 +08:00
|
|
|
|
|
2003-01-09 23:44:49 +08:00
|
|
|
|
while (topad--) {
|
|
|
|
|
*t-- = '0';
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
2003-01-09 23:44:49 +08:00
|
|
|
|
|
2003-01-10 21:32:24 +08:00
|
|
|
|
if (dp) {
|
|
|
|
|
/* now copy the chars after the point */
|
|
|
|
|
memcpy(t - declen + 1, dp + 1, declen);
|
|
|
|
|
|
|
|
|
|
t -= declen;
|
|
|
|
|
s -= declen;
|
|
|
|
|
}
|
2003-01-09 23:44:49 +08:00
|
|
|
|
|
|
|
|
|
/* add decimal point */
|
|
|
|
|
*t-- = dec_point;
|
2000-06-13 12:31:02 +08:00
|
|
|
|
s--;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
2003-01-10 21:32:24 +08:00
|
|
|
|
|
2003-01-09 23:44:49 +08:00
|
|
|
|
/* copy the numbers before the decimal place, adding thousand
|
|
|
|
|
* separator every three digits */
|
|
|
|
|
while(s >= tmpbuf) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
*t-- = *s--;
|
2000-07-15 02:46:13 +08:00
|
|
|
|
if (thousand_sep && (++count%3)==0 && s>=tmpbuf) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
*t-- = thousand_sep;
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-01-09 23:44:49 +08:00
|
|
|
|
|
|
|
|
|
/* and a minus sign, if needed */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
if (is_negative) {
|
|
|
|
|
*t-- = '-';
|
|
|
|
|
}
|
2003-01-09 23:44:49 +08:00
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
efree(tmpbuf);
|
2003-01-10 21:32:24 +08:00
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
return resbuf;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
/* }}} */
|
2005-05-08 00:08:03 +08:00
|
|
|
|
|
2001-09-22 05:59:27 +08:00
|
|
|
|
/* {{{ proto string number_format(float number [, int num_decimal_places [, string dec_seperator, string thousands_seperator]])
|
1999-04-17 08:37:12 +08:00
|
|
|
|
Formats a number with grouped thousands */
|
1999-05-16 19:19:26 +08:00
|
|
|
|
PHP_FUNCTION(number_format)
|
1999-04-17 08:37:12 +08:00
|
|
|
|
{
|
2001-08-12 01:03:37 +08:00
|
|
|
|
zval **num, **dec, **t_s, **d_p;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
char thousand_sep=',', dec_point='.';
|
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
|
switch(ZEND_NUM_ARGS()) {
|
1999-10-14 03:43:36 +08:00
|
|
|
|
case 1:
|
1999-12-19 06:40:35 +08:00
|
|
|
|
if (zend_get_parameters_ex(1, &num)==FAILURE) {
|
1999-10-14 03:43:36 +08:00
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
2001-08-12 01:03:37 +08:00
|
|
|
|
RETURN_STRING(_php_math_number_format(Z_DVAL_PP(num), 0, dec_point, thousand_sep), 0);
|
1999-10-14 03:43:36 +08:00
|
|
|
|
break;
|
|
|
|
|
case 2:
|
1999-12-19 06:40:35 +08:00
|
|
|
|
if (zend_get_parameters_ex(2, &num, &dec)==FAILURE) {
|
1999-10-14 03:43:36 +08:00
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
convert_to_long_ex(dec);
|
2001-08-12 01:03:37 +08:00
|
|
|
|
RETURN_STRING(_php_math_number_format(Z_DVAL_PP(num), Z_LVAL_PP(dec), dec_point, thousand_sep), 0);
|
1999-10-14 03:43:36 +08:00
|
|
|
|
break;
|
|
|
|
|
case 4:
|
1999-12-19 06:40:35 +08:00
|
|
|
|
if (zend_get_parameters_ex(4, &num, &dec, &d_p, &t_s)==FAILURE) {
|
1999-10-14 03:43:36 +08:00
|
|
|
|
RETURN_FALSE;
|
|
|
|
|
}
|
|
|
|
|
convert_to_double_ex(num);
|
|
|
|
|
convert_to_long_ex(dec);
|
2004-12-14 08:37:19 +08:00
|
|
|
|
|
|
|
|
|
if (Z_TYPE_PP(d_p) != IS_NULL) {
|
|
|
|
|
convert_to_string_ex(d_p);
|
2005-07-29 20:58:25 +08:00
|
|
|
|
if (Z_STRLEN_PP(d_p)>=1) {
|
2004-12-14 08:37:19 +08:00
|
|
|
|
dec_point=Z_STRVAL_PP(d_p)[0];
|
|
|
|
|
} else if (Z_STRLEN_PP(d_p)==0) {
|
|
|
|
|
dec_point=0;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
}
|
2004-12-14 08:37:19 +08:00
|
|
|
|
if (Z_TYPE_PP(t_s) != IS_NULL) {
|
|
|
|
|
convert_to_string_ex(t_s);
|
2005-07-29 20:58:25 +08:00
|
|
|
|
if (Z_STRLEN_PP(t_s)>=1) {
|
2004-12-14 08:37:19 +08:00
|
|
|
|
thousand_sep=Z_STRVAL_PP(t_s)[0];
|
|
|
|
|
} else if(Z_STRLEN_PP(t_s)==0) {
|
|
|
|
|
thousand_sep=0;
|
|
|
|
|
}
|
1999-10-14 03:43:36 +08:00
|
|
|
|
}
|
2001-08-12 01:03:37 +08:00
|
|
|
|
RETURN_STRING(_php_math_number_format(Z_DVAL_PP(num), Z_LVAL_PP(dec), dec_point, thousand_sep), 0);
|
1999-10-14 03:43:36 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
|
break;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
1999-10-14 03:43:36 +08:00
|
|
|
|
|
2002-03-03 01:08:09 +08:00
|
|
|
|
/* {{{ proto float fmod(float x, float y)
|
|
|
|
|
Returns the remainder of dividing x by y as a float */
|
2002-02-21 19:44:41 +08:00
|
|
|
|
PHP_FUNCTION(fmod)
|
|
|
|
|
{
|
|
|
|
|
double num1, num2;
|
|
|
|
|
|
|
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd", &num1, &num2) == FAILURE) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Z_DVAL_P(return_value) = fmod(num1, num2);
|
|
|
|
|
Z_TYPE_P(return_value) = IS_DOUBLE;
|
|
|
|
|
}
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
2005-05-02 20:12:04 +08:00
|
|
|
|
|
2005-05-02 17:17:49 +08:00
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
|
/*
|
|
|
|
|
* Local variables:
|
|
|
|
|
* tab-width: 4
|
|
|
|
|
* c-basic-offset: 4
|
|
|
|
|
* End:
|
2002-10-15 22:51:01 +08:00
|
|
|
|
* vim600: fdm=marker
|
|
|
|
|
* vim: noet sw=4 ts=4
|
1999-04-17 08:37:12 +08:00
|
|
|
|
*/
|