1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Import from ruby/bigdecimal repository

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56550 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mrkn 2016-11-03 12:46:22 +00:00
parent 6c7024f6f4
commit 03c9bc2b1d
2 changed files with 92 additions and 89 deletions

View file

@ -1,3 +1,8 @@
Thu Nov 3 21:45:00 2016 Kenta Murata <mrkn@mrkn.jp>
* ext/bigdecimal/bigdecimal.c: Import changes from ruby/bigdecimal
repository.
Thu Nov 3 15:01:29 2016 Nobuyoshi Nakada <nobu@ruby-lang.org> Thu Nov 3 15:01:29 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* file.c: include sys/sysmacros.h for ArchLinux which deprecated * file.c: include sys/sysmacros.h for ArchLinux which deprecated

View file

@ -2525,7 +2525,7 @@ BigDecimal_initialize(int argc, VALUE *argv, VALUE self)
/* :nodoc: /* :nodoc:
* *
* private method to dup and clone the provided BigDecimal +other+ * private method for dup and clone the provided BigDecimal +other+
*/ */
static VALUE static VALUE
BigDecimal_initialize_copy(VALUE self, VALUE other) BigDecimal_initialize_copy(VALUE self, VALUE other)
@ -2760,7 +2760,7 @@ BigMath_s_exp(VALUE klass, VALUE x, VALUE vprec)
rb_raise(rb_eArgError, "Zero or negative precision for exp"); rb_raise(rb_eArgError, "Zero or negative precision for exp");
} }
/* TODO: the following switch statement is almostly the same as one in the /* TODO: the following switch statement is almost same as one in the
* BigDecimalCmp function. */ * BigDecimalCmp function. */
switch (TYPE(x)) { switch (TYPE(x)) {
case T_DATA: case T_DATA:
@ -2898,7 +2898,7 @@ BigMath_s_log(VALUE klass, VALUE x, VALUE vprec)
rb_raise(rb_eArgError, "Zero or negative precision for exp"); rb_raise(rb_eArgError, "Zero or negative precision for exp");
} }
/* TODO: the following switch statement is almostly the same as one in the /* TODO: the following switch statement is almost same as one in the
* BigDecimalCmp function. */ * BigDecimalCmp function. */
switch (TYPE(x)) { switch (TYPE(x)) {
case T_DATA: case T_DATA:
@ -3126,9 +3126,8 @@ get_vp_value:
* *
* Copyright (C) 2002 by Shigeo Kobayashi <shigeo@tinyforest.gr.jp>. * Copyright (C) 2002 by Shigeo Kobayashi <shigeo@tinyforest.gr.jp>.
* *
* You may distribute under the terms of either the GNU General Public * BigDecimal is released under the Ruby and 2-clause BSD licenses.
* License or the Artistic License, as specified in the README file * See LICENSE.txt for details.
* of the BigDecimal distribution.
* *
* Maintained by mrkn <mrkn@mrkn.jp> and ruby-core members. * Maintained by mrkn <mrkn@mrkn.jp> and ruby-core members.
* *
@ -3906,28 +3905,28 @@ VpAlloc(size_t mx, const char *szVal)
if (mx == 0) ++mx; if (mx == 0) ++mx;
if (szVal) { if (szVal) {
while (ISSPACE(*szVal)) szVal++; while (ISSPACE(*szVal)) szVal++;
if (*szVal != '#') { if (*szVal != '#') {
if (mf) { if (mf) {
mf = (mf + BASE_FIG - 1) / BASE_FIG + 2; /* Needs 1 more for div */ mf = (mf + BASE_FIG - 1) / BASE_FIG + 2; /* Needs 1 more for div */
if (mx > mf) { if (mx > mf) {
mx = mf; mx = mf;
} }
} }
} }
else { else {
++szVal; ++szVal;
} }
} }
else { else {
/* necessary to be able to store */ /* necessary to be able to store */
/* at least mx digits. */ /* at least mx digits. */
/* szVal==NULL ==> allocate zero value. */ /* szVal==NULL ==> allocate zero value. */
vp = VpAllocReal(mx); vp = VpAllocReal(mx);
/* xmalloc() alway returns(or throw interruption) */ /* xmalloc() alway returns(or throw interruption) */
vp->MaxPrec = mx; /* set max precision */ vp->MaxPrec = mx; /* set max precision */
VpSetZero(vp, 1); /* initialize vp to zero. */ VpSetZero(vp, 1); /* initialize vp to zero. */
return vp; return vp;
} }
/* Skip all '_' after digit: 2006-6-30 */ /* Skip all '_' after digit: 2006-6-30 */
@ -3937,43 +3936,42 @@ VpAlloc(size_t mx, const char *szVal)
i = 0; i = 0;
ipn = 0; ipn = 0;
while ((psz[i] = szVal[ipn]) != 0) { while ((psz[i] = szVal[ipn]) != 0) {
if (ISDIGIT(psz[i])) ++ni; if (ISSPACE(psz[i])) {
if (psz[i] == '_') { psz[i] = 0;
if (ni > 0) { break;
ipn++; }
continue; if (ISDIGIT(psz[i])) ++ni;
} if (psz[i] == '_') {
psz[i] = 0; if (ni > 0) {
break; ipn++;
} continue;
++i; }
++ipn; psz[i] = 0;
} break;
/* Skip trailing spaces */ }
while (--i > 0) { ++i;
if (ISSPACE(psz[i])) psz[i] = 0; ++ipn;
else break;
} }
szVal = psz; szVal = psz;
/* Check on Inf & NaN */ /* Check on Inf & NaN */
if (StrCmp(szVal, SZ_PINF) == 0 || StrCmp(szVal, SZ_INF) == 0 ) { if (StrCmp(szVal, SZ_PINF) == 0 || StrCmp(szVal, SZ_INF) == 0 ) {
vp = VpAllocReal(1); vp = VpAllocReal(1);
vp->MaxPrec = 1; /* set max precision */ vp->MaxPrec = 1; /* set max precision */
VpSetPosInf(vp); VpSetPosInf(vp);
return vp; return vp;
} }
if (StrCmp(szVal, SZ_NINF) == 0) { if (StrCmp(szVal, SZ_NINF) == 0) {
vp = VpAllocReal(1); vp = VpAllocReal(1);
vp->MaxPrec = 1; /* set max precision */ vp->MaxPrec = 1; /* set max precision */
VpSetNegInf(vp); VpSetNegInf(vp);
return vp; return vp;
} }
if (StrCmp(szVal, SZ_NaN) == 0) { if (StrCmp(szVal, SZ_NaN) == 0) {
vp = VpAllocReal(1); vp = VpAllocReal(1);
vp->MaxPrec = 1; /* set max precision */ vp->MaxPrec = 1; /* set max precision */
VpSetNaN(vp); VpSetNaN(vp);
return vp; return vp;
} }
/* check on number szVal[] */ /* check on number szVal[] */
@ -3983,45 +3981,45 @@ VpAlloc(size_t mx, const char *szVal)
/* Skip digits */ /* Skip digits */
ni = 0; /* digits in mantissa */ ni = 0; /* digits in mantissa */
while ((v = szVal[i]) != 0) { while ((v = szVal[i]) != 0) {
if (!ISDIGIT(v)) break; if (!ISDIGIT(v)) break;
++i; ++i;
++ni; ++ni;
} }
nf = 0; nf = 0;
ipf = 0; ipf = 0;
ipe = 0; ipe = 0;
ne = 0; ne = 0;
if (v) { if (v) {
/* other than digit nor \0 */ /* other than digit nor \0 */
if (szVal[i] == '.') { /* xxx. */ if (szVal[i] == '.') { /* xxx. */
++i; ++i;
ipf = i; ipf = i;
while ((v = szVal[i]) != 0) { /* get fraction part. */ while ((v = szVal[i]) != 0) { /* get fraction part. */
if (!ISDIGIT(v)) break; if (!ISDIGIT(v)) break;
++i; ++i;
++nf; ++nf;
} }
} }
ipe = 0; /* Exponent */ ipe = 0; /* Exponent */
switch (szVal[i]) { switch (szVal[i]) {
case '\0': case '\0':
break; break;
case 'e': case 'E': case 'e': case 'E':
case 'd': case 'D': case 'd': case 'D':
++i; ++i;
ipe = i; ipe = i;
v = szVal[i]; v = szVal[i];
if ((v == '-') || (v == '+')) ++i; if ((v == '-') || (v == '+')) ++i;
while ((v=szVal[i]) != 0) { while ((v=szVal[i]) != 0) {
if (!ISDIGIT(v)) break; if (!ISDIGIT(v)) break;
++i; ++i;
++ne; ++ne;
} }
break; break;
default: default:
break; break;
} }
} }
nalloc = (ni + nf + BASE_FIG - 1) / BASE_FIG + 1; /* set effective allocation */ nalloc = (ni + nf + BASE_FIG - 1) / BASE_FIG + 1; /* set effective allocation */
/* units for szVal[] */ /* units for szVal[] */
@ -4090,7 +4088,7 @@ VpAsgn(Real *c, Real *a, int isw)
/* /*
* c = a + b when operation = 1 or 2 * c = a + b when operation = 1 or 2
* = a - b when operation = -1 or -2. * c = a - b when operation = -1 or -2.
* Returns number of significant digits of c * Returns number of significant digits of c
*/ */
VP_EXPORT size_t VP_EXPORT size_t
@ -4223,7 +4221,7 @@ end_if:
} }
/* /*
* Addition of two variable precisional variables * Addition of two values with variable precision
* a and b assuming abs(a)>abs(b). * a and b assuming abs(a)>abs(b).
* c = abs(a) + abs(b) ; where |a|>=|b| * c = abs(a) + abs(b) ; where |a|>=|b|
*/ */
@ -4982,7 +4980,7 @@ VpComp(Real *a, Real *b)
goto Exit; goto Exit;
} }
/* a and b have same exponent, then compare significand. */ /* a and b have same exponent, then compare their significand. */
mx = (a->Prec < b->Prec) ? a->Prec : b->Prec; mx = (a->Prec < b->Prec) ? a->Prec : b->Prec;
ind = 0; ind = 0;
while (ind < mx) { while (ind < mx) {