Back to index

php5  5.3.10
Functions
math.c File Reference
#include "php.h"
#include "php_math.h"
#include "zend_multiply.h"
#include <math.h>
#include <float.h>
#include <stdlib.h>
#include "basic_functions.h"

Go to the source code of this file.

Functions

static int php_intlog10abs (double value)
static double php_intpow10 (int power)
static int php_math_is_finite (double value)
static double php_round_helper (double value, int mode)
PHPAPI double _php_math_round (double value, int places, int mode)
static double php_asinh (double z)
static double php_acosh (double x)
static double php_atanh (double z)
static double php_log1p (double x)
static double php_expm1 (double x)
 PHP_FUNCTION (abs)
 PHP_FUNCTION (ceil)
 PHP_FUNCTION (floor)
 PHP_FUNCTION (round)
 PHP_FUNCTION (sin)
 PHP_FUNCTION (cos)
 PHP_FUNCTION (tan)
 PHP_FUNCTION (asin)
 PHP_FUNCTION (acos)
 PHP_FUNCTION (atan)
 PHP_FUNCTION (atan2)
 PHP_FUNCTION (sinh)
 PHP_FUNCTION (cosh)
 PHP_FUNCTION (tanh)
 PHP_FUNCTION (asinh)
 PHP_FUNCTION (acosh)
 PHP_FUNCTION (atanh)
 PHP_FUNCTION (pi)
 PHP_FUNCTION (is_finite)
 PHP_FUNCTION (is_infinite)
 PHP_FUNCTION (is_nan)
 PHP_FUNCTION (pow)
 PHP_FUNCTION (exp)
 PHP_FUNCTION (expm1)
 PHP_FUNCTION (log1p)
 PHP_FUNCTION (log)
 PHP_FUNCTION (log10)
 PHP_FUNCTION (sqrt)
 PHP_FUNCTION (hypot)
 PHP_FUNCTION (deg2rad)
 PHP_FUNCTION (rad2deg)
PHPAPI long _php_math_basetolong (zval *arg, int base)
PHPAPI int _php_math_basetozval (zval *arg, int base, zval *ret)
PHPAPI char * _php_math_longtobase (zval *arg, int base)
PHPAPI char * _php_math_zvaltobase (zval *arg, int base TSRMLS_DC)
 PHP_FUNCTION (bindec)
 PHP_FUNCTION (hexdec)
 PHP_FUNCTION (octdec)
 PHP_FUNCTION (decbin)
 PHP_FUNCTION (decoct)
 PHP_FUNCTION (dechex)
 PHP_FUNCTION (base_convert)
PHPAPI char * _php_math_number_format (double d, int dec, char dec_point, char thousand_sep)
 PHP_FUNCTION (number_format)
 PHP_FUNCTION (fmod)

Function Documentation

PHPAPI long _php_math_basetolong ( zval *  arg,
int  base 
)

Definition at line 794 of file math.c.

{
       long num = 0, digit, onum;
       int i;
       char c, *s;

       if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) {
              return 0;
       }

       s = Z_STRVAL_P(arg);

       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;
              
              if (digit >= base) {
                     continue;
              }

              onum = num;
              num = num * base + digit;
              if (num > onum)
                     continue;

              {
                     TSRMLS_FETCH();

                     php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number '%s' is too big to fit in long", s);
                     return LONG_MAX;
              }
       }

       return num;
}
PHPAPI int _php_math_basetozval ( zval *  arg,
int  base,
zval *  ret 
)

Definition at line 839 of file math.c.

{
       long num = 0;
       double fnum = 0;
       int i;
       int mode = 0;
       char c, *s;
       long cutoff;
       int cutlim;

       if (Z_TYPE_P(arg) != IS_STRING || base < 2 || base > 36) {
              return FAILURE;
       }

       s = Z_STRVAL_P(arg);

       cutoff = LONG_MAX / base;
       cutlim = LONG_MAX % base;
       
       for (i = Z_STRLEN_P(arg); i > 0; i--) {
              c = *s++;

              /* 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;

              if (c >= base)
                     continue;
              
              switch (mode) {
              case 0: /* Integer */
                     if (num < cutoff || (num == cutoff && c <= cutlim)) {
                            num = num * base + c;
                            break;
                     } else {
                            fnum = num;
                            mode = 1;
                     }
                     /* fall-through */
              case 1: /* Float */
                     fnum = fnum * base + c;
              }      
       }

       if (mode == 1) {
              ZVAL_DOUBLE(ret, fnum);
       } else {
              ZVAL_LONG(ret, num);
       }
       return SUCCESS;
}

Here is the caller graph for this function:

PHPAPI char* _php_math_longtobase ( zval *  arg,
int  base 
)

Definition at line 903 of file math.c.

{
       static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
       char buf[(sizeof(unsigned long) << 3) + 1];
       char *ptr, *end;
       unsigned long value;

       if (Z_TYPE_P(arg) != IS_LONG || base < 2 || base > 36) {
              return STR_EMPTY_ALLOC();
       }

       value = Z_LVAL_P(arg);

       end = ptr = buf + sizeof(buf) - 1;
       *ptr = '\0';

       do {
              *--ptr = digits[value % base];
              value /= base;
       } while (ptr > buf && value);

       return estrndup(ptr, end - ptr);
}

Here is the caller graph for this function:

PHPAPI char* _php_math_number_format ( double  d,
int  dec,
char  dec_point,
char  thousand_sep 
)

Definition at line 1095 of file math.c.

{
       char *tmpbuf = NULL, *resbuf;
       char *s, *t;  /* source, target */
       char *dp;
       int integral;
       int tmplen, reslen=0;
       int count=0;
       int is_negative=0;

       if (d < 0) {
              is_negative = 1;
              d = -d;
       }

       dec = MAX(0, dec);
       d = _php_math_round(d, dec, PHP_ROUND_HALF_UP);

       tmplen = spprintf(&tmpbuf, 0, "%.*F", dec, d);

       if (tmpbuf == NULL || !isdigit((int)tmpbuf[0])) {
              return tmpbuf;
       }

       /* find decimal point, if expected */
       if (dec) {
              dp = strpbrk(tmpbuf, ".,");
       } else {
              dp = NULL;
       }

       /* calculate the length of the return buffer */
       if (dp) {
              integral = dp - tmpbuf;
       } else {
              /* no decimal point was found */
              integral = tmplen;
       }

       /* allow for thousand separators */
       if (thousand_sep) {
              integral += (integral-1) / 3;
       }
       
       reslen = integral;
       
       if (dec) {
              reslen += dec;

              if (dec_point) {
                     reslen++;
              }
       }

       /* add a byte for minus sign */
       if (is_negative) {
              reslen++;
       }
       resbuf = (char *) emalloc(reslen+1); /* +1 for NUL terminator */

       s = tmpbuf+tmplen-1;
       t = resbuf+reslen;
       *t-- = '\0';

       /* copy the decimal places.
        * Take care, as the sprintf implementation may return less places than
        * we requested due to internal buffer limitations */
       if (dec) {
              int declen = dp ? s - dp : 0;
              int topad = dec > declen ? dec - declen : 0;

              /* pad with '0's */
              while (topad--) {
                     *t-- = '0';
              }
              
              if (dp) {
                     s -= declen + 1; /* +1 to skip the point */
                     t -= declen;

                     /* now copy the chars after the point */
                     memcpy(t + 1, dp + 1, declen);
              }

              /* add decimal point */
              if (dec_point) {
                     *t-- = dec_point;
              }
       }

       /* copy the numbers before the decimal point, adding thousand
        * separator every three digits */
       while(s >= tmpbuf) {
              *t-- = *s--;
              if (thousand_sep && (++count%3)==0 && s>=tmpbuf) {
                     *t-- = thousand_sep;
              }
       }

       /* and a minus sign, if needed */
       if (is_negative) {
              *t-- = '-';
       }

       efree(tmpbuf);
       
       return resbuf;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI double _php_math_round ( double  value,
int  places,
int  mode 
)

Definition at line 139 of file math.c.

                                                                  {
       double f1, f2;
       double tmp_value;
       int precision_places;

       if (!php_math_is_finite(value)) {
              return value;
       }
       
       precision_places = 14 - php_intlog10abs(value);

       f1 = php_intpow10(abs(places));

       /* If the decimal precision guaranteed by FP arithmetic is higher than
          the requested places BUT is small enough to make sure a non-zero value
          is returned, pre-round the result to the precision */
       if (precision_places > places && precision_places - places < 15) {
              f2 = php_intpow10(abs(precision_places));
              if (precision_places >= 0) {
                     tmp_value = value * f2;
              } else {
                     tmp_value = value / f2;
              }
              /* preround the result (tmp_value will always be something * 1e14,
                 thus never larger than 1e15 here) */
              tmp_value = php_round_helper(tmp_value, mode);
              /* now correctly move the decimal point */
              f2 = php_intpow10(abs(places - precision_places));
              /* because places < precision_places */
              tmp_value = tmp_value / f2;
       } else {
              /* adjust the value */
              if (places >= 0) {
                     tmp_value = value * f1;
              } else {
                     tmp_value = value / f1;
              }
              /* This value is beyond our precision, so rounding it is pointless */
              if (fabs(tmp_value) >= 1e15) {
                     return value;
              }
       }

       /* round the temp value */
       tmp_value = php_round_helper(tmp_value, mode);
       
       /* see if it makes sense to use simple division to round the value */
       if (abs(places) < 23) {
              if (places > 0) {
                     tmp_value = tmp_value / f1;
              } else {
                     tmp_value = tmp_value * f1;
              }
       } else {
              /* Simple division can't be used since that will cause wrong results.
                 Instead, the number is converted to a string and back again using
                 strtod(). strtod() will return the nearest possible FP value for
                 that string. */

              /* 40 Bytes should be more than enough for this format string. The
                 float won't be larger than 1e15 anyway. But just in case, use
                 snprintf() and make sure the buffer is zero-terminated */
              char buf[40];
              snprintf(buf, 39, "%15fe%d", tmp_value, -places);
              buf[39] = '\0';
              tmp_value = zend_strtod(buf, NULL);
              /* couldn't convert to string and back */
              if (!zend_finite(tmp_value) || zend_isnan(tmp_value)) {
                     tmp_value = value;
              }
       }

       return tmp_value;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PHPAPI char* _php_math_zvaltobase ( zval *  arg,
int base  TSRMLS_DC 
)

Definition at line 933 of file math.c.

{
       static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";

       if ((Z_TYPE_P(arg) != IS_LONG && Z_TYPE_P(arg) != IS_DOUBLE) || base < 2 || base > 36) {
              return STR_EMPTY_ALLOC();
       }

       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];

              /* Don't try to convert +/- infinity */
              if (fvalue == HUGE_VAL || fvalue == -HUGE_VAL) {
                     php_error_docref(NULL TSRMLS_CC, E_WARNING, "Number too large");
                     return STR_EMPTY_ALLOC();
              }

              end = ptr = buf + sizeof(buf) - 1;
              *ptr = '\0';

              do {
                     *--ptr = digits[(int) fmod(fvalue, base)];
                     fvalue /= base;
              } while (ptr > buf && fabs(fvalue) >= 1);

              return estrndup(ptr, end - ptr);
       }
       
       return _php_math_longtobase(arg, base);
}      

Here is the call graph for this function:

Here is the caller graph for this function:

static double php_acosh ( double  x) [static]

Definition at line 229 of file math.c.

{
#ifdef HAVE_ACOSH
       return(acosh(x));
#else
       return(log(x + sqrt(x * x - 1)));
#endif
}

Here is the caller graph for this function:

static double php_asinh ( double  z) [static]

Definition at line 217 of file math.c.

{
#ifdef HAVE_ASINH
       return(asinh(z));
#else
       return(log(z + sqrt(1 + pow(z, 2))) / log(M_E));
#endif
}

Here is the caller graph for this function:

static double php_atanh ( double  z) [static]

Definition at line 241 of file math.c.

{
#ifdef HAVE_ATANH
       return(atanh(z));
#else
       return(0.5 * log((1 + z) / (1 - z)));
#endif
}

Here is the caller graph for this function:

static double php_expm1 ( double  x) [static]

Definition at line 265 of file math.c.

{
#if !defined(PHP_WIN32) && !defined(NETWARE)
       return(expm1(x));
#else
       return(exp(x) - 1);
#endif
}

Here is the caller graph for this function:

PHP_FUNCTION ( abs  )

Definition at line 277 of file math.c.

{
       zval **value;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &value) == FAILURE) {
              return;
       }
       convert_scalar_to_number_ex(value);
       
       if (Z_TYPE_PP(value) == IS_DOUBLE) {
              RETURN_DOUBLE(fabs(Z_DVAL_PP(value)));
       } else if (Z_TYPE_PP(value) == IS_LONG) {
              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));
              }
       }
       RETURN_FALSE;
}

Here is the call graph for this function:

PHP_FUNCTION ( ceil  )

Definition at line 301 of file math.c.

{
       zval **value;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &value) == FAILURE) {
              return;
       }
       convert_scalar_to_number_ex(value);

       if (Z_TYPE_PP(value) == IS_DOUBLE) {
              RETURN_DOUBLE(ceil(Z_DVAL_PP(value)));
       } else if (Z_TYPE_PP(value) == IS_LONG) {
              convert_to_double_ex(value);
              RETURN_DOUBLE(Z_DVAL_PP(value));
       }
       RETURN_FALSE;
}

Here is the call graph for this function:

PHP_FUNCTION ( floor  )

Definition at line 322 of file math.c.

{
       zval **value;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &value) == FAILURE) {
              return;
       }
       convert_scalar_to_number_ex(value);

       if (Z_TYPE_PP(value) == IS_DOUBLE) {
              RETURN_DOUBLE(floor(Z_DVAL_PP(value)));
       } else if (Z_TYPE_PP(value) == IS_LONG) {
              convert_to_double_ex(value);
              RETURN_DOUBLE(Z_DVAL_PP(value));
       }
       RETURN_FALSE;
}

Here is the call graph for this function:

Definition at line 343 of file math.c.

{
       zval **value;
       int places = 0;
       long precision = 0;
       long mode = PHP_ROUND_HALF_UP;
       double return_val;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z|ll", &value, &precision, &mode) == FAILURE) {
              return;
       }

       if (ZEND_NUM_ARGS() >= 2) {
              places = (int) precision;
       }
       convert_scalar_to_number_ex(value);

       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:
                     return_val = (Z_TYPE_PP(value) == IS_LONG) ? (double)Z_LVAL_PP(value) : Z_DVAL_PP(value);
                     return_val = _php_math_round(return_val, places, mode);
                     RETURN_DOUBLE(return_val);
                     break;

              default:
                     RETURN_FALSE;
                     break;
       }
}

Here is the call graph for this function:

PHP_FUNCTION ( sin  )

Definition at line 383 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(sin(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( cos  )

Definition at line 396 of file math.c.

{
       double num;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(cos(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( tan  )

Definition at line 409 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(tan(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( asin  )

Definition at line 422 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(asin(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( acos  )

Definition at line 435 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(acos(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( atan  )

Definition at line 448 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(atan(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( atan2  )

Definition at line 461 of file math.c.

{
       double num1, num2;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd", &num1, &num2) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(atan2(num1, num2));
}

Here is the call graph for this function:

PHP_FUNCTION ( sinh  )

Definition at line 474 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(sinh(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( cosh  )

Definition at line 487 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(cosh(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( tanh  )

Definition at line 500 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(tanh(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( asinh  )

Definition at line 513 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(php_asinh(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( acosh  )

Definition at line 526 of file math.c.

{
       double num;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(php_acosh(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( atanh  )

Definition at line 539 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(php_atanh(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( pi  )

Definition at line 552 of file math.c.

PHP_FUNCTION ( is_finite  )

Definition at line 560 of file math.c.

{
       double dval;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dval) == FAILURE) {
              return;
       }
       RETURN_BOOL(zend_finite(dval));
}

Here is the call graph for this function:

PHP_FUNCTION ( is_infinite  )

Definition at line 573 of file math.c.

{
       double dval;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dval) == FAILURE) {
              return;
       }
       RETURN_BOOL(zend_isinf(dval));
}

Here is the call graph for this function:

PHP_FUNCTION ( is_nan  )

Definition at line 586 of file math.c.

{
       double dval;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dval) == FAILURE) {
              return;
       }
       RETURN_BOOL(zend_isnan(dval));
}

Here is the call graph for this function:

PHP_FUNCTION ( pow  )

Definition at line 599 of file math.c.

{
       zval *zbase, *zexp;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/z/", &zbase, &zexp) == FAILURE) {
              return;
       }

       /* make sure we're dealing with numbers */
       convert_scalar_to_number(zbase TSRMLS_CC);
       convert_scalar_to_number(zexp TSRMLS_CC);

       /* if both base and exponent were longs, we'll try to get a long out */
       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);
              }

              /* calculate pow(long,long) in O(log exp) operations, bail if overflow */
              while (i >= 1) {
                     int overflow;
                     double dval = 0.0;

                     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);
                     }
              }
       }
       convert_to_double(zbase);
       convert_to_double(zexp);
       
       RETURN_DOUBLE(pow(Z_DVAL_P(zbase), Z_DVAL_P(zexp)));
}

Here is the call graph for this function:

PHP_FUNCTION ( exp  )

Definition at line 649 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }

       RETURN_DOUBLE(exp(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( expm1  )

Definition at line 667 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(php_expm1(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( log1p  )

Definition at line 684 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(php_log1p(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( log  )

Definition at line 697 of file math.c.

{
       double num, base = 0;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|d", &num, &base) == FAILURE) {
              return;
       }
       if (ZEND_NUM_ARGS() == 1) {
              RETURN_DOUBLE(log(num));
       }
       if (base <= 0.0) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "base must be greater than 0");                       
              RETURN_FALSE;
       }
       if (base == 1) {
              RETURN_DOUBLE(php_get_nan());
       } else {
              RETURN_DOUBLE(log(num) / log(base));
       }
}

Here is the call graph for this function:

PHP_FUNCTION ( log10  )

Definition at line 721 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(log10(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( sqrt  )

Definition at line 734 of file math.c.

{
       double num;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &num) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(sqrt(num));
}

Here is the call graph for this function:

PHP_FUNCTION ( hypot  )

Definition at line 747 of file math.c.

{
       double num1, num2;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd", &num1, &num2) == FAILURE) {
              return;
       }
#if HAVE_HYPOT
       RETURN_DOUBLE(hypot(num1, num2));
#elif defined(_MSC_VER)
       RETURN_DOUBLE(_hypot(num1, num2));
#else
       RETURN_DOUBLE(sqrt((num1 * num1) + (num2 * num2)));
#endif
}

Here is the call graph for this function:

PHP_FUNCTION ( deg2rad  )

Definition at line 766 of file math.c.

{
       double deg;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &deg) == FAILURE) {
              return;
       }
       RETURN_DOUBLE((deg / 180.0) * M_PI);
}

Here is the call graph for this function:

PHP_FUNCTION ( rad2deg  )

Definition at line 779 of file math.c.

{
       double rad;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &rad) == FAILURE) {
              return;
       }
       RETURN_DOUBLE((rad / M_PI) * 180);
}

Here is the call graph for this function:

PHP_FUNCTION ( bindec  )

Definition at line 969 of file math.c.

{
       zval **arg;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
              return;
       }
       convert_to_string_ex(arg);
       if (_php_math_basetozval(*arg, 2, return_value) == FAILURE) {
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

PHP_FUNCTION ( hexdec  )

Definition at line 985 of file math.c.

{
       zval **arg;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
              return;
       }
       convert_to_string_ex(arg);
       if (_php_math_basetozval(*arg, 16, return_value) == FAILURE) {
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

PHP_FUNCTION ( octdec  )

Definition at line 1001 of file math.c.

{
       zval **arg;
       
       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
              return;
       }
       convert_to_string_ex(arg);
       if (_php_math_basetozval(*arg, 8, return_value) == FAILURE) {
              RETURN_FALSE;
       }
}

Here is the call graph for this function:

PHP_FUNCTION ( decbin  )

Definition at line 1017 of file math.c.

{
       zval **arg;
       char *result;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
              return;
       }
       convert_to_long_ex(arg);
       result = _php_math_longtobase(*arg, 2);
       RETURN_STRING(result, 0);
}

Here is the call graph for this function:

PHP_FUNCTION ( decoct  )

Definition at line 1033 of file math.c.

{
       zval **arg;
       char *result;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
              return;
       }
       convert_to_long_ex(arg);
       result = _php_math_longtobase(*arg, 8);
       RETURN_STRING(result, 0);
}

Here is the call graph for this function:

PHP_FUNCTION ( dechex  )

Definition at line 1049 of file math.c.

{
       zval **arg;
       char *result;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
              return;
       }
       convert_to_long_ex(arg);
       result = _php_math_longtobase(*arg, 16);
       RETURN_STRING(result, 0);
}

Here is the call graph for this function:

PHP_FUNCTION ( base_convert  )

Definition at line 1065 of file math.c.

{
       zval **number, temp;
       long frombase, tobase;
       char *result;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zll", &number, &frombase, &tobase) == FAILURE) {
              return;
       }
       convert_to_string_ex(number);
       
       if (frombase < 2 || frombase > 36) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid `from base' (%ld)", frombase);
              RETURN_FALSE;
       }
       if (tobase < 2 || tobase > 36) {
              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid `to base' (%ld)", tobase);
              RETURN_FALSE;
       }

       if(_php_math_basetozval(*number, frombase, &temp) == FAILURE) {
              RETURN_FALSE;
       }
       result = _php_math_zvaltobase(&temp, tobase TSRMLS_CC);
       RETVAL_STRING(result, 0);
} 

Here is the call graph for this function:

PHP_FUNCTION ( number_format  )

Definition at line 1207 of file math.c.

{
       double num;
       long dec = 0;
       char *thousand_sep = NULL, *dec_point = NULL;
       char thousand_sep_chr = ',', dec_point_chr = '.';
       int thousand_sep_len = 0, dec_point_len = 0;
       
       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) {
              return;
       }

       switch(ZEND_NUM_ARGS()) {
       case 1:
              RETURN_STRING(_php_math_number_format(num, 0, dec_point_chr, thousand_sep_chr), 0);
              break;
       case 2:
              RETURN_STRING(_php_math_number_format(num, dec, dec_point_chr, thousand_sep_chr), 0);
              break;
       case 4:
              if (dec_point != NULL) {
                     if (dec_point_len) {
                            dec_point_chr = dec_point[0];
                     } else {
                            dec_point_chr = 0;
                     }
              }
              if (thousand_sep != NULL) {
                     if (thousand_sep_len) {
                            thousand_sep_chr = thousand_sep[0];
                     } else {
                            thousand_sep_chr = 0;       
                     }
              }
              RETURN_STRING(_php_math_number_format(num, dec, dec_point_chr, thousand_sep_chr), 0);
              break;
       default:
              WRONG_PARAM_COUNT;
              break;
       }
}

Here is the call graph for this function:

PHP_FUNCTION ( fmod  )

Definition at line 1252 of file math.c.

{
       double num1, num2;

       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "dd",  &num1, &num2) == FAILURE) {
              return;
       }
       RETURN_DOUBLE(fmod(num1, num2));
}

Here is the call graph for this function:

static int php_intlog10abs ( double  value) [inline, static]

Definition at line 36 of file math.c.

                                                {
       int result;
       value = fabs(value);

       if (value < 1e-8 || value > 1e23) {
              result = (int)floor(log10(value));
       } else {
              static const double values[] = {
                     1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 1e-1,
                     1e0,  1e1,  1e2,  1e3,  1e4,  1e5,  1e6,  1e7,
                     1e8,  1e9,  1e10, 1e11, 1e12, 1e13, 1e14, 1e15,
                     1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
              /* Do a binary search with 5 steps */
              result = 16;
              if (value < values[result]) {
                     result -= 8;
              } else {
                     result += 8;
              }
              if (value < values[result]) {
                     result -= 4;
              } else {
                     result += 4;
              }
              if (value < values[result]) {
                     result -= 2;
              } else {
                     result += 2;
              }
              if (value < values[result]) {
                     result -= 1;
              } else {
                     result += 1;
              }
              if (value < values[result]) {
                     result -= 1;
              }
              result -= 8;
       }
       return result;
}

Here is the caller graph for this function:

static double php_intpow10 ( int  power) [inline, static]

Definition at line 81 of file math.c.

                                             {
       static const double powers[] = {
              1e0,  1e1,  1e2,  1e3,  1e4,  1e5,  1e6,  1e7,
              1e8,  1e9,  1e10, 1e11, 1e12, 1e13, 1e14, 1e15,
              1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};

       /* Not in lookup table */
       if (power < 0 || power > 22) {
              return pow(10.0, (double)power);
       }
       return powers[power];
}

Here is the caller graph for this function:

static double php_log1p ( double  x) [static]

Definition at line 253 of file math.c.

{
#ifdef HAVE_LOG1P
       return(log1p(x));
#else
       return(log(1 + x));
#endif
}

Here is the caller graph for this function:

static int php_math_is_finite ( double  value) [inline, static]

Definition at line 96 of file math.c.

                                                   {
#if defined(PHP_WIN32)
       return _finite(value);
#elif defined(isfinite)
       return isfinite(value);
#else
       return value == value && (value == 0. || value * 2. != value);
#endif
}

Here is the caller graph for this function:

static double php_round_helper ( double  value,
int  mode 
) [inline, static]

Definition at line 109 of file math.c.

                                                              {
       double tmp_value;

       if (value >= 0.0) {
              tmp_value = floor(value + 0.5);
              if ((mode == PHP_ROUND_HALF_DOWN && value == (-0.5 + tmp_value)) ||
                     (mode == PHP_ROUND_HALF_EVEN && value == (0.5 + 2 * floor(tmp_value/2.0))) ||
                     (mode == PHP_ROUND_HALF_ODD  && value == (0.5 + 2 * floor(tmp_value/2.0) - 1.0)))
              {
                     tmp_value = tmp_value - 1.0;
              }
       } else {
              tmp_value = ceil(value - 0.5);
              if ((mode == PHP_ROUND_HALF_DOWN && value == (0.5 + tmp_value)) ||
                     (mode == PHP_ROUND_HALF_EVEN && value == (-0.5 + 2 * ceil(tmp_value/2.0))) ||
                     (mode == PHP_ROUND_HALF_ODD  && value == (-0.5 + 2 * ceil(tmp_value/2.0) + 1.0)))
              {
                     tmp_value = tmp_value + 1.0;
              }
       }

       return tmp_value;
}

Here is the caller graph for this function: