summaryrefslogtreecommitdiff
path: root/ext/bigdecimal/bigdecimal.c
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bigdecimal/bigdecimal.c')
-rw-r--r--ext/bigdecimal/bigdecimal.c426
1 files changed, 255 insertions, 171 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index d1aa56c121..d2701cfb8e 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -136,24 +136,6 @@ rb_rational_den(VALUE rat)
#define DoSomeOne(x,y,f) rb_num_coerce_bin(x,y,f)
/*
- * Returns the BigDecimal version number.
- */
-static VALUE
-BigDecimal_version(VALUE self)
-{
- /*
- * 1.0.0: Ruby 1.8.0
- * 1.0.1: Ruby 1.8.1
- * 1.1.0: Ruby 1.9.3
- */
-#ifndef RUBY_BIGDECIMAL_VERSION
-# error RUBY_BIGDECIMAL_VERSION is not defined
-#endif
- rb_warning("BigDecimal.ver is deprecated; use BigDecimal::VERSION instead.");
- return rb_str_new2(RUBY_BIGDECIMAL_VERSION);
-}
-
-/*
* VP routines used in BigDecimal part
*/
static unsigned short VpGetException(void);
@@ -664,9 +646,10 @@ VP_EXPORT Real *
VpNewRbClass(size_t mx, const char *str, VALUE klass)
{
VALUE obj = TypedData_Wrap_Struct(klass, &BigDecimal_data_type, 0);
- Real *pv = VpAlloc(mx,str);
+ Real *pv = VpAlloc(mx, str, 1);
RTYPEDDATA_DATA(obj) = pv;
pv->obj = obj;
+ RB_OBJ_FREEZE(obj);
return pv;
}
@@ -2165,15 +2148,10 @@ BigDecimal_exponent(VALUE self)
return INT2NUM(e);
}
-/* Returns debugging information about the value as a string of comma-separated
- * values in angle brackets with a leading #:
+/* Returns a string representation of self.
*
* BigDecimal("1234.5678").inspect
* #=> "0.12345678e4"
- *
- * The first part is the address, the second is the value as a string, and
- * the final part ss(mm) is the current number of significant digits and the
- * maximum number of significant digits, respectively.
*/
static VALUE
BigDecimal_inspect(VALUE self)
@@ -2335,7 +2313,7 @@ BigDecimal_power(int argc, VALUE*argv, VALUE self)
n = NIL_P(prec) ? (ssize_t)(x->Prec*VpBaseFig()) : NUM2SSIZET(prec);
if (VpIsNaN(x)) {
- y = VpCreateRbObject(n, "0#");
+ y = VpCreateRbObject(n, "0");
RB_GC_GUARD(y->obj);
VpSetNaN(y);
return ToValue(y);
@@ -2459,7 +2437,7 @@ BigDecimal_power(int argc, VALUE*argv, VALUE self)
}
}
else {
- y = VpCreateRbObject(n, "0#");
+ y = VpCreateRbObject(n, "0");
if (BIGDECIMAL_NEGATIVE_P(x)) {
if (is_integer(vexp)) {
if (is_even(vexp)) {
@@ -2492,7 +2470,7 @@ BigDecimal_power(int argc, VALUE*argv, VALUE self)
}
else if (RTEST(rb_funcall(abs_value, '<', 1, INT2FIX(1)))) {
if (is_negative(vexp)) {
- y = VpCreateRbObject(n, "0#");
+ y = VpCreateRbObject(n, "0");
if (is_even(vexp)) {
VpSetInf(y, VpGetSign(x));
}
@@ -2510,7 +2488,7 @@ BigDecimal_power(int argc, VALUE*argv, VALUE self)
}
else {
if (is_positive(vexp)) {
- y = VpCreateRbObject(n, "0#");
+ y = VpCreateRbObject(n, "0");
if (is_even(vexp)) {
VpSetInf(y, VpGetSign(x));
}
@@ -2560,52 +2538,8 @@ BigDecimal_power_op(VALUE self, VALUE exp)
return BigDecimal_power(1, &exp, self);
}
-static VALUE
-BigDecimal_s_allocate(VALUE klass)
-{
- return VpNewRbClass(0, NULL, klass)->obj;
-}
-
static Real *BigDecimal_new(int argc, VALUE *argv);
-/* call-seq:
- * new(initial, digits)
- *
- * Create a new BigDecimal object.
- *
- * initial:: The initial value, as an Integer, a Float, a Rational,
- * a BigDecimal, or a String.
- *
- * If it is a String, spaces are ignored and unrecognized characters
- * terminate the value.
- *
- * digits:: The number of significant digits, as an Integer. If omitted or 0,
- * the number of significant digits is determined from the initial
- * value.
- *
- * The actual number of significant digits used in computation is usually
- * larger than the specified number.
- *
- * ==== Exceptions
- *
- * TypeError:: If the +initial+ type is neither Integer, Float,
- * Rational, nor BigDecimal, this exception is raised.
- *
- * TypeError:: If the +digits+ is not an Integer, this exception is raised.
- *
- * ArgumentError:: If +initial+ is a Float, and the +digits+ is larger than
- * Float::DIG + 1, this exception is raised.
- *
- * ArgumentError:: If the +initial+ is a Float or Rational, and the +digits+
- * value is omitted, this exception is raised.
- */
-static VALUE
-BigDecimal_s_new(int argc, VALUE *argv, VALUE self)
-{
- rb_warning("BigDecimal.new is deprecated; use Kernel.BigDecimal method instead.");
- return rb_call_super(argc, argv);
-}
-
static VALUE
BigDecimal_initialize(int argc, VALUE *argv, VALUE self)
{
@@ -2700,10 +2634,40 @@ BigDecimal_new(int argc, VALUE *argv)
break;
}
StringValueCStr(iniValue);
- return VpAlloc(mf, RSTRING_PTR(iniValue));
+ return VpAlloc(mf, RSTRING_PTR(iniValue), 1);
}
-/* See also BigDecimal.new */
+/* call-seq:
+ * BigDecimal(initial, digits)
+ *
+ * Create a new BigDecimal object.
+ *
+ * initial:: The initial value, as an Integer, a Float, a Rational,
+ * a BigDecimal, or a String.
+ *
+ * If it is a String, spaces are ignored and unrecognized characters
+ * terminate the value.
+ *
+ * digits:: The number of significant digits, as an Integer. If omitted or 0,
+ * the number of significant digits is determined from the initial
+ * value.
+ *
+ * The actual number of significant digits used in computation is usually
+ * larger than the specified number.
+ *
+ * ==== Exceptions
+ *
+ * TypeError:: If the +initial+ type is neither Integer, Float,
+ * Rational, nor BigDecimal, this exception is raised.
+ *
+ * TypeError:: If the +digits+ is not an Integer, this exception is raised.
+ *
+ * ArgumentError:: If +initial+ is a Float, and the +digits+ is larger than
+ * Float::DIG + 1, this exception is raised.
+ *
+ * ArgumentError:: If the +initial+ is a Float or Rational, and the +digits+
+ * value is omitted, this exception is raised.
+ */
static VALUE
BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
{
@@ -2715,6 +2679,7 @@ BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
GUARD_OBJ(pv, BigDecimal_new(argc, argv));
if (ToValue(pv)) pv = VpCopy(NULL, pv);
RTYPEDDATA_DATA(obj) = pv;
+ RB_OBJ_FREEZE(obj);
return pv->obj = obj;
}
@@ -3138,6 +3103,21 @@ get_vp_value:
return y;
}
+VALUE
+rmpd_util_str_to_d(VALUE str)
+{
+ ENTER(1);
+ char const *c_str;
+ Real *pv;
+ VALUE obj;
+
+ c_str = StringValueCStr(str);
+ GUARD_OBJ(pv, VpAlloc(0, c_str, 0));
+ obj = TypedData_Wrap_Struct(rb_cBigDecimal, &BigDecimal_data_type, pv);
+ RB_OBJ_FREEZE(obj);
+ return obj;
+}
+
/* Document-class: BigDecimal
* BigDecimal provides arbitrary-precision floating point decimal arithmetic.
*
@@ -3277,18 +3257,17 @@ Init_bigdecimal(void)
/* Class and method registration */
rb_cBigDecimal = rb_define_class("BigDecimal", rb_cNumeric);
- rb_define_alloc_func(rb_cBigDecimal, BigDecimal_s_allocate);
/* Global function */
rb_define_global_function("BigDecimal", BigDecimal_global_new, -1);
/* Class methods */
- rb_define_singleton_method(rb_cBigDecimal, "new", BigDecimal_s_new, -1);
+ rb_undef_method(CLASS_OF(rb_cBigDecimal), "allocate");
+ rb_undef_method(CLASS_OF(rb_cBigDecimal), "new");
rb_define_singleton_method(rb_cBigDecimal, "mode", BigDecimal_mode, -1);
rb_define_singleton_method(rb_cBigDecimal, "limit", BigDecimal_limit, -1);
rb_define_singleton_method(rb_cBigDecimal, "double_fig", BigDecimal_double_fig, 0);
rb_define_singleton_method(rb_cBigDecimal, "_load", BigDecimal_load, 1);
- rb_define_singleton_method(rb_cBigDecimal, "ver", BigDecimal_version, 0);
rb_define_singleton_method(rb_cBigDecimal, "save_exception_mode", BigDecimal_save_exception_mode, 0);
rb_define_singleton_method(rb_cBigDecimal, "save_rounding_mode", BigDecimal_save_rounding_mode, 0);
@@ -3745,25 +3724,19 @@ One(void)
VP_EXPORT double
VpGetDoubleNaN(void) /* Returns the value of NaN */
{
- static double fNaN = 0.0;
- if (fNaN == 0.0) fNaN = Zero()/Zero();
- return fNaN;
+ return nan("");
}
VP_EXPORT double
VpGetDoublePosInf(void) /* Returns the value of +Infinity */
{
- static double fInf = 0.0;
- if (fInf == 0.0) fInf = One()/Zero();
- return fInf;
+ return HUGE_VAL;
}
VP_EXPORT double
VpGetDoubleNegInf(void) /* Returns the value of -Infinity */
{
- static double fInf = 0.0;
- if (fInf == 0.0) fInf = -(One()/Zero());
- return fInf;
+ return -HUGE_VAL;
}
VP_EXPORT double
@@ -3964,8 +3937,8 @@ VpInit(BDIGIT BaseVal)
VpGetDoubleNegZero();
/* Allocates Vp constants. */
- VpConstOne = VpAlloc(1UL, "1");
- VpPt5 = VpAlloc(1UL, ".5");
+ VpConstOne = VpAlloc(1UL, "1", 1);
+ VpPt5 = VpAlloc(1UL, ".5", 1);
#ifdef BIGDECIMAL_DEBUG
gnAlloc = 0;
@@ -4029,6 +4002,52 @@ overflow:
return VpException(VP_EXCEPTION_OVERFLOW, "Exponent overflow", 0);
}
+Real *
+rmpd_parse_special_string(const char *str)
+{
+ static const struct {
+ const char *str;
+ size_t len;
+ int sign;
+ } table[] = {
+ { SZ_INF, sizeof(SZ_INF) - 1, VP_SIGN_POSITIVE_INFINITE },
+ { SZ_PINF, sizeof(SZ_PINF) - 1, VP_SIGN_POSITIVE_INFINITE },
+ { SZ_NINF, sizeof(SZ_NINF) - 1, VP_SIGN_NEGATIVE_INFINITE },
+ { SZ_NaN, sizeof(SZ_NaN) - 1, VP_SIGN_NaN }
+ };
+ static const size_t table_length = sizeof(table) / sizeof(table[0]);
+ size_t i;
+
+ for (i = 0; i < table_length; ++i) {
+ const char *p;
+ if (strncmp(str, table[i].str, table[i].len) != 0) {
+ continue;
+ }
+
+ p = str + table[i].len;
+ while (*p && ISSPACE(*p)) ++p;
+ if (*p == '\0') {
+ Real *vp = VpAllocReal(1);
+ vp->MaxPrec = 1;
+ switch (table[i].sign) {
+ default:
+ UNREACHABLE; break;
+ case VP_SIGN_POSITIVE_INFINITE:
+ VpSetPosInf(vp);
+ return vp;
+ case VP_SIGN_NEGATIVE_INFINITE:
+ VpSetNegInf(vp);
+ return vp;
+ case VP_SIGN_NaN:
+ VpSetNaN(vp);
+ return vp;
+ }
+ }
+ }
+
+ return NULL;
+}
+
/*
* Allocates variable.
* [Input]
@@ -4043,10 +4062,10 @@ overflow:
* NULL be returned if memory allocation is failed,or any error.
*/
VP_EXPORT Real *
-VpAlloc(size_t mx, const char *szVal)
+VpAlloc(size_t mx, const char *szVal, int strict_p)
{
const char *orig_szVal = szVal;
- size_t i, ni, ipn, ipf, nf, ipe, ne, dot_seen, exp_seen, nalloc;
+ size_t i, j, ni, ipf, nf, ipe, ne, dot_seen, exp_seen, nalloc;
char v, *psz;
int sign=1;
Real *vp = NULL;
@@ -4057,7 +4076,10 @@ VpAlloc(size_t mx, const char *szVal)
if (mx == 0) ++mx;
if (szVal) {
+ /* Skipping leading spaces */
while (ISSPACE(*szVal)) szVal++;
+
+ /* Processing the leading one `#` */
if (*szVal != '#') {
if (mf) {
mf = (mf + BASE_FIG - 1) / BASE_FIG + 2; /* Needs 1 more for div */
@@ -4071,6 +4093,7 @@ VpAlloc(size_t mx, const char *szVal)
}
}
else {
+ return_zero:
/* necessary to be able to store */
/* at least mx digits. */
/* szVal==NULL ==> allocate zero value. */
@@ -4081,105 +4104,166 @@ VpAlloc(size_t mx, const char *szVal)
return vp;
}
- /* Skip all '_' after digit: 2006-6-30 */
- ni = 0;
+ /* Check on Inf & NaN */
+ if ((vp = rmpd_parse_special_string(szVal)) != NULL) {
+ return vp;
+ }
+
+ /* Scanning digits */
+
+ /* A buffer for keeping scanned digits */
buf = rb_str_tmp_new(strlen(szVal) + 1);
psz = RSTRING_PTR(buf);
- i = 0;
- ipn = 0;
- while ((psz[i] = szVal[ipn]) != 0) {
- if (ISSPACE(psz[i])) {
- psz[i] = 0;
+
+ /* cursor: i for psz, and j for szVal */
+ i = j = 0;
+
+ /* Scanning: sign part */
+ v = psz[i] = szVal[j];
+ if ((v == '-') || (v == '+')) {
+ sign = -(v == '-');
+ ++i;
+ ++j;
+ }
+
+ /* Scanning: integer part */
+ ni = 0; /* number of digits in the integer part */
+ while ((v = psz[i] = szVal[j]) != '\0') {
+ if (!strict_p && ISSPACE(v)) {
+ v = psz[i] = '\0';
break;
}
- if (ISDIGIT(psz[i])) ++ni;
- if (psz[i] == '_') {
+ if (v == '_') {
if (ni > 0) {
- ipn++;
- continue;
+ v = szVal[j+1];
+ if (v == '\0' || ISSPACE(v) || ISDIGIT(v)) {
+ ++j;
+ continue;
+ }
+ if (!strict_p) {
+ v = psz[i] = '\0';
+ break;
+ }
}
- psz[i] = 0;
+ goto invalid_value;
+ }
+ if (!ISDIGIT(v)) {
break;
}
+ ++ni;
++i;
- ++ipn;
+ ++j;
}
- szVal = psz;
- /* Check on Inf & NaN */
- if (StrCmp(szVal, SZ_PINF) == 0 || StrCmp(szVal, SZ_INF) == 0 ) {
- vp = VpAllocReal(1);
- vp->MaxPrec = 1; /* set max precision */
- VpSetPosInf(vp);
- return vp;
- }
- if (StrCmp(szVal, SZ_NINF) == 0) {
- vp = VpAllocReal(1);
- vp->MaxPrec = 1; /* set max precision */
- VpSetNegInf(vp);
- return vp;
- }
- if (StrCmp(szVal, SZ_NaN) == 0) {
- vp = VpAllocReal(1);
- vp->MaxPrec = 1; /* set max precision */
- VpSetNaN(vp);
- return vp;
- }
-
- /* check on number szVal[] */
- ipn = i = 0;
- if (szVal[i] == '-') { sign=-1; ++i; }
- else if (szVal[i] == '+') ++i;
- /* Skip digits */
- ni = 0; /* digits in mantissa */
- while ((v = szVal[i]) != 0) {
- if (!ISDIGIT(v)) break;
- ++i;
- ++ni;
- }
- nf = 0;
- ipf = 0;
- ipe = 0;
- ne = 0;
+ /* Scanning: fractional part */
+ nf = 0; /* number of digits in the fractional part */
+ ne = 0; /* number of digits in the exponential part */
+ ipf = 0; /* index of the beginning of the fractional part */
+ ipe = 0; /* index of the beginning of the exponential part */
dot_seen = 0;
exp_seen = 0;
- if (v) {
- /* other than digit nor \0 */
- if (szVal[i] == '.') { /* xxx. */
+
+ if (v != '\0') {
+ /* Scanning fractional part */
+ if ((psz[i] = szVal[j]) == '.') {
dot_seen = 1;
++i;
+ ++j;
ipf = i;
- while ((v = szVal[i]) != 0) { /* get fraction part. */
+ while ((v = psz[i] = szVal[j]) != '\0') {
+ if (!strict_p && ISSPACE(v)) {
+ v = psz[i] = '\0';
+ break;
+ }
+ if (v == '_') {
+ if (nf > 0 && ISDIGIT(szVal[j+1])) {
+ ++j;
+ continue;
+ }
+ if (!strict_p) {
+ v = psz[i] = '\0';
+ if (nf == 0) {
+ dot_seen = 0;
+ }
+ break;
+ }
+ goto invalid_value;
+ }
if (!ISDIGIT(v)) break;
++i;
+ ++j;
++nf;
}
}
- ipe = 0; /* Exponent */
-
- switch (szVal[i]) {
- case '\0':
- break;
- case 'e': case 'E':
- case 'd': case 'D':
- exp_seen = 1;
- ++i;
- ipe = i;
- v = szVal[i];
- if ((v == '-') || (v == '+')) ++i;
- while ((v=szVal[i]) != 0) {
- if (!ISDIGIT(v)) break;
+
+ /* Scanning exponential part */
+ if (v != '\0') {
+ switch ((psz[i] = szVal[j])) {
+ case '\0':
+ break;
+ case 'e': case 'E':
+ case 'd': case 'D':
+ exp_seen = 1;
++i;
- ++ne;
- }
- break;
- default:
- break;
+ ++j;
+ ipe = i;
+ v = psz[i] = szVal[j];
+ if ((v == '-') || (v == '+')) {
+ ++i;
+ ++j;
+ }
+ while ((v = psz[i] = szVal[j]) != '\0') {
+ if (!strict_p && ISSPACE(v)) {
+ v = psz[i] = '\0';
+ break;
+ }
+ if (v == '_') {
+ if (ne > 0 && ISDIGIT(szVal[j+1])) {
+ ++j;
+ continue;
+ }
+ if (!strict_p) {
+ v = psz[i] = '\0';
+ if (ne == 0) {
+ exp_seen = 0;
+ }
+ break;
+ }
+ goto invalid_value;
+ }
+ if (!ISDIGIT(v)) break;
+ ++i;
+ ++j;
+ ++ne;
+ }
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (v != '\0') {
+ /* Scanning trailing spaces */
+ while (ISSPACE(szVal[j])) ++j;
+
+ /* Invalid character */
+ if (szVal[j]) {
+ goto invalid_value;
+ }
}
}
+
+ psz[i] = '\0';
+
if (((ni == 0 || dot_seen) && nf == 0) || (exp_seen && ne == 0)) {
- VALUE str = rb_str_new2(orig_szVal);
- rb_raise(rb_eArgError, "invalid value for BigDecimal(): \"%"PRIsVALUE"\"", str);
+ VALUE str;
+ invalid_value:
+ if (!strict_p) {
+ goto return_zero;
+ }
+
+ str = rb_str_new2(orig_szVal);
+ rb_raise(rb_eArgError, "invalid value for BigDecimal(): \"%"PRIsVALUE"\"", str);
}
nalloc = (ni + nf + BASE_FIG - 1) / BASE_FIG + 1; /* set effective allocation */
@@ -4191,7 +4275,7 @@ VpAlloc(size_t mx, const char *szVal)
/* xmalloc() alway returns(or throw interruption) */
vp->MaxPrec = mx; /* set max precision */
VpSetZero(vp, sign);
- VpCtoV(vp, &szVal[ipn], ni, &szVal[ipf], nf, &szVal[ipe], ne);
+ VpCtoV(vp, psz, ni, psz + ipf, nf, psz + ipe, ne);
rb_str_resize(buf, 0);
return vp;
}
@@ -4754,7 +4838,7 @@ VpMult(Real *c, Real *a, Real *b)
if (MxIndC < MxIndAB) { /* The Max. prec. of c < Prec(a)+Prec(b) */
w = c;
- c = VpAlloc((size_t)((MxIndAB + 1) * BASE_FIG), "#0");
+ c = VpAlloc((size_t)((MxIndAB + 1) * BASE_FIG), "#0", 1);
MxIndC = MxIndAB;
}
@@ -5922,8 +6006,8 @@ VpSqrt(Real *y, Real *x)
if (x->MaxPrec > (size_t)n) n = (ssize_t)x->MaxPrec;
/* allocate temporally variables */
- f = VpAlloc(y->MaxPrec * (BASE_FIG + 2), "#1");
- r = VpAlloc((n + n) * (BASE_FIG + 2), "#1");
+ f = VpAlloc(y->MaxPrec * (BASE_FIG + 2), "#1", 1);
+ r = VpAlloc((n + n) * (BASE_FIG + 2), "#1", 1);
nr = 0;
y_prec = y->MaxPrec;
@@ -6375,8 +6459,8 @@ VpPower(Real *y, Real *x, SIGNED_VALUE n)
/* Allocate working variables */
- w1 = VpAlloc((y->MaxPrec + 2) * BASE_FIG, "#0");
- w2 = VpAlloc((w1->MaxPrec * 2 + 1) * BASE_FIG, "#0");
+ w1 = VpAlloc((y->MaxPrec + 2) * BASE_FIG, "#0", 1);
+ w2 = VpAlloc((w1->MaxPrec * 2 + 1) * BASE_FIG, "#0", 1);
/* calculation start */
VpAsgn(y, x, 1);