[PHP-CVS] cvs: php-src /ext/standard math.c
iliaa Wed Oct 29 20:17:43 2008 UTC Modified files: /php-src/ext/standard math.c Log: MFB: Fixed bug #42294 (Unified solution for round() based on C99 round). http://cvs.php.net/viewvc.cgi/php-src/ext/standard/math.c?r1=1.147r2=1.148diff_format=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.147 php-src/ext/standard/math.c:1.148 --- php-src/ext/standard/math.c:1.147 Tue Jul 1 08:17:19 2008 +++ php-src/ext/standard/math.c Wed Oct 29 20:17:43 2008 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.147 2008/07/01 08:17:19 tony2001 Exp $ */ +/* $Id: math.c,v 1.148 2008/10/29 20:17:43 iliaa Exp $ */ #include php.h #include php_math.h @@ -29,25 +29,59 @@ #include float.h #include stdlib.h -#ifndef PHP_ROUND_FUZZ -# ifndef PHP_WIN32 -# define PHP_ROUND_FUZZ 0.501 -# else -# define PHP_ROUND_FUZZ 0.5 -# endif -#endif +/* + * Pertains to some of the code found in the php_round() function + * Ref: http://www.freebsd.org/cgi/query-pr.cgi?pr=59797 + * + * Copyright (c) 2003, Steven G. Kargl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + *notice unmodified, this list of conditions, and the following + *disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + *notice, this list of conditions and the following disclaimer in the + *documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +static double php_round(double val, int places) { + double t; + double f = pow(10.0, (double) places); + double x = val * f; + + if (zend_isinf(x) || zend_isnan(x)) { + return val; + } + + if (x = 0.0) { + t = ceil(x); + if ((t - x) 0.501) { + t -= 1.0; + } + } else { + t = ceil(-x); + if ((t x) 0.501) { + t -= 1.0; + } + t = -t; + } + x = t / f; -#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; \ -} \ + return !zend_isnan(x) ? x : t; +} /* {{{ php_asinh */ @@ -204,7 +238,7 @@ return_val = (Z_TYPE_P(value) == IS_LONG) ? (double)Z_LVAL_P(value) : Z_DVAL_P(value); - PHP_ROUND_WITH_FUZZ(return_val, places); + return_val = php_round(return_val, places); RETURN_DOUBLE(return_val); break; @@ -968,7 +1002,7 @@ } dec = MAX(0, dec); - PHP_ROUND_WITH_FUZZ(d, dec); + d = php_round(d, dec); tmplen = spprintf(tmpbuf, 0, %.*f, dec, d); -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
felixdv Wed Oct 29 21:43:30 2008 UTC Modified files: /php-src/ext/standard math.c Log: Fix typo. http://cvs.php.net/viewvc.cgi/php-src/ext/standard/math.c?r1=1.148r2=1.149diff_format=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.148 php-src/ext/standard/math.c:1.149 --- php-src/ext/standard/math.c:1.148 Wed Oct 29 20:17:43 2008 +++ php-src/ext/standard/math.c Wed Oct 29 21:43:30 2008 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.148 2008/10/29 20:17:43 iliaa Exp $ */ +/* $Id: math.c,v 1.149 2008/10/29 21:43:30 felixdv Exp $ */ #include php.h #include php_math.h @@ -73,7 +73,7 @@ } } else { t = ceil(-x); - if ((t x) 0.501) { + if ((t + x) 0.501) { t -= 1.0; } t = -t; -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
tony2001Tue Jul 1 08:17:19 2008 UTC Modified files: /php-src/ext/standard math.c Log: int - long http://cvs.php.net/viewvc.cgi/php-src/ext/standard/math.c?r1=1.146r2=1.147diff_format=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.146 php-src/ext/standard/math.c:1.147 --- php-src/ext/standard/math.c:1.146 Tue May 6 10:57:54 2008 +++ php-src/ext/standard/math.c Tue Jul 1 08:17:19 2008 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.146 2008/05/06 10:57:54 kalle Exp $ */ +/* $Id: math.c,v 1.147 2008/07/01 08:17:19 tony2001 Exp $ */ #include php.h #include php_math.h @@ -1068,7 +1068,7 @@ { zval *sep1 = NULL, *sep2 = NULL; double num; - int dec = 0; + long dec = 0; char thousand_sep=',', dec_point='.'; char *tmp; -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
kalle Tue May 6 10:57:54 2008 UTC Modified files: /php-src/ext/standard math.c Log: Changed php_*h() + php_log1p() + php_expm1() to be static http://cvs.php.net/viewvc.cgi/php-src/ext/standard/math.c?r1=1.145r2=1.146diff_format=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.145 php-src/ext/standard/math.c:1.146 --- php-src/ext/standard/math.c:1.145 Mon May 5 06:28:03 2008 +++ php-src/ext/standard/math.c Tue May 6 10:57:54 2008 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.145 2008/05/05 06:28:03 kalle Exp $ */ +/* $Id: math.c,v 1.146 2008/05/06 10:57:54 kalle Exp $ */ #include php.h #include php_math.h @@ -51,7 +51,7 @@ /* {{{ php_asinh */ -double php_asinh(double z) +static double php_asinh(double z) { #ifdef HAVE_ASINH return(asinh(z)); @@ -63,7 +63,7 @@ /* {{{ php_acosh */ -double php_acosh(double x) +static double php_acosh(double x) { #ifdef HAVE_ACOSH return(acosh(x)); @@ -75,7 +75,7 @@ /* {{{ php_atanh */ -double php_atanh(double z) +static double php_atanh(double z) { #ifdef HAVE_ATANH return(atanh(z)); @@ -87,7 +87,7 @@ /* {{{ php_log1p */ -double php_log1p(double x) +static double php_log1p(double x) { #ifdef HAVE_LOG1P return(log1p(x)); @@ -99,7 +99,7 @@ /* {{{ php_expm1 */ -double php_expm1(double x) +static double php_expm1(double x) { #if !defined(PHP_WIN32) !defined(NETWARE) return(expm1(x)); -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
tony2001Wed Dec 27 19:07:28 2006 UTC Modified files: /php-src/ext/standard math.c Log: NULL is a special value for number_format() meaning use default separator http://cvs.php.net/viewvc.cgi/php-src/ext/standard/math.c?r1=1.140r2=1.141diff_format=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.140 php-src/ext/standard/math.c:1.141 --- php-src/ext/standard/math.c:1.140 Fri Dec 22 21:18:30 2006 +++ php-src/ext/standard/math.c Wed Dec 27 19:07:28 2006 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.140 2006/12/22 21:18:30 andrei Exp $ */ +/* $Id: math.c,v 1.141 2006/12/27 19:07:28 tony2001 Exp $ */ #include php.h #include php_math.h @@ -1018,30 +1018,30 @@ Formats a number with grouped thousands */ PHP_FUNCTION(number_format) { - char *sep1 = NULL, *sep2 = NULL; - int sep1_len, sep2_len; + zval *sep1 = NULL, *sep2 = NULL; double num; int dec = 0; char thousand_sep=',', dec_point='.'; char *tmp; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, d|lss, num, dec, - sep1, sep1_len, UG(ascii_conv), - sep2, sep2_len, UG(ascii_conv)) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, d|lzz, num, dec, sep1, sep2) == FAILURE) { return; } - if (sep1) { - if (sep1_len = 1) { - dec_point = sep1[0]; - } else if (sep1_len == 0) { + if (sep1 Z_TYPE_P(sep1) != IS_NULL) { + convert_to_string_with_converter(sep1, UG(ascii_conv)); + if (Z_STRLEN_P(sep1)) { + dec_point = Z_STRVAL_P(sep1)[0]; + } else { dec_point = 0; } } - if (sep2) { - if (sep2_len = 1) { - thousand_sep = sep2[0]; - } else if (sep2_len == 0) { + + if (sep2 Z_TYPE_P(sep2) != IS_NULL) { + convert_to_string_with_converter(sep2, UG(ascii_conv)); + if (Z_STRLEN_P(sep2)) { + thousand_sep = Z_STRVAL_P(sep2)[0]; + } else { thousand_sep = 0; } } -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-CVS] cvs: php-src /ext/standard math.c
On Dec 27, 2006, at 11:07 AM, Antony Dovgal wrote: - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, d|lss, num, dec, - sep1, sep1_len, UG(ascii_conv), - sep2, sep2_len, UG(ascii_conv)) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, d|lzz, num, dec, sep1, sep2) == FAILURE) { return; } Why not use '!' modifier to say that the arg can be NULL? The the code below is not necessary. - if (sep1) { - if (sep1_len = 1) { - dec_point = sep1[0]; - } else if (sep1_len == 0) { + if (sep1 Z_TYPE_P(sep1) != IS_NULL) { + convert_to_string_with_converter(sep1, UG(ascii_conv)); + if (Z_STRLEN_P(sep1)) { + dec_point = Z_STRVAL_P(sep1)[0]; + } else { dec_point = 0; If you don't want to use '!', at least check for return value of convert_to_string_with_converter() and fail, if needed. -Andrei -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
andrei Fri Dec 22 21:18:30 2006 UTC Modified files: /php-src/ext/standard math.c Log: Unicode support in number_format(). http://cvs.php.net/viewvc.cgi/php-src/ext/standard/math.c?r1=1.139r2=1.140diff_format=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.139 php-src/ext/standard/math.c:1.140 --- php-src/ext/standard/math.c:1.139 Fri Dec 22 04:03:35 2006 +++ php-src/ext/standard/math.c Fri Dec 22 21:18:30 2006 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.139 2006/12/22 04:03:35 iliaa Exp $ */ +/* $Id: math.c,v 1.140 2006/12/22 21:18:30 andrei Exp $ */ #include php.h #include php_math.h @@ -1014,71 +1014,40 @@ } /* }}} */ -/* {{{ proto string number_format(float number [, int num_decimal_places [, string dec_seperator, string thousands_seperator]]) +/* {{{ proto string number_format(float number [, int num_decimal_places [, string dec_seperator, string thousands_seperator]]) U Formats a number with grouped thousands */ PHP_FUNCTION(number_format) { - zval **num, **dec, **t_s, **d_p; + char *sep1 = NULL, *sep2 = NULL; + int sep1_len, sep2_len; + double num; + int dec = 0; char thousand_sep=',', dec_point='.'; char *tmp; - switch(ZEND_NUM_ARGS()) { - case 1: - if (zend_get_parameters_ex(1, num)==FAILURE) { - RETURN_FALSE; - } - convert_to_double_ex(num); - tmp = _php_math_number_format(Z_DVAL_PP(num), 0, dec_point, thousand_sep); - RETVAL_RT_STRING(tmp, 0); - if (UG(unicode)) { - efree(tmp); - } + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, d|lss, num, dec, + sep1, sep1_len, UG(ascii_conv), + sep2, sep2_len, UG(ascii_conv)) == FAILURE) { return; - case 2: - if (zend_get_parameters_ex(2, num, dec)==FAILURE) { - RETURN_FALSE; - } - convert_to_double_ex(num); - convert_to_long_ex(dec); - tmp = _php_math_number_format(Z_DVAL_PP(num), Z_LVAL_PP(dec), dec_point, thousand_sep); - RETVAL_RT_STRING(tmp, 0); - if (UG(unicode)) { - efree(tmp); - } - return; - case 4: - if (zend_get_parameters_ex(4, num, dec, d_p, t_s)==FAILURE) { - RETURN_FALSE; - } - convert_to_double_ex(num); - convert_to_long_ex(dec); + } - if (Z_TYPE_PP(d_p) != IS_NULL) { - convert_to_string_ex(d_p); - if (Z_STRLEN_PP(d_p)=1) { - dec_point=Z_STRVAL_PP(d_p)[0]; - } else if (Z_STRLEN_PP(d_p)==0) { - dec_point=0; - } - } - if (Z_TYPE_PP(t_s) != IS_NULL) { - convert_to_string_ex(t_s); - if (Z_STRLEN_PP(t_s)=1) { - thousand_sep=Z_STRVAL_PP(t_s)[0]; - } else if(Z_STRLEN_PP(t_s)==0) { - thousand_sep=0; - } + if (sep1) { + if (sep1_len = 1) { + dec_point = sep1[0]; + } else if (sep1_len == 0) { + dec_point = 0; } - tmp = _php_math_number_format(Z_DVAL_PP(num), Z_LVAL_PP(dec), dec_point, thousand_sep); - RETVAL_RT_STRING(tmp, 0); - if (UG(unicode)) { - efree(tmp); + } + if (sep2) { + if (sep2_len = 1) { + thousand_sep = sep2[0]; + } else if (sep2_len == 0) { + thousand_sep = 0; } - return; - default: - WRONG_PARAM_COUNT; - break; } + + tmp = _php_math_number_format(num, dec, dec_point, thousand_sep); + RETVAL_ASCII_STRING(tmp, ZSTR_AUTOFREE); } /* }}} */ -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c /ext/standard/tests/strings bug29538.phpt
helly Sun Jul 16 10:49:06 2006 UTC Added files: /php-src/ext/standard/tests/strings bug29538.phpt Modified files: /php-src/ext/standard math.c Log: - Fixed Bug #29538 number_format and problem with 0 http://cvs.php.net/viewvc.cgi/php-src/ext/standard/math.c?r1=1.134r2=1.135diff_format=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.134 php-src/ext/standard/math.c:1.135 --- php-src/ext/standard/math.c:1.134 Mon Feb 6 11:28:20 2006 +++ php-src/ext/standard/math.c Sun Jul 16 10:49:06 2006 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.134 2006/02/06 11:28:20 tony2001 Exp $ */ +/* $Id: math.c,v 1.135 2006/07/16 10:49:06 helly Exp $ */ #include php.h #include php_math.h @@ -976,13 +976,8 @@ is_negative = 1; d = -d; } - if (!dec_point dec 0) { - d *= pow(10, dec); - dec = 0; - } else { - dec = MAX(0, dec); - } + dec = MAX(0, dec); PHP_ROUND_WITH_FUZZ(d, dec); tmplen = spprintf(tmpbuf, 0, %.*f, dec, d); @@ -991,8 +986,10 @@ return tmpbuf; } + /* find decimal point, if expected */ + dp = dec ? strchr(tmpbuf, '.') : NULL; + /* calculate the length of the return buffer */ - dp = strchr(tmpbuf, '.'); if (dp) { integral = dp - tmpbuf; } else { @@ -1008,7 +1005,11 @@ reslen = integral; if (dec) { - reslen += 1 + dec; + reslen += dec; + + if (dec_point) { + reslen++; + } } /* add a byte for minus sign */ @@ -1025,29 +1026,29 @@ * Take care, as the sprintf implementation may return less places than * we requested due to internal buffer limitations */ if (dec) { - int declen = dp ? strlen(dp+1) : 0; - int topad = declen 0 ? dec - declen : 0; + int declen = dp ? s - dp : 0; + int topad = dec declen ? dec - declen : 0; /* pad with '0's */ - while (topad--) { *t-- = '0'; } if (dp) { - /* now copy the chars after the point */ - memcpy(t - declen + 1, dp + 1, declen); - + s -= declen + 1; /* +1 to skip the point */ t -= declen; - s -= declen; + + /* now copy the chars after the point */ + memcpy(t + 1, dp + 1, declen); } /* add decimal point */ - *t-- = dec_point; - s--; + if (dec_point) { + *t-- = dec_point; + } } - /* copy the numbers before the decimal place, adding thousand + /* copy the numbers before the decimal point, adding thousand * separator every three digits */ while(s = tmpbuf) { *t-- = *s--; http://cvs.php.net/viewvc.cgi/php-src/ext/standard/tests/strings/bug29538.phpt?view=markuprev=1.1 Index: php-src/ext/standard/tests/strings/bug29538.phpt +++ php-src/ext/standard/tests/strings/bug29538.phpt --TEST-- Bug #29538 (number_format and problem with 0) --FILE-- ?php echo number_format(0.25, 2, '', ''), \n; echo number_format(1234, 2, '', ','); ? --EXPECT-- 025 1,23400 -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
andrey Sat May 7 08:41:57 2005 EDT Modified files: /php-src/ext/standard math.c Log: update protos http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.125r2=1.126ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.125 php-src/ext/standard/math.c:1.126 --- php-src/ext/standard/math.c:1.125 Thu May 5 19:42:03 2005 +++ php-src/ext/standard/math.c Sat May 7 08:41:56 2005 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.125 2005/05/05 23:42:03 sniper Exp $ */ +/* $Id: math.c,v 1.126 2005/05/07 12:41:56 andrey Exp $ */ #include php.h #include php_math.h @@ -1214,7 +1214,7 @@ return (vr / elements_num); } -/* {{{ proto float math_variance(array a) +/* {{{ proto float math_variance(array a[, bool sample = false]) Returns the population variance */ PHP_FUNCTION(math_variance) { @@ -1237,7 +1237,7 @@ /* }}} */ -/* {{{ proto float math_std_dev(array a) +/* {{{ proto float math_std_dev(array a[, bool sample = false]) Returns the standard deviation */ PHP_FUNCTION(math_std_dev) { -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
sniper Sat May 7 12:08:03 2005 EDT Modified files: /php-src/ext/standard math.c Log: ws + cs + proto fixes http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.126r2=1.127ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.126 php-src/ext/standard/math.c:1.127 --- php-src/ext/standard/math.c:1.126 Sat May 7 08:41:56 2005 +++ php-src/ext/standard/math.c Sat May 7 12:08:03 2005 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.126 2005/05/07 12:41:56 andrey Exp $ */ +/* $Id: math.c,v 1.127 2005/05/07 16:08:03 sniper Exp $ */ #include php.h #include php_math.h @@ -55,12 +55,11 @@ /* {{{ proto int abs(int number) Return the absolute value of the number */ - PHP_FUNCTION(abs) { zval **value; - if (ZEND_NUM_ARGS()!=1||zend_get_parameters_ex(1, value)==FAILURE) { + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, value) == FAILURE) { WRONG_PARAM_COUNT; } @@ -75,18 +74,17 @@ RETURN_LONG(Z_LVAL_PP(value) 0 ? -Z_LVAL_PP(value) : Z_LVAL_PP(value)); } } - RETURN_FALSE; } - /* }}} */ + /* {{{ proto float ceil(float number) Returns the next highest integer value of the number */ PHP_FUNCTION(ceil) { zval **value; - if (ZEND_NUM_ARGS()!=1||zend_get_parameters_ex(1, value)==FAILURE) { + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, value) == FAILURE) { WRONG_PARAM_COUNT; } @@ -101,15 +99,15 @@ RETURN_FALSE; } - /* }}} */ + /* {{{ proto float floor(float number) Returns the next lowest integer value from the number */ PHP_FUNCTION(floor) { zval **value; - if (ZEND_NUM_ARGS()!=1||zend_get_parameters_ex(1, value)==FAILURE) { + if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, value) == FAILURE) { WRONG_PARAM_COUNT; } @@ -124,10 +122,8 @@ RETURN_FALSE; } - /* }}} */ - /* {{{ proto float round(float number [, int precision]) Returns the number rounded to specified precision */ PHP_FUNCTION(round) @@ -171,9 +167,9 @@ } } /* }}} */ + /* {{{ proto float sin(float number) Returns the sine of the number in radians */ - PHP_FUNCTION(sin) { zval **num; @@ -185,11 +181,10 @@ Z_DVAL_P(return_value) = sin(Z_DVAL_PP(num)); Z_TYPE_P(return_value) = IS_DOUBLE; } - /* }}} */ + /* {{{ proto float cos(float number) Returns the cosine of the number in radians */ - PHP_FUNCTION(cos) { zval **num; @@ -202,6 +197,7 @@ Z_TYPE_P(return_value) = IS_DOUBLE; } /* }}} */ + /* {{{ proto float tan(float number) Returns the tangent of the number in radians */ PHP_FUNCTION(tan) @@ -215,11 +211,10 @@ Z_DVAL_P(return_value) = tan(Z_DVAL_PP(num)); Z_TYPE_P(return_value) = IS_DOUBLE; } - /* }}} */ + /* {{{ proto float asin(float number) Returns the arc sine of the number in radians */ - PHP_FUNCTION(asin) { zval **num; @@ -231,11 +226,10 @@ Z_DVAL_P(return_value) = asin(Z_DVAL_PP(num)); Z_TYPE_P(return_value) = IS_DOUBLE; } - /* }}} */ + /* {{{ proto float acos(float number) Return the arc cosine of the number in radians */ - PHP_FUNCTION(acos) { zval **num; @@ -247,11 +241,10 @@ Z_DVAL_P(return_value) = acos(Z_DVAL_PP(num)); Z_TYPE_P(return_value) = IS_DOUBLE; } - /* }}} */ + /* {{{ proto float atan(float number) Returns the arc tangent of the number in radians */ - PHP_FUNCTION(atan) { zval **num; @@ -263,11 +256,10 @@ Z_DVAL_P(return_value) = atan(Z_DVAL_PP(num)); Z_TYPE_P(return_value) = IS_DOUBLE; } - /* }}} */ + /* {{{ proto float atan2(float y, float x) Returns the arc tangent of y/x, with the resulting quadrant determined by the signs of y and x */ - PHP_FUNCTION(atan2) { zval **num1, **num2; @@ -280,11 +272,10 @@ Z_DVAL_P(return_value) = atan2(Z_DVAL_PP(num1), Z_DVAL_PP(num2)); Z_TYPE_P(return_value) = IS_DOUBLE; } - /* }}} */ + /* {{{ proto float sinh(float number) Returns the hyperbolic sine of the number, defined as (exp(number) - exp(-number))/2 */ - PHP_FUNCTION(sinh) { zval **num; @@ -296,11 +287,10 @@ Z_DVAL_P(return_value) = sinh(Z_DVAL_PP(num)); Z_TYPE_P(return_value) = IS_DOUBLE; } - /* }}} */ + /* {{{ proto float cosh(float number) Returns the hyperbolic cosine of the number, defined as (exp(number) + exp(-number))/2 */ - PHP_FUNCTION(cosh) { zval **num; @@ -313,6 +303,7 @@ Z_TYPE_P(return_value) = IS_DOUBLE; } /* }}} */ + /* {{{ proto float tanh(float number) Returns the hyperbolic tangent of the number, defined as sinh(number)/cosh(number) */ PHP_FUNCTION(tanh) @@ -326,7 +317,6 @@
[PHP-CVS] cvs: php-src /ext/standard math.c
sniper Thu May 5 19:42:03 2005 EDT Modified files: /php-src/ext/standard math.c Log: fix proto http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.124r2=1.125ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.124 php-src/ext/standard/math.c:1.125 --- php-src/ext/standard/math.c:1.124 Mon May 2 08:32:21 2005 +++ php-src/ext/standard/math.c Thu May 5 19:42:03 2005 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.124 2005/05/02 12:32:21 andrey Exp $ */ +/* $Id: math.c,v 1.125 2005/05/05 23:42:03 sniper Exp $ */ #include php.h #include php_math.h @@ -1215,7 +1215,7 @@ } /* {{{ proto float math_variance(array a) - Returns the standard deviation */ + Returns the population variance */ PHP_FUNCTION(math_variance) { zval *arr; -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c /ext/standard/tests/math math_std_dev.phpt
andrey Mon May 2 07:01:14 2005 EDT Modified files: /php-src/ext/standard math.c /php-src/ext/standard/tests/mathmath_std_dev.phpt Log: fix division by zero. throw an warning if the array is empty. http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.120r2=1.121ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.120 php-src/ext/standard/math.c:1.121 --- php-src/ext/standard/math.c:1.120 Mon May 2 05:17:49 2005 +++ php-src/ext/standard/math.c Mon May 2 07:01:13 2005 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.120 2005/05/02 09:17:49 andrey Exp $ */ +/* $Id: math.c,v 1.121 2005/05/02 11:01:13 andrey Exp $ */ #include php.h #include php_math.h @@ -1195,6 +1195,10 @@ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, a, arr) == FAILURE) { return; } + if (zend_hash_num_elements(Z_ARRVAL_P(arr)) == 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, The array has zero elements); + RETURN_FALSE; + } zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), pos); while (zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)entry, pos) == SUCCESS) { convert_to_double_ex(entry); http://cvs.php.net/diff.php/php-src/ext/standard/tests/math/math_std_dev.phpt?r1=1.1r2=1.2ty=u Index: php-src/ext/standard/tests/math/math_std_dev.phpt diff -u php-src/ext/standard/tests/math/math_std_dev.phpt:1.1 php-src/ext/standard/tests/math/math_std_dev.phpt:1.2 --- php-src/ext/standard/tests/math/math_std_dev.phpt:1.1 Mon May 2 05:17:49 2005 +++ php-src/ext/standard/tests/math/math_std_dev.phpt Mon May 2 07:01:14 2005 @@ -5,6 +5,10 @@ $a=array(4, 1, 7); $dev=math_std_dev($a); var_dump(sprintf(%2.9f, $dev)); +var_dump(math_std_dev(array())); ? ---EXPECT-- -string(11) 2.449489743 \ No newline at end of file +--EXPECTF-- +string(11) 2.449489743 + +Warning: math_std_dev(): The array has zero elements in %s on line %d +bool(false) \ No newline at end of file -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
andrey Mon May 2 08:32:22 2005 EDT Modified files: /php-src/ext/standard math.c Log: fixed div by zero #div by zero is bad...div by zero is bad...bad http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.123r2=1.124ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.123 php-src/ext/standard/math.c:1.124 --- php-src/ext/standard/math.c:1.123 Mon May 2 08:29:37 2005 +++ php-src/ext/standard/math.c Mon May 2 08:32:21 2005 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.123 2005/05/02 12:29:37 andrey Exp $ */ +/* $Id: math.c,v 1.124 2005/05/02 12:32:21 andrey Exp $ */ #include php.h #include php_math.h @@ -1228,6 +1228,10 @@ php_error_docref(NULL TSRMLS_CC, E_WARNING, The array has zero elements); RETURN_FALSE; } + if (sample zend_hash_num_elements(Z_ARRVAL_P(arr)) == 1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, The array has only 1 element); + RETURN_FALSE; + } RETURN_DOUBLE(php_population_variance(arr, sample)); } /* }}} */ @@ -1247,6 +1251,10 @@ php_error_docref(NULL TSRMLS_CC, E_WARNING, The array has zero elements); RETURN_FALSE; } + if (sample zend_hash_num_elements(Z_ARRVAL_P(arr)) == 1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, The array has only 1 element); + RETURN_FALSE; + } RETURN_DOUBLE(sqrt(php_population_variance(arr, sample))); } /* }}} */ -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
iliaa Mon Dec 13 19:37:20 2004 EDT Modified files: /php-src/ext/standard math.c Log: Fixed bug #28228 (NULL decimal separator is not being handled correctly). http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.118r2=1.119ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.118 php-src/ext/standard/math.c:1.119 --- php-src/ext/standard/math.c:1.118 Mon Sep 20 18:08:06 2004 +++ php-src/ext/standard/math.c Mon Dec 13 19:37:19 2004 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.118 2004/09/20 22:08:06 wez Exp $ */ +/* $Id: math.c,v 1.119 2004/12/14 00:37:19 iliaa Exp $ */ #include php.h #include php_math.h @@ -1141,17 +1141,22 @@ } convert_to_double_ex(num); convert_to_long_ex(dec); - convert_to_string_ex(d_p); - convert_to_string_ex(t_s); - if (Z_STRLEN_PP(d_p)==1) { - dec_point=Z_STRVAL_PP(d_p)[0]; - } else if (Z_STRLEN_PP(d_p)==0) { - dec_point=0; + + if (Z_TYPE_PP(d_p) != IS_NULL) { + convert_to_string_ex(d_p); + if (Z_STRLEN_PP(d_p)==1) { + dec_point=Z_STRVAL_PP(d_p)[0]; + } else if (Z_STRLEN_PP(d_p)==0) { + dec_point=0; + } } - if (Z_STRLEN_PP(t_s)==1) { - thousand_sep=Z_STRVAL_PP(t_s)[0]; - } else if(Z_STRLEN_PP(t_s)==0) { - thousand_sep=0; + if (Z_TYPE_PP(t_s) != IS_NULL) { + convert_to_string_ex(t_s); + if (Z_STRLEN_PP(t_s)==1) { + thousand_sep=Z_STRVAL_PP(t_s)[0]; + } else if(Z_STRLEN_PP(t_s)==0) { + thousand_sep=0; + } } RETURN_STRING(_php_math_number_format(Z_DVAL_PP(num), Z_LVAL_PP(dec), dec_point, thousand_sep), 0); break; -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
iliaa Sun Aug 8 17:26:29 2004 EDT Modified files: /php-src/ext/standard math.c Log: MFB: removed \r characters. http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.116r2=1.117ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.116 php-src/ext/standard/math.c:1.117 --- php-src/ext/standard/math.c:1.116 Mon Jul 19 03:19:44 2004 +++ php-src/ext/standard/math.c Sun Aug 8 17:26:29 2004 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.116 2004/07/19 07:19:44 andi Exp $ */ +/* $Id: math.c,v 1.117 2004/08/08 21:26:29 iliaa Exp $ */ #include php.h #include php_math.h @@ -448,34 +448,34 @@ 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; - - 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); - } + 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; + + 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); -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
iliaa Thu Jun 10 15:38:24 2004 EDT Modified files: /php-src/ext/standard math.c Log: hypot() is not experimental. http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.114r2=1.115ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.114 php-src/ext/standard/math.c:1.115 --- php-src/ext/standard/math.c:1.114 Mon May 24 13:02:11 2004 +++ php-src/ext/standard/math.c Thu Jun 10 15:38:24 2004 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.114 2004/05/24 17:02:11 iliaa Exp $ */ +/* $Id: math.c,v 1.115 2004/06/10 19:38:24 iliaa Exp $ */ #include php.h #include php_math.h @@ -616,11 +616,6 @@ /* {{{ proto float hypot(float num1, float num2) Returns sqrt(num1*num1 + num2*num2) */ - -/* - WARNING: this function is expermental: it could change its name or - disappear in the next version of PHP! -*/ #ifdef HAVE_HYPOT PHP_FUNCTION(hypot) -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c /ext/standard/tests/math bug28228.phpt
iliaa Fri Apr 30 09:26:01 2004 EDT Added files: /php-src/ext/standard/tests/mathbug28228.phpt Modified files: /php-src/ext/standard math.c Log: Fixed bug #28228 (number_format() does not allow empty decimal separator). http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.112r2=1.113ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.112 php-src/ext/standard/math.c:1.113 --- php-src/ext/standard/math.c:1.112 Wed Mar 10 10:59:50 2004 +++ php-src/ext/standard/math.c Fri Apr 30 09:26:01 2004 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.112 2004/03/10 15:59:50 abies Exp $ */ +/* $Id: math.c,v 1.113 2004/04/30 13:26:01 iliaa Exp $ */ #include php.h #include php_math.h @@ -1020,7 +1020,12 @@ is_negative = 1; d = -d; } - dec = MAX(0, dec); + if (!dec_point dec 0) { + d *= pow(10, dec); + dec = 0; + } else { + dec = MAX(0, dec); + } PHP_ROUND_WITH_FUZZ(d, dec); @@ -1140,6 +1145,8 @@ convert_to_string_ex(t_s); if (Z_STRLEN_PP(d_p)==1) { dec_point=Z_STRVAL_PP(d_p)[0]; + } else if (Z_STRLEN_PP(d_p)==0) { + dec_point=0; } if (Z_STRLEN_PP(t_s)==1) { thousand_sep=Z_STRVAL_PP(t_s)[0]; http://cvs.php.net/co.php/php-src/ext/standard/tests/math/bug28228.phpt?r=1.1p=1 Index: php-src/ext/standard/tests/math/bug28228.phpt +++ php-src/ext/standard/tests/math/bug28228.phpt --TEST-- Bug #28228 (number_format() does not allow empty decimal separator) --FILE-- ?php echo number_format(1234.5678, 4, '', '') . \n; echo number_format(1234.5678, 4, NULL, ',') . \n; echo number_format(1234.5678, 4, 0, ',') . \n; echo number_format(1234.5678, 4); ? --EXPECT-- 12345678 12,345,678 1,23405678 1,234.5678 -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
abies Wed Mar 10 10:57:51 2004 EDT Modified files: /php-src/ext/standard math.c Log: Don't use fp arithmetic for int results # FP arithmetic is not accurate enough on 64-bit archs # This patch relies on ZEND_SIGNED_MULTIPLY_LONG(), # so that should be fixed as well. http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.110r2=1.111ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.110 php-src/ext/standard/math.c:1.111 --- php-src/ext/standard/math.c:1.110 Thu Jan 8 03:17:33 2004 +++ php-src/ext/standard/math.c Wed Mar 10 10:57:49 2004 @@ -19,10 +19,11 @@ +--+ */ -/* $Id: math.c,v 1.110 2004/01/08 08:17:33 andi Exp $ */ +/* $Id: math.c,v 1.111 2004/03/10 15:57:49 abies Exp $ */ #include php.h #include php_math.h +#include zend_multiply.h #include math.h #include float.h @@ -449,22 +450,38 @@ convert_scalar_to_number(zexp TSRMLS_CC); /* if both base and exponent were longs, we'll try to get a long out */ - wantlong = Z_TYPE_P(zbase) == IS_LONG -Z_TYPE_P(zexp ) == IS_LONG Z_LVAL_P(zexp) = 0; - + 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; + + 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); - /* go ahead and calculate things. */ - dval = pow(Z_DVAL_P(zbase),Z_DVAL_P(zexp)); - - /* if we wanted a long, and dval LONG_MAX, it must be a long. */ - if (wantlong zend_finite(dval) dval = (double)LONG_MAX) { - RETURN_LONG((long)dval); - } - - /* otherwise just return the double. */ - RETURN_DOUBLE(dval); + RETURN_DOUBLE( pow(Z_DVAL_P(zbase),Z_DVAL_P(zexp)) ); } /* }}} */ -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
abies Wed Mar 10 10:59:50 2004 EDT Modified files: /php-src/ext/standard math.c Log: Nuke unused vars http://cvs.php.net/diff.php/php-src/ext/standard/math.c?r1=1.111r2=1.112ty=u Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.111 php-src/ext/standard/math.c:1.112 --- php-src/ext/standard/math.c:1.111 Wed Mar 10 10:57:49 2004 +++ php-src/ext/standard/math.c Wed Mar 10 10:59:50 2004 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.111 2004/03/10 15:57:49 abies Exp $ */ +/* $Id: math.c,v 1.112 2004/03/10 15:59:50 abies Exp $ */ #include php.h #include php_math.h @@ -438,8 +438,6 @@ PHP_FUNCTION(pow) { zval *zbase, *zexp; - double dval; - zend_bool wantlong; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, z/z/, zbase, zexp) == FAILURE) { return; -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c /ext/standard/tests/math bug25694.phpt
iliaa Mon Nov 17 21:50:42 2003 EDT Added files: /php-src/ext/standard/tests/mathbug25694.phpt Modified files: /php-src/ext/standard math.c Log: Fixed Bug #25694 (round() and number_format() give inconsistent results). Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.108 php-src/ext/standard/math.c:1.109 --- php-src/ext/standard/math.c:1.108 Sun Oct 5 04:08:49 2003 +++ php-src/ext/standard/math.c Mon Nov 17 21:50:40 2003 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.108 2003/10/05 08:08:49 zeev Exp $ */ +/* $Id: math.c,v 1.109 2003/11/18 02:50:40 iliaa Exp $ */ #include php.h #include php_math.h @@ -40,6 +40,18 @@ # endif #endif +#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; \ +} \ + /* {{{ proto int abs(int number) Return the absolute value of the number */ @@ -121,7 +133,7 @@ { zval **value, **precision; int places = 0; - double f, return_val; + double return_val; if (ZEND_NUM_ARGS() 1 || ZEND_NUM_ARGS() 2 || zend_get_parameters_ex(ZEND_NUM_ARGS(), value, precision) == FAILURE) { @@ -147,14 +159,7 @@ return_val = (Z_TYPE_PP(value) == IS_LONG) ? (double)Z_LVAL_PP(value) : Z_DVAL_PP(value); - f = pow(10.0, (double) places); - - return_val *= f; - if (return_val = 0.0) - return_val = floor(return_val + PHP_ROUND_FUZZ); - else - return_val = ceil(return_val - PHP_ROUND_FUZZ); - return_val /= f; + PHP_ROUND_WITH_FUZZ(return_val, places); RETURN_DOUBLE(return_val); break; @@ -995,13 +1000,15 @@ int tmplen, reslen=0; int count=0; int is_negative=0; - + if (d 0) { is_negative = 1; d = -d; } dec = MAX(0, dec); + PHP_ROUND_WITH_FUZZ(d, dec); + tmplen = spprintf(tmpbuf, 0, %.*f, dec, d); if (tmpbuf == NULL || !isdigit((int)tmpbuf[0])) { Index: php-src/ext/standard/tests/math/bug25694.phpt +++ php-src/ext/standard/tests/math/bug25694.phpt --TEST-- Bug #25694 (round() and number_format() inconsistency) --FILE-- ?php echo round 0.045 = . round(0.045, 2) . \n; echo number format 0.045 = . number_format(0.045, 2) . \n\n; echo round 0.055 = . round(0.055, 2) . \n; echo number format 0.055 = . number_format(0.055, 2) . \n\n; echo round 5.045 = . round(5.045, 2) . \n; echo number format 5.045 = . number_format(5.045, 2) . \n\n; echo round 5.055 = . round(5.055, 2) . \n; echo number format 5.055 = . number_format(5.055, 2) . \n\n; echo round 3.025 = . round(3.025, 2) . \n; echo number format 3.025 = . number_format(3.025, 2) . \n\n; echo round 4.025 = . round(4.025, 2) . \n; echo number format 4.025 = . number_format(4.025, 2) . \n\n; ? --EXPECT-- round 0.045 = 0.05 number format 0.045 = 0.05 round 0.055 = 0.06 number format 0.055 = 0.06 round 5.045 = 5.05 number format 5.045 = 5.05 round 5.055 = 5.06 number format 5.055 = 5.06 round 3.025 = 3.03 number format 3.025 = 3.03 round 4.025 = 4.03 number format 4.025 = 4.03 -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP-CVS] cvs: php-src /ext/standard math.c
iliaa Fri Aug 8 19:40:44 2003 EDT Modified files: /php-src/ext/standard math.c Log: Avoid a round() bug that occurs due to over optimization of C code by gcc. This bug was confirmed across multiple systems with gcc 2.95.3 3.X+ Index: php-src/ext/standard/math.c diff -u php-src/ext/standard/math.c:1.104 php-src/ext/standard/math.c:1.105 --- php-src/ext/standard/math.c:1.104 Tue Jun 10 16:03:38 2003 +++ php-src/ext/standard/math.c Fri Aug 8 19:40:44 2003 @@ -19,7 +19,7 @@ +--+ */ -/* $Id: math.c,v 1.104 2003/06/10 20:03:38 imajes Exp $ */ +/* $Id: math.c,v 1.105 2003/08/08 23:40:44 iliaa Exp $ */ #include php.h #include php_math.h @@ -32,6 +32,8 @@ #define M_PI 3.14159265358979323846 #endif +#define PHP_ROUND_FUZZ 0.501 + /* {{{ proto int abs(int number) Return the absolute value of the number */ @@ -143,9 +145,9 @@ return_val *= f; if (return_val = 0.0) - return_val = floor(return_val + 0.5); + return_val = floor(return_val + PHP_ROUND_FUZZ); else - return_val = ceil(return_val - 0.5); + return_val = ceil(return_val - PHP_ROUND_FUZZ); return_val /= f; RETURN_DOUBLE(return_val); -- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php