1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
|
/*
+----------------------------------------------------------------------+
| PHP HTML Embedded Scripting Language Version 3.0 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2000 PHP Development Team (See Credits file) |
+----------------------------------------------------------------------+
| This program is free software; you can redistribute it and/or modify |
| it under the terms of one of the following licenses: |
| |
| A) the GNU General Public License as published by the Free Software |
| Foundation; either version 2 of the License, or (at your option) |
| any later version. |
| |
| B) the PHP License as published by the PHP Development Team and |
| included in the distribution in the file: LICENSE |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of both licenses referred to here. |
| If you did not, or have any questions about PHP licensing, please |
| contact core@php.net. |
+----------------------------------------------------------------------+
| Authors: Jim Winstead (jimw@php.net) |
| Stig Sther Bakken <ssb@fast.no> |
+----------------------------------------------------------------------+
*/
/* $Id: math.c,v 1.55 2000/09/19 18:50:52 fmk Exp $ */
#include "php.h"
#include "internal_functions.h"
#include "phpmath.h"
#include "snprintf.h"
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
PHPAPI char *_php3_number_format(double, int, char ,char);
/* {{{ proto int abs(int number)
Return the absolute value of the number */
void php3_abs(INTERNAL_FUNCTION_PARAMETERS) {
pval *value;
TLS_VARS;
if (ARG_COUNT(ht)!=1||getParameters(ht,1,&value)==FAILURE) {
WRONG_PARAM_COUNT;
}
if (value->type == IS_STRING) {
convert_string_to_number(value);
}
if (value->type == IS_DOUBLE) {
RETURN_DOUBLE(fabs(value->value.dval));
}
else if (value->type == IS_LONG) {
RETURN_LONG(labs(value->value.lval));
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto double ceil(double number)
Returns the next highest integer value of the number */
void php3_ceil(INTERNAL_FUNCTION_PARAMETERS) {
pval *value;
TLS_VARS;
if (ARG_COUNT(ht)!=1||getParameters(ht,1,&value)==FAILURE) {
WRONG_PARAM_COUNT;
}
if (value->type == IS_STRING) {
convert_string_to_number(value);
}
if (value->type == IS_DOUBLE) {
RETURN_DOUBLE(ceil(value->value.dval));
}
else if (value->type == IS_LONG) {
RETURN_LONG(value->value.lval);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto double floor(double number)
Returns the next lowest integer value from the number */
void php3_floor(INTERNAL_FUNCTION_PARAMETERS) {
pval *value;
TLS_VARS;
if (ARG_COUNT(ht)!=1||getParameters(ht,1,&value)==FAILURE) {
WRONG_PARAM_COUNT;
}
if (value->type == IS_STRING) {
convert_string_to_number(value);
}
if (value->type == IS_DOUBLE) {
RETURN_DOUBLE(floor(value->value.dval));
}
else if (value->type == IS_LONG) {
RETURN_LONG(value->value.lval);
}
RETURN_FALSE;
}
/* }}} */
#ifndef HAVE_RINT
/* emulate rint */
inline double rint(double n)
{
double i, f;
f = modf(n, &i);
if (f > .5)
i++;
else if (f < -.5)
i--;
return i;
}
#endif
/* {{{ proto int round(double number)
Returns the rounded value of the number */
void php3_round(INTERNAL_FUNCTION_PARAMETERS)
{
pval *value;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &value) == FAILURE) {
WRONG_PARAM_COUNT;
}
if (value->type == IS_STRING) {
convert_string_to_number(value);
}
if (value->type == IS_DOUBLE) {
double d;
d=rint(value->value.dval);
if(d==0.0) d=0.0; /* workaround for rint() returning -0 instead of 0 */
RETURN_DOUBLE(d);
}
if (value->type == IS_LONG) {
RETURN_DOUBLE((double)value->value.lval);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ proto double sin(double number)
Returns the sine of the number in radians */
void php3_sin(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num);
return_value->value.dval = sin(num->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double cos(double number)
Returns the cosine of the number in radians */
void php3_cos(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num);
return_value->value.dval = cos(num->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double tan(double number)
Returns the tangent of the number in radians */
void php3_tan(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num);
return_value->value.dval = tan(num->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double asin(double number)
Returns the arc sine of the number in radians */
void php3_asin(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num);
return_value->value.dval = asin(num->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double acos(double number)
Return the arc cosine of the number in radians */
void php3_acos(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num);
return_value->value.dval = acos(num->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double atan(double number)
Returns the arc tangent of the number in radians */
void php3_atan(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num);
return_value->value.dval = atan(num->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double atan2(double y, double x)
Returns the arc tangent of y/x, with the resulting quadrant determined by the signs of y and x */
void php3_atan2(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num1, *num2;
if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &num1, &num2) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num1);
convert_to_double(num2);
return_value->value.dval = atan2(num1->value.dval,num2->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double pi(void)
Returns an approximation of pi */
void php3_pi(INTERNAL_FUNCTION_PARAMETERS)
{
return_value->value.dval = M_PI;
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double pow(double base, double exponent)
Returns base raised to the power of expopent */
void php3_pow(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num1, *num2;
TLS_VARS;
if (ARG_COUNT(ht) != 2 || getParameters(ht,2,&num1,&num2) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num1);
convert_to_double(num2);
RETURN_DOUBLE(pow(num1->value.dval, num2->value.dval));
}
/* }}} */
/* {{{ proto double exp(double number)
Returns e raised to the power of the number */
void php3_exp(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num);
return_value->value.dval = exp(num->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double log(double number)
Returns the natural logarithm of the number */
void php3_log(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num);
return_value->value.dval = log(num->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double log10(double number)
Returns the base-10 logarithm of the number */
void php3_log10(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num);
return_value->value.dval = log10(num->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double sqrt(double number)
Returns the square root of the number */
void php3_sqrt(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &num) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(num);
return_value->value.dval = sqrt(num->value.dval);
return_value->type = IS_DOUBLE;
}
/* }}} */
/* {{{ proto double deg2rad(double number)
Converts the number in degrees to the radian equivalent */
void php3_deg2rad(INTERNAL_FUNCTION_PARAMETERS)
{
pval *deg;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, °) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(deg);
RETVAL_DOUBLE((deg->value.dval / 180.0) * M_PI);
}
/* }}} */
/* {{{ proto double rad2deg(double number)
Converts the radian number to the equivalent number in degrees */
void php3_rad2deg(INTERNAL_FUNCTION_PARAMETERS)
{
pval *rad;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &rad) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_double(rad);
RETVAL_DOUBLE((rad->value.dval / M_PI) * 180);
}
/* }}} */
/*
* Convert a string representation of a base(2-36) number to a long.
*/
static long
_php3_basetolong(pval *arg, int base) {
long mult = 1, num = 0, digit;
int i;
char c, *s;
if (arg->type != IS_STRING || base < 2 || base > 36) {
return 0;
}
s = arg->value.str.val;
for (i = arg->value.str.len - 1; i >= 0; i--, mult *= base) {
c = toupper(s[i]);
if (c >= '0' && c <= '9') {
digit = (c - '0');
} else if (c >= 'A' && c <= 'Z') {
digit = (c - 'A' + 10);
} else {
continue;
}
if (digit >= base) {
continue;
}
num += mult * digit;
}
return num;
}
/*
* Convert a long to a string containing a base(2-36) representation of
* the number.
*/
static char *
_php3_longtobase(pval *arg, int base)
{
static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
char *result, *ptr, *ret;
int len, digit;
long value;
if (arg->type != IS_LONG || base < 2 || base > 36) {
return empty_string;
}
value = arg->value.lval;
/* allocates space for the longest possible result with the lowest base */
len = (sizeof(arg->value.lval) * 8) + 1;
result = emalloc((sizeof(arg->value.lval) * 8) + 1);
ptr = result + len - 1;
*ptr-- = '\0';
do {
digit = value % base;
*ptr = digits[digit];
value /= base;
}
while (ptr-- > result && value);
ptr++;
ret = estrdup(ptr);
efree(result);
return ret;
}
/* {{{ proto int bindec(string binary_number)
Returns the decimal equivalent of the binary number */
void php3_bindec(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
long ret;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg);
ret = _php3_basetolong(arg, 2);
RETVAL_LONG(ret);
}
/* }}} */
/* {{{ proto int hexdec(string hexadecimal_number)
Returns the decimal equivalent of the hexadecimal number */
void php3_hexdec(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
long ret;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg);
ret = _php3_basetolong(arg, 16);
RETVAL_LONG(ret);
}
/* }}} */
/* {{{ proto int octdec(string octal_number)
Returns the decimal equivalent of an octal string */
void php3_octdec(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
long ret;
TLS_VARS;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(arg);
ret = _php3_basetolong(arg, 8);
RETVAL_LONG(ret);
}
/* }}} */
/* {{{ proto string decbin(int decimal_number)
Returns a string containing a binary representation of the number */
void php3_decbin(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
char *result;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
result = _php3_longtobase(arg, 2);
return_value->type = IS_STRING;
return_value->value.str.len = strlen(result);
return_value->value.str.val = result;
}
/* }}} */
/* {{{ proto string decoct(int decimal_number)
Returns a string containing an octal representation of the given number */
void php3_decoct(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
char *result;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
result = _php3_longtobase(arg, 8);
return_value->type = IS_STRING;
return_value->value.str.len = strlen(result);
return_value->value.str.val = result;
}
/* }}} */
/* {{{ proto string dechex(int decimal_number)
Returns a string containing a hexadecimal representation of the given number */
void php3_dechex(INTERNAL_FUNCTION_PARAMETERS)
{
pval *arg;
char *result;
if (ARG_COUNT(ht) != 1 || getParameters(ht, 1, &arg) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_long(arg);
result = _php3_longtobase(arg, 16);
return_value->type = IS_STRING;
return_value->value.str.len = strlen(result);
return_value->value.str.val = result;
}
/* }}} */
/* {{{ proto string base_convert(string number, int frombase, int tobase)
Converts a number in a string from any base <= 36 to any base <= 36.
*/
void php3_base_convert(INTERNAL_FUNCTION_PARAMETERS)
{
pval *number, *frombase, *tobase, temp;
char *result;
if (ARG_COUNT(ht) != 3 || getParameters(ht, 3, &number, &frombase, &tobase)
== FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string(number);
convert_to_long(frombase);
convert_to_long(tobase);
if (frombase->value.lval < 2 || frombase->value.lval > 36) {
php3_error(E_WARNING, "base_convert: invalid `from base' (%d)",
frombase->value.lval);
RETURN_FALSE;
}
if (tobase->value.lval < 2 || tobase->value.lval > 36) {
php3_error(E_WARNING, "base_convert: invalid `to base' (%d)",
tobase->value.lval);
RETURN_FALSE;
}
temp.type = IS_LONG;
temp.value.lval = _php3_basetolong(number, frombase->value.lval);
result = _php3_longtobase(&temp, tobase->value.lval);
RETVAL_STRING(result, 0);
} /* }}} */
PHPAPI char *_php3_number_format(double d,int dec,char dec_point,char thousand_sep)
{
char *tmpbuf,*resbuf;
char *s,*t; /* source, target */
int tmplen,reslen=0;
int count=0;
int is_negative=0;
if (d<0) {
is_negative=1;
d = -d;
}
dec = MAX(0,dec);
tmpbuf = (char *) emalloc(32+dec);
tmplen=_php3_sprintf(tmpbuf,"%.*f",dec,d);
if (!isdigit(tmpbuf[0])) {
return tmpbuf;
}
for (t=tmpbuf; *t; t++) {
if (*t=='.') {
*t = dec_point;
}
}
if (dec) {
reslen = dec+1 + (tmplen-dec-1) + (tmplen-1-dec-1)/3;
} else {
reslen = tmplen+(tmplen-1)/3;
}
if (is_negative) {
reslen++;
}
resbuf = (char *) emalloc(reslen+1);
s = tmpbuf+tmplen-1;
t = resbuf+reslen;
*t-- = 0;
if (dec) {
while (*s!=dec_point) {
*t-- = *s--;
}
*t-- = *s--; /* copy that dot */
}
while(s>=tmpbuf) {
*t-- = *s--;
if ((++count%3)==0 && s>=tmpbuf) {
*t-- = thousand_sep;
}
}
if (is_negative) {
*t-- = '-';
}
efree(tmpbuf);
return resbuf;
}
/* {{{ proto string number_format(double number [, int num_decimal_places [, string dec_separator, string thousands_separator]])
Formats a number with grouped thousands */
void php3_number_format(INTERNAL_FUNCTION_PARAMETERS)
{
pval *num,*dec,*t_s,*d_p;
char thousand_sep=',', dec_point='.';
switch(ARG_COUNT(ht)) {
case 1:
if (getParameters(ht, 1, &num)==FAILURE) {
RETURN_FALSE;
}
convert_to_double(num);
RETURN_STRING(_php3_number_format(num->value.dval,0,dec_point,thousand_sep),0);
break;
case 2:
if (getParameters(ht, 2, &num, &dec)==FAILURE) {
RETURN_FALSE;
}
convert_to_double(num);
convert_to_long(dec);
RETURN_STRING(_php3_number_format(num->value.dval,dec->value.lval,dec_point,thousand_sep),0);
break;
case 4:
if (getParameters(ht, 4, &num, &dec, &d_p, &t_s)==FAILURE) {
RETURN_FALSE;
}
convert_to_double(num);
convert_to_long(dec);
convert_to_string(d_p);
convert_to_string(t_s);
if (d_p->value.str.len==1) {
dec_point=d_p->value.str.val[0];
}
if (t_s->value.str.len==1) {
thousand_sep=t_s->value.str.val[0];
}
RETURN_STRING(_php3_number_format(num->value.dval,dec->value.lval,dec_point,thousand_sep),0);
break;
default:
WRONG_PARAM_COUNT;
break;
}
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
*/
|