diff options
author | shigek <shigek@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-07-18 15:23:23 +0000 |
---|---|---|
committer | shigek <shigek@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2003-07-18 15:23:23 +0000 |
commit | b10272dc371a03844f685dc6db765c2bea11d29b (patch) | |
tree | 8d5691e2768e08f226fd5bc7500facb07dfc0a39 /ext | |
parent | b31bca1b45fa65e8b4da102505871ca77aa1a45b (diff) |
More pathes from Tadasi Saito.
As discussed in ruby-dev ML:
E,PI, etc are disabled.
BigDecimal op String disabled.
to_f changed.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4091 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext')
-rw-r--r-- | ext/bigdecimal/MANIFEST | 8 | ||||
-rw-r--r-- | ext/bigdecimal/bigdecimal.c | 225 | ||||
-rw-r--r-- | ext/bigdecimal/bigdecimal.h | 2 | ||||
-rw-r--r-- | ext/bigdecimal/bigdecimal_en.html | 89 | ||||
-rw-r--r-- | ext/bigdecimal/bigdecimal_ja.html | 113 |
5 files changed, 263 insertions, 174 deletions
diff --git a/ext/bigdecimal/MANIFEST b/ext/bigdecimal/MANIFEST index 7408e05284..8e9415eeea 100644 --- a/ext/bigdecimal/MANIFEST +++ b/ext/bigdecimal/MANIFEST @@ -7,10 +7,10 @@ depend extconf.rb bigdecimal_en.html bigdecimal_ja.html -lib/jacobian.rb -lib/newton.rb -lib/ludcmp.rb -lib/bigdecimal-rational.rb +lib/bigdecimal/jacobian.rb +lib/bigdecimal/newton.rb +lib/bigdecimal/ludcmp.rb +lib/bigdecimal/util.rb sample/linear.rb sample/nlsolve.rb sample/pi.rb diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 4a04d2a94a..8f86108e4f 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -9,6 +9,7 @@ * of this BigDecimal distribution. * * NOTES: + * For the notes other than listed bellow,see ruby CVS log. * 2003-04-17 * Bug in negative.exp(n) reported by Hitoshi Miyazaki fixed. * 2003-03-28 @@ -34,11 +35,16 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <errno.h> +#include <float.h> +#include <math.h> #include "ruby.h" #include "math.h" #include "version.h" - -/* #define USE_MUTABLE_METHOD */ + +/* #define ENABLE_NUMERIC_STRING */ +/* #define ENABLE_TRIAL_METHOD */ +/* #define ENABLE_BANG_METHOD */ VALUE rb_cBigDecimal; @@ -96,7 +102,6 @@ do_coerce(VALUE *x, VALUE *y) { VALUE ary; VALUE a[2]; - a[0] = *x; a[1] = *y; ary = rb_rescue(coerce_body, (VALUE)a, coerce_rescue, (VALUE)a); if (TYPE(ary) != T_ARRAY || RARRAY(ary)->len != 2) { @@ -135,7 +140,6 @@ ToValue(Real *p) static Real * GetVpValue(VALUE v, int must) { - double dv; Real *pv; VALUE bg; char szD[128]; @@ -153,40 +157,14 @@ GetVpValue(VALUE v, int must) case T_FIXNUM: sprintf(szD, "%d", FIX2INT(v)); return VpCreateRbObject(VpBaseFig() * 2 + 1, szD); - case T_FLOAT: - pv = VpCreateRbObject(VpDblFig()*2,"0"); - dv = RFLOAT(v)->value; - /* From float */ - if (isinf(dv)) { - VpException(VP_EXCEPTION_INFINITY,"Computation including infinity",0); - if(dv==VpGetDoublePosInf()) { - VpSetPosInf(pv); - } else { - VpSetNegInf(pv); - } - } else - if (isnan(dv)) { - VpException(VP_EXCEPTION_NaN,"Computation including NaN(Not a number)",0); - VpSetNaN(pv); - } else { - if (VpIsNegDoubleZero(dv)) { - VpSetNegZero(pv); - } else if(dv==0.0) { - VpSetPosZero(pv); - } else if(dv==1.0) { - VpSetOne(pv); - } else if(dv==-1.0) { - VpSetOne(pv); - pv->sign = -pv->sign; - } else { - VpDtoV(pv,dv); - } - } - return pv; + +#ifdef ENABLE_NUMERIC_STRING case T_STRING: SafeStringValue(v); return VpCreateRbObject(strlen(RSTRING(v)->ptr) + VpBaseFig() + 1, RSTRING(v)->ptr); +#endif /* ENABLE_NUMERIC_STRING */ + case T_BIGNUM: bg = rb_big2str(v, 10); return VpCreateRbObject(strlen(RSTRING(bg)->ptr) + VpBaseFig() + 1, @@ -449,15 +427,43 @@ BigDecimal_induced_from(VALUE self, VALUE x) } static VALUE +BigDecimal_to_f(VALUE self) +{ + ENTER(1); + Real *p; + double d, d2, da; + S_LONG e; + + GUARD_OBJ(p,GetVpValue(self,1)); + if(VpVtoD(&d, &e, p)!=1) return rb_float_new(d); + errno = 0; + d2 = pow(10.0,(double)e); + da = fabs(d); + if(errno == ERANGE || da > (DBL_MAX / d2) || da < (DBL_MIN / d2)) { + U_LONG nc = VpNumOfChars(p)+1; + char *psz = ALLOCA_N(char, nc); + VpToString(p, psz, 0); + rb_raise(rb_eRangeError, "BigDecimal %s out of Float range", psz); + } + return rb_float_new(d*d2); +} + +static VALUE BigDecimal_coerce(VALUE self, VALUE other) { ENTER(2); VALUE obj; Real *b; - GUARD_OBJ(b,GetVpValue(other,1)); - obj = rb_ary_new(); - obj = rb_ary_push(obj, b->obj); - obj = rb_ary_push(obj, self); + if(TYPE(other) == T_FLOAT) { + obj = rb_ary_new(); + obj = rb_ary_push(obj,other); + obj = rb_ary_push(obj,BigDecimal_to_f(self)); + } else { + GUARD_OBJ(b,GetVpValue(other,1)); + obj = rb_ary_new(); + obj = rb_ary_push(obj, b->obj); + obj = rb_ary_push(obj, self); + } return obj; } @@ -677,14 +683,14 @@ BigDecimal_div(VALUE self, VALUE r) ENTER(5); Real *c=NULL, *res=NULL, *div = NULL; r = BigDecimal_divide(&c, &res, &div, self, r); - SAVE(c);SAVE(res);SAVE(div); if(r!=(VALUE)0) return r; /* coerced by other */ + SAVE(c);SAVE(res);SAVE(div); /* a/b = c + r/b */ /* c xxxxx r 00000yyyyy ==> (y/b)*BASE >= HALF_BASE */ /* Round */ - if(VpIsDef(c)) { + if(VpIsDef(c) && (!VpIsZero(c))) { VpInternalRound(c,0,c->frac[c->Prec-1],(VpBaseVal()*res->frac[0])/div->frac[0]); } return ToValue(c); @@ -731,8 +737,8 @@ BigDecimal_mod(VALUE self, VALUE r) /* %: a%b = a - (a.to_f/b).floor * b */ Real *div=NULL, *mod=NULL; obj = BigDecimal_DoDivmod(self,r,&div,&mod); - SAVE(div);SAVE(mod); if(obj!=(VALUE)0) return obj; + SAVE(div);SAVE(mod); return ToValue(mod); } @@ -867,19 +873,6 @@ BigDecimal_mult2(VALUE self, VALUE b, VALUE n) } static VALUE -BigDecimal_dup(VALUE self) -{ - ENTER(5); - Real *c, *a; - U_LONG mx; - GUARD_OBJ(a,GetVpValue(self,1)); - mx = a->Prec *(VpBaseFig() + 1); - GUARD_OBJ(c,VpCreateRbObject(mx, "0")); - VpAsgn(c, a, 1); - return ToValue(c); -} - -static VALUE BigDecimal_abs(VALUE self) { ENTER(5); @@ -967,7 +960,6 @@ BigDecimal_truncate(int argc, VALUE *argv, VALUE self) ENTER(5); Real *c, *a; int iLoc; - int sw; U_LONG mx; VALUE vLoc; @@ -1081,9 +1073,7 @@ BigDecimal_split(VALUE self) VpSzMantissa(vp,psz1); s = 1; if(psz1[0]=='-') { - int i=0; - s = -1; - while(psz1[i]=psz1[i+1]) i++ ; + s = -1; ++psz1; } if(psz1[0]=='N') s=0; /* NaN */ e = VpExponent10(vp); @@ -1151,6 +1141,25 @@ BigDecimal_power(VALUE self, VALUE p) } static VALUE +BigDecimal_global_new(int argc, VALUE *argv, VALUE self) +{ + ENTER(5); + Real *pv; + S_LONG mf; + VALUE nFig; + VALUE iniValue; + + if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) { + mf = 0; + } else { + mf = GetPositiveInt(nFig); + } + SafeStringValue(iniValue); + GUARD_OBJ(pv,VpCreateRbObject(mf, RSTRING(iniValue)->ptr)); + return ToValue(pv); +} + +static VALUE BigDecimal_new(int argc, VALUE *argv, VALUE self) { ENTER(5); @@ -1183,6 +1192,14 @@ BigDecimal_limit(int argc, VALUE *argv, VALUE self) } static VALUE +BigDecimal_sign(VALUE self) +{ /* sign */ + int s = GetVpValue(self,1)->sign; + return INT2FIX(s); +} + +#ifdef ENABLE_TRIAL_METHOD +static VALUE BigDecimal_e(VALUE self, VALUE nFig) { ENTER(5); @@ -1223,13 +1240,6 @@ BigDecimal_exp(VALUE self, VALUE nFig) } static VALUE -BigDecimal_sign(VALUE self) -{ /* sign */ - int s = GetVpValue(self,1)->sign; - return INT2FIX(s); -} - -static VALUE BigDecimal_sincos(VALUE self, VALUE nFig) { ENTER(5); @@ -1252,9 +1262,9 @@ BigDecimal_sincos(VALUE self, VALUE nFig) rb_ary_push(obj, objCos); return obj; } +#endif /* ENABLE_TRIAL_METHOD */ - -#ifdef USE_MUTABLE_METHOD +#ifdef ENABLE_BANG_METHOD /**** Following methods are all MUTABLE and not currently activated. ****/ static void CheckAssign(VALUE x, VALUE y) @@ -1348,7 +1358,7 @@ BigDecimal_mult3(VALUE self, VALUE c, VALUE a, VALUE b) f = VpMult(cv,av,bv); return INT2NUM(f); } -#endif /* USE_MUTABLE_METHOD */ +#endif /* ENABLE_BANG_METHOD */ void Init_bigdecimal(void) @@ -1359,13 +1369,14 @@ Init_bigdecimal(void) /* Class and method registration */ rb_cBigDecimal = rb_define_class("BigDecimal",rb_cNumeric); + /* Global function */ + rb_define_global_function("BigDecimal", BigDecimal_global_new, -1); + /* Class methods */ rb_define_singleton_method(rb_cBigDecimal, "mode", BigDecimal_mode, 2); rb_define_singleton_method(rb_cBigDecimal, "new", BigDecimal_new, -1); rb_define_singleton_method(rb_cBigDecimal, "limit", BigDecimal_limit, -1); - rb_define_singleton_method(rb_cBigDecimal, "E", BigDecimal_e, 1); rb_define_singleton_method(rb_cBigDecimal, "double_fig", BigDecimal_double_fig, 0); - rb_define_singleton_method(rb_cBigDecimal, "PI", BigDecimal_pi, 1); rb_define_singleton_method(rb_cBigDecimal, "induced_from",BigDecimal_induced_from, 1); rb_define_singleton_method(rb_cBigDecimal, "_load", BigDecimal_load, 1); @@ -1417,8 +1428,8 @@ Init_bigdecimal(void) rb_define_method(rb_cBigDecimal, "modulo", BigDecimal_mod, 1); rb_define_method(rb_cBigDecimal, "remainder", BigDecimal_remainder, 1); rb_define_method(rb_cBigDecimal, "divmod", BigDecimal_divmod, 1); - rb_define_method(rb_cBigDecimal, "dup", BigDecimal_dup, 0); - rb_define_method(rb_cBigDecimal, "to_f", BigDecimal_dup, 0); /* to_f === dup */ + /* rb_define_method(rb_cBigDecimal, "dup", BigDecimal_dup, 0); */ + rb_define_method(rb_cBigDecimal, "to_f", BigDecimal_to_f, 0); rb_define_method(rb_cBigDecimal, "abs", BigDecimal_abs, 0); rb_define_method(rb_cBigDecimal, "sqrt", BigDecimal_sqrt, 1); rb_define_method(rb_cBigDecimal, "fix", BigDecimal_fix, 0); @@ -1428,8 +1439,6 @@ Init_bigdecimal(void) rb_define_method(rb_cBigDecimal, "ceil", BigDecimal_ceil, -1); rb_define_method(rb_cBigDecimal, "power", BigDecimal_power, 1); rb_define_method(rb_cBigDecimal, "**", BigDecimal_power, 1); - rb_define_method(rb_cBigDecimal, "exp", BigDecimal_exp, 1); - rb_define_method(rb_cBigDecimal, "sincos", BigDecimal_sincos, 1); rb_define_method(rb_cBigDecimal, "<=>", BigDecimal_comp, 1); rb_define_method(rb_cBigDecimal, "==", BigDecimal_eq, 1); rb_define_method(rb_cBigDecimal, "===", BigDecimal_eq, 1); @@ -1451,20 +1460,27 @@ Init_bigdecimal(void) rb_define_method(rb_cBigDecimal, "truncate", BigDecimal_truncate, -1); rb_define_method(rb_cBigDecimal, "_dump", BigDecimal_dump, -1); -#ifdef USE_MUTABLE_METHOD +#ifdef ENABLE_TRIAL_METHOD + rb_define_singleton_method(rb_cBigDecimal, "E", BigDecimal_e, 1); + rb_define_singleton_method(rb_cBigDecimal, "PI", BigDecimal_pi, 1); + rb_define_method(rb_cBigDecimal, "exp", BigDecimal_exp, 1); + rb_define_method(rb_cBigDecimal, "sincos", BigDecimal_sincos, 1); +#endif /* ENABLE_TRIAL_METHOD */ + +#ifdef ENABLE_BANG_METHOD rb_define_singleton_method(rb_cBigDecimal, "assign!", BigDecimal_assign, 3); rb_define_singleton_method(rb_cBigDecimal, "add!", BigDecimal_add3, 3); rb_define_singleton_method(rb_cBigDecimal, "sub!", BigDecimal_sub3, 3); rb_define_singleton_method(rb_cBigDecimal, "mult!", BigDecimal_mult3, 3); rb_define_singleton_method(rb_cBigDecimal, "div!",BigDecimal_divmod4, 4); -#endif /* USE_MUTABLE_METHOD */ +#endif /* ENABLE_BANG_METHOD */ } /* * * ============================================================================ * - * vp_ routines begins here + * vp_ routines begin from here. * * ============================================================================ * @@ -2064,7 +2080,7 @@ VpAlloc(U_LONG mx, char *szVal) VP_EXPORT int VpAsgn(Real *c, Real *a, int isw) { - U_LONG j, n; + U_LONG n; if(VpIsNaN(a)) { VpSetNaN(c); return 0; @@ -2080,7 +2096,7 @@ VpAsgn(Real *c, Real *a, int isw) VpSetSign(c,(isw*VpGetSign(a))); /* set sign */ n =(a->Prec < c->MaxPrec) ?(a->Prec) :(c->MaxPrec); c->Prec = n; - for(j=0;j < n; ++j) c->frac[j] = a->frac[j]; + memcpy(c->frac, a->frac, n * sizeof(U_LONG)); /* Needs round ? */ if(c->Prec < a->Prec) { VpInternalRound(c,n,(n>0)?a->frac[n-1]:0,a->frac[n]); @@ -2093,7 +2109,6 @@ VpAsgn(Real *c, Real *a, int isw) return c->Prec*BASE_FIG; } - /* * c = a + b when operation = 1 or 2 * = a - b when operation = -1 or -2. @@ -2602,7 +2617,7 @@ VpMult(Real *c, Real *a, Real *b) VpSetSign(c,VpGetSign(a)*VpGetSign(b)); /* set sign */ Carry = 0; nc = ind_c = MxIndAB; - for(i = 0; i <= nc; i++) c->frac[i] = 0; /* Initialize c */ + memset(c->frac, 0, (nc + 1) * sizeof(U_LONG)); /* Initialize c */ c->Prec = nc + 1; /* set precision */ for(nc = 0; nc < MxIndAB; ++nc, --ind_c) { if(nc < MxIndB) { /* The left triangle of the Fig. */ @@ -3113,7 +3128,7 @@ static void VpFormatSt(char *psz,S_INT fFmt) { U_LONG ie; - U_LONG i, j; + U_LONG i; S_INT nf = 0; char ch; int fDot = 0; @@ -3131,8 +3146,7 @@ VpFormatSt(char *psz,S_INT fFmt) if(ch == 'E') break; nf++; if(nf > fFmt) { - for(j = ie; j >= i; --j) - psz[j + 1] = psz[j]; + memmove(psz + i + 1, psz + i, ie - i + 1); ++ie; nf = 0; psz[i] = ' '; @@ -3283,7 +3297,7 @@ VpCtoV(Real *a, char *int_chr, U_LONG ni, char *frac, U_LONG nf, char *exp_chr, mi = ni; me = ne; signe = 1; - for(i=0;i < ma;++i) a->frac[i] = 0; + memset(a->frac, 0, ma * sizeof(U_LONG)); if(ne > 0) { i = 0; if(exp_chr[0] == '-') { @@ -3398,24 +3412,51 @@ Final: * DBLE_FIG ... Number of digits in a double variable. * * m -> d*10**e, 0<d<BASE + * [Returns] + * 0 ... Zero + * 1 ... Normal + * 2 ... Infinity + * -1 ... NaN */ -VP_EXPORT void +VP_EXPORT int VpVtoD(double *d, S_LONG *e, Real *m) { U_LONG ind_m, mm, fig; double div; + int f = 1; - fig =(DBLE_FIG + BASE_FIG - 1) / BASE_FIG; + if(VpIsNaN(m)) { + *d = VpGetDoubleNaN(); + *e = 0; + f = -1; /* NaN */ + goto Exit; + } else if(VpIsPosZero(m)) { *d = 0.0; *e = 0; + f = 0; goto Exit; } else if(VpIsNegZero(m)) { *d = VpGetDoubleNegZero(); *e = 0; + f = 0; + goto Exit; + } else + if(VpIsPosInf(m)) { + *d = VpGetDoublePosInf(); + *e = 0; + f = 2; + goto Exit; + } else + if(VpIsNegInf(m)) { + *d = VpGetDoubleNegInf(); + *e = 0; + f = 2; goto Exit; } + /* Normal number */ + fig =(DBLE_FIG + BASE_FIG - 1) / BASE_FIG; ind_m = 0; mm = Min(fig,(m->Prec)); *d = 0.0; @@ -3435,7 +3476,7 @@ Exit: printf(" DBLE_FIG = %ld\n", DBLE_FIG); } #endif /*_DEBUG */ - return; + return f; } /* @@ -3479,7 +3520,7 @@ VpDtoV(Real *m, double d) /* Now val = 0.xxxxx*BASE**ne */ mm = m->MaxPrec; - for(ind_m = 0;ind_m < mm;ind_m++) m->frac[ind_m] = 0; + memset(m->frac, 0, mm * sizeof(U_LONG)); for(ind_m = 0;val > 0.0 && ind_m < mm;ind_m++) { val *=(double)((S_INT)BASE); i =(U_LONG) val; @@ -3684,7 +3725,7 @@ Exit: VP_EXPORT void VpActiveRound(Real *y, Real *x, int f, int nf) { - int n,i,j,ix,ioffset; + int n,i,ix,ioffset; U_LONG v; U_LONG div; @@ -3700,7 +3741,7 @@ VpActiveRound(Real *y, Real *x, int f, int nf) ix = (nf + ((int)BASE_FIG))/((int)BASE_FIG)-1; if(ix<0 || ((U_LONG)ix)>=y->Prec) goto Exit; /* Unable to round */ ioffset = nf - ix*((int)BASE_FIG); - for(j=ix+1;j<(int)y->Prec;++j) y->frac[j] = 0; + memset(y->frac+ix+1, 0, (y->Prec - (ix+1)) * sizeof(U_LONG)); /* VpNmlz(y); */ v = y->frac[ix]; /* drop digits after pointed digit */ @@ -3929,6 +3970,7 @@ Exit: return 1; } +#ifdef ENABLE_TRIAL_METHOD /* * Calculates pi(=3.141592653589793238462........). */ @@ -4216,6 +4258,7 @@ Exit: VpFree(r); VpFree(z); } +#endif /* ENABLE_TRIAL_METHOD */ #ifdef _DEBUG int diff --git a/ext/bigdecimal/bigdecimal.h b/ext/bigdecimal/bigdecimal.h index 3debb52826..940647b446 100644 --- a/ext/bigdecimal/bigdecimal.h +++ b/ext/bigdecimal/bigdecimal.h @@ -131,7 +131,7 @@ VP_EXPORT S_LONG VpExponent10(Real *a); VP_EXPORT void VpSzMantissa(Real *a,char *psz); VP_EXPORT void VpToString(Real *a,char *psz,int fFmt); VP_EXPORT int VpCtoV(Real *a,char *int_chr,U_LONG ni,char *frac,U_LONG nf,char *exp_chr,U_LONG ne); -VP_EXPORT void VpVtoD(double *d,S_LONG *e,Real *m); +VP_EXPORT int VpVtoD(double *d,S_LONG *e,Real *m); VP_EXPORT void VpDtoV(Real *m,double d); VP_EXPORT void VpItoV(Real *m,S_INT ival); VP_EXPORT int VpSqrt(Real *y,Real *x); diff --git a/ext/bigdecimal/bigdecimal_en.html b/ext/bigdecimal/bigdecimal_en.html index 04dd83a429..5344720adc 100644 --- a/ext/bigdecimal/bigdecimal_en.html +++ b/ext/bigdecimal/bigdecimal_en.html @@ -81,21 +81,29 @@ to create BigDecimal objects,the program would like:<BR> <CODE><PRE> require 'bigdecimal' a=BigDecimal::new("0.123456789123456789") - b=BigDecimal::new("123456.78912345678",40) + b=BigDecimal("123456.78912345678",40) c=a+b </PRE></CODE> <H3>List of methods</H3> -In the following explanations,n specifies the minimum number of resulting significant digits, -not exactly but slightly excess memories will be allocated to newly created object. In 32 bits integer system,every 4 digits(in decimal) are computed simultaneously. This means the number of significant digits in BigDecimal is always a multiple of 4. +<P> +Some more methods are available in Ruby code (not C code). +To use them,just require util.rb as: +<PRE><CODE> +require "bigdecimal/util.rb" +</CODE></PRE> +String to BigDecimal conversion, BigDecimal to String conversion +(in "nnnnnn.mmmm" form not in "0.xxxxxEn" form) etc are defined. +For details,see the util.rb code. <H4><U>Class methods</U></H4> <UL> <LI><B>new</B></LI><BLOCKQUOTE> "new" method creates a new BigDecimal object.<BR> -a=BigDecimal::new(s[,n])<BR> +a=BigDecimal::new(s[,n]) or<BR> +a=BigDecimal(s[,n]) or<BR> where:<BR> s: Initial value string.<BR> n: Maximum number of significant digits of a. n must be a Fixnum object. @@ -187,18 +195,6 @@ Base value used in the BigDecimal calculation. On 32 bits integer system,the value of BASE is 10000.<BR> b = BigDecimal::BASE<BR> </BLOCKQUOTE> - -<LI><B>E</B></LI><BLOCKQUOTE> -e = BigDecimal::E(n)<BR> -where e(=2.718281828....) is the base value of natural logarithm.<BR> -n specifies the length of significant digits of e. - -</BLOCKQUOTE> -<LI><B>PI</B></LI><BLOCKQUOTE> -e = BigDecimal::PI(n)<BR> -returns at least n digits of the ratio of the circumference of a circle to its diameter -(pi=3.14159265358979....) using J.Machin's formula.<BR> -</BLOCKQUOTE> </UL> <H4><U>Instance methods</U></H4> @@ -394,14 +390,15 @@ means a = 0.xxxxxxx*10**n. </BLOCKQUOTE> <LI><B>to_f</B></LI><BLOCKQUOTE> -same as dup method. -creates a new BigDecimal object having same value. +Creates a new Float object having (nearly) the same value. +Use split method if you want to convert by yourself. </BLOCKQUOTE> </BLOCKQUOTE> <LI><B>sign</B></LI><BLOCKQUOTE> -returns the 'attribute' of a. n = a.sign <BR> +returns positive value if a > 0,negative value if a < 0, +otherwise zero if a == 0.<BR> where the value of n means that a is:<BR> n = BigDecimal::SIGN_NaN(0) : a is NaN<BR> n = BigDecimal::SIGN_POSITIVE_ZERO(1) : a is +0<BR> @@ -455,28 +452,13 @@ where "0x112344" is the address, '0.314E1' is the value,4 is the number of the significant digits, and 12 is the maximum number of the significant digits the object can hold. - </BLOCKQUOTE> -<LI><B>dup</B></LI><BLOCKQUOTE> -creates a new BigDecimal object having same value. -</BLOCKQUOTE> <LI><B>sqrt</B></LI><BLOCKQUOTE> c = a.sqrt(n)<BR> computes square root value of a with significant digit number n at least.<BR> - -</BLOCKQUOTE> -<LI><B>sincos</B></LI><BLOCKQUOTE> -computes and returns sine and cosine value of a with significant digit number n at least.<BR> -sin,cos = a.sincos(n)<BR> -Computation may return bad results unless |a|<2*3.1415..... </BLOCKQUOTE> -<LI><B>exp</B></LI><BLOCKQUOTE> -c = a.exp(n)<BR> -computes the base of natural logarithm value(e=2.718281828....) powered by a -with significant digit number n at least.<BR> -</BLOCKQUOTE> <LI><B>**</B></LI><BLOCKQUOTE> c = a ** n<BR> returns the value of a powered by n. @@ -507,6 +489,41 @@ same as ==,used in case statement. <LI>></LI> <LI>>=</LI> </UL> + +<H4><U>Class methods(trial version)</U></H4> +Following class methods are in trial stage, and not usable in default. +Uncomment /* #define ENABLE_TRIAL_METHOD */ in bigdecimal.c, compile and install +again if you want to use. +<UL> +<LI><B>E</B></LI><BLOCKQUOTE> +e = BigDecimal::E(n)<BR> +where e(=2.718281828....) is the base value of natural logarithm.<BR> +n specifies the length of significant digits of e. + +</BLOCKQUOTE> +<LI><B>PI</B></LI><BLOCKQUOTE> +e = BigDecimal::PI(n)<BR> +returns at least n digits of the ratio of the circumference of a circle to its diameter +(pi=3.14159265358979....) using J.Machin's formula.<BR> +</BLOCKQUOTE> +</UL> + +<H4><U>Instance methods(trial version)</U></H4> +Following instance methods are in trial stage, and not usable in default. +Uncomment /* #define ENABLE_TRIAL_METHOD */ in bigdecimal.c, compile and install +again if you want to use. +<UL> +<LI><B>sincos</B></LI><BLOCKQUOTE> +computes and returns sine and cosine value of a with significant digit number n at least.<BR> +sin,cos = a.sincos(n)<BR> +Computation may return bad results unless |a|<2*3.1415..... +</BLOCKQUOTE> +<LI><B>exp</B></LI><BLOCKQUOTE> +c = a.exp(n)<BR> +computes the base of natural logarithm value(e=2.718281828....) powered by a +with significant digit number n at least.<BR> +</BLOCKQUOTE> +</UL> <HR> <H3>About 'coerce'</H3> @@ -521,7 +538,9 @@ same as ==,used in case statement. BigDecimal object and the operation is performed,otherwise an error occures.</DD> </DL> -Attention must be paid when a String is to be translated to BigDecimal. +String is not translated to BigDecimal in default. +Uncomment /* #define ENABLE_NUMERIC_STRING */ in bigdecimal.c, compile and install +again if you want to enable string to BigDecimal conversion. Translation stops without error at the character representing non digit. For instance,"10XX" is translated to 10,"XXXX" is translated to 0.<BR> String representing zero or infinity such as "Infinity","+Infinity","-Infinity",and "NaN" can also be translated to BigDecimal unless false is specified by mode method.<BR> diff --git a/ext/bigdecimal/bigdecimal_ja.html b/ext/bigdecimal/bigdecimal_ja.html index 56bebaa863..caa1326c92 100644 --- a/ext/bigdecimal/bigdecimal_ja.html +++ b/ext/bigdecimal/bigdecimal_ja.html @@ -84,7 +84,7 @@ bigdecimal.c,bigdecimal.h <PRE> require 'bigdecimal' a=BigDecimal::new("0.123456789123456789") -b=BigDecimal::new("123456.78912345678",40) +b=BigDecimal("123456.78912345678",40) c=a+b </PRE> </CODE> @@ -92,18 +92,27 @@ c=a+b �Ƃ����悤�Ȋ����Ŏg�p���܂��B <H3>���\�b�h�ꗗ</H3> -�ȉ��̂悤�ȃ��\�b�h�����p�\�ł��B<BR> -�L�q��ABigDecimal �I�u�W�F�N�g�� a,b,c,r�ŁAString�i������j�I�u�W�F�N�g�� - s�A������ n �ŕ\�L���܂��B�܂��A�u�L�������v�Ƃ� BigDecimal �����x��ۏ��� -�����ł��B�҂�����ł͂���܂���A��̗]�T�������Čv�Z����܂��B�܂��A +�ȉ��̃��\�b�h�����p�\�ł��B +�u�L�������v�Ƃ� BigDecimal �����x��ۏ��錅���ł��B +�҂�����ł͂���܂���A��̗]�T�������Čv�Z����܂��B�܂��A �Ⴆ�R�Q�r�b�g�̃V�X�e���ł͂P�O�i�łS�����Ɍv�Z���܂��B�]���āA����ł́A �����́u�L�������v�͂S�̔{���ƂȂ��Ă��܂��B +<P> +�ȉ��̃��\�b�h�ȊO�ɂ��A(C �ł͂Ȃ�) Ruby �\�[�X�̌`�� +����Ă�����̂�����܂��B�Ⴆ�A������ BigDecimal �ւ� +�ϊ���A"0.xxxxxEn" �Ƃ����`���ł͂Ȃ� "nnnnn.mmmm" �̌`���̕����� +�֕ϊ����郁�\�b�h��������܂��B���p����ɂ� +<PRE><CODE> +require "bigdecimal/util.rb" +</CODE></PRE> +�̂悤�ɂ��܂��B�ڍׂ� util.rb �̓��e���Q�Ƃ��ĉ������B <H4><U>�N���X���\�b�h</U></H4> <UL> <LI><B>new</B></LI><BLOCKQUOTE> �V���� BigDecimal �I�u�W�F�N�g�����܂��B<BR> -a=BigDecimal::new(s[,n])<BR> +a=BigDecimal::new(s[,n]) �܂���<BR> +a=BigDecimal(s[,n])<BR> s �͏����l����Ŏw�肵�܂��D n �͕K�v�ȗL�������ia �̍ő�L�������j�𐮐��Ŏw�肵�܂��B n �� 0 �܂��͏ȗ����ꂽ�Ƃ��́An �̒l�� s �̗L�������Ƃ݂Ȃ���܂��B @@ -195,17 +204,6 @@ double_fig�͈ȉ��� C �v���O�����̌��ʂƓ����ł��B �����Ŏg�p������̒l�ł��B������ 32 �r�b�g�̏����n�ł�10000�ł��B<BR> b = BigDecimal::BASE<BR> </BLOCKQUOTE> - -<LI><B>E</B></LI><BLOCKQUOTE> -���R�ΐ��̒�e(=2.718281828....)���v�Z���܂��i�����Ƀe�C���[�W�J�Łj�B<BR> -e = BigDecimal::E(n)<BR> -n�͕K�v�ȗL�������𐮐��Ŏw�肵�܂��B -</BLOCKQUOTE> -<LI><B>PI</B></LI><BLOCKQUOTE> -�~����(=3.14159265358979....)���v�Z���܂��iJ�DMachin�̌�����p���܂��j�B<BR> -e = BigDecimal::PI(n)<BR> -n �͕K�v�ȗL�������𐮐��Ŏw�肵�܂��B -</BLOCKQUOTE> </UL> <H4><U>�C���X�^���X���\�b�h</U></H4> @@ -374,8 +372,9 @@ i �͒l�ɉ����� Fixnum �� Bignum �ɂȂ�܂��B a �� Infinity �� NaN �̂Ƃ��Ai �� nil �ɂȂ�܂��B </BLOCKQUOTE> <LI><B>to_f</B></LI><BLOCKQUOTE> -dup �ƑS�������ł��B -�����l�� BigDecimal �I�u�W�F�N�g�����܂��B +Float �I�u�W�F�N�g�ɕϊ����܂��B +��肫�ߍׂ����l���K�v�Ȃ�� split ���\�b�h�𗘗p���� +���������B </BLOCKQUOTE> <LI><B>to_s[(n)]</B></LI><BLOCKQUOTE> ������ɕϊ����܂�("0.xxxxxEn"�̌`�ɂȂ�܂��j�B<BR> @@ -391,7 +390,7 @@ n = a.exponent <BR> </BLOCKQUOTE> <LI><B>sign</B></LI><BLOCKQUOTE> -�l�̑�����Ԃ��܂��B +�l����(sign > 0)�A��(sign < 0)�A���̑�(sigh==0)�ł��邩�̏���Ԃ��܂��B n = a.sign <BR> �Ƃ����Ƃ� n �̒l�� a ���ȉ��̂Ƃ����Ӗ����܂��B<BR> () �̒��̐����́A���ۂ̒l�ł�(<A HREF="#STRUCT">�u�����\���v</A>���Q��)�B<BR> @@ -443,28 +442,6 @@ p a=BigDecimal::new("3.14",10)<BR> �ŏ���16�i���̓I�u�W�F�N�g�̃A�h���X�A���� '0.314E1' �͒l�A ����4�͌��݂̗L������(�\������傫�����Ƃ�����܂�)�A �Ō�̓I�u�W�F�N�g����蓾��ő包���ɂȂ�܂��B - -</BLOCKQUOTE> -<LI><B>dup</B></LI><BLOCKQUOTE> -�����l�� BigDecimal �I�u�W�F�N�g�����܂��B -</BLOCKQUOTE> -<LI><B>sqrt</B></LI><BLOCKQUOTE> -a�̗L���� n ���̕������in �̕������ł͂���܂���j�B -����܂��A�����Ƀj���[�g���@�Ōv�Z���܂��B<BR> -c = a.sqrt(n)<BR> -</BLOCKQUOTE> -<LI><B>sincos</B></LI><BLOCKQUOTE> -a �̗L���� n ���� sin �� cos ���Ɂi�e�C���[�W�J�Łj�v�Z���āA - sin �� cos �̔z���Ԃ��܂��B -n �͕K�v�ȗL�������ł��i n �� sin �� cos ���v�Z����킯�ł͂���܂���j�B -<BR> -sin,cos = a.sincos(n)<BR> -|a|<2*3.1415....�łȂ��Ɛ������������v�Z�ł��Ȃ����Ƃ�����܂��B -</BLOCKQUOTE> -<LI><B>exp</B></LI><BLOCKQUOTE> -���R�ΐ��̒�e(=2.718281828....)�� a ����v�Z���܂��B<BR> -c = a.exp(n)<BR> -n �͕K�v�ȗL�������ł��B </BLOCKQUOTE> <LI><B>**</B></LI><BLOCKQUOTE> a �� n ����v�Z���܂��B���͐����B<BR> @@ -476,6 +453,11 @@ c = a ** n<BR> c = a.power(n)<BR> ���ʂƂ��� c �̗L������ a �� n �{�ȏ�ɂȂ�̂Œ��ӁB </BLOCKQUOTE> +<LI><B>sqrt</B></LI><BLOCKQUOTE> +a�̗L���� n ���̕������in �̕������ł͂���܂���j�� +�j���[�g���@�Ōv�Z���܂��B<BR> +c = a.sqrt(n)<BR> +</BLOCKQUOTE> <LI><B><=></B></LI><BLOCKQUOTE> a==b �Ȃ� 0�Aa > b �Ȃ� 1�Aa < b �Ȃ� -1 �ɂȂ�܂��B<BR> @@ -493,11 +475,56 @@ c = a <=> b <LI><B>></B></LI> <LI><B>>=</B></LI> </UL> + +<H4><U>(�]���i�K��)�N���X���\�b�h</U></H4> +�ȉ��̃N���X���\�b�h�́A�܂��]���i�K�ł��̂ŁA�ʏ�ł� +�g�p�ł��܂���B�g�p����ɂ� bigdecimal.c �� +�u/* #define ENABLE_TRIAL_METHOD */�v +�̃R�����g���O���A�ăR���p�C���E�ăC���X�g�[�����K�v�ł��B + +<UL> +<LI><B>E</B></LI><BLOCKQUOTE> +���R�ΐ��̒�e(=2.718281828....)���v�Z���܂��i�����Ƀe�C���[�W�J�Łj�B<BR> +e = BigDecimal::E(n)<BR> +n�͕K�v�ȗL�������𐮐��Ŏw�肵�܂��B +</BLOCKQUOTE> +<LI><B>PI</B></LI><BLOCKQUOTE> +�~����(=3.14159265358979....)���v�Z���܂��iJ�DMachin�̌�����p���܂��j�B<BR> +e = BigDecimal::PI(n)<BR> +n �͕K�v�ȗL�������𐮐��Ŏw�肵�܂��B +</BLOCKQUOTE> +</UL> + +<H4><U>(�]���i�K��)�C���X�^���X���\�b�h</U></H4> +�ȉ��̃C���X�^���X���\�b�h�́A�܂��]���i�K�ł��̂ŁA�ʏ�ł� +�g�p�ł��܂���B�g�p����ɂ� bigdecimal.c �� +�u/* #define ENABLE_TRIAL_METHOD */�v +�̃R�����g���O���āA�ăR���p�C���E�ăC���X�g�[�����K�v�ł��B +<UL> +<LI><B>sincos</B></LI><BLOCKQUOTE> +a �̗L���� n ���� sin �� cos ���Ɂi�e�C���[�W�J�Łj�v�Z���āA + sin �� cos �̔z���Ԃ��܂��B +n �͕K�v�ȗL�������ł��i n �� sin �� cos ���v�Z����킯�ł͂���܂���j�B +<BR> +sin,cos = a.sincos(n)<BR> +|a| < 2*3.1415....�łȂ��Ɛ������������v�Z�ł��Ȃ����Ƃ�����܂��B +</BLOCKQUOTE> +<LI><B>exp</B></LI><BLOCKQUOTE> +���R�ΐ��̒�e(=2.718281828....)�� a ����v�Z���܂��B<BR> +c = a.exp(n)<BR> +n �͕K�v�ȗL�������ł��B +</BLOCKQUOTE> +</UL> + <H3>coerce�ɂ���</H3> BigDecimal �I�u�W�F�N�g���Z�p���Z�q�̍��ɂ���Ƃ��́ABigDecimal �I�u�W�F�N�g�� �E�ɂ���I�u�W�F�N�g��(�K�v�Ȃ�) BigDecimal �ɕϊ����Ă���v�Z���܂��B �]���āABigDecimal �I�u�W�F�N�g�ȊO�ł����l���Ӗ�������̂Ȃ�E�ɒu���� -���Z�͉\�ł��B<BR><BR> +���Z�͉\�ł��B<BR> +�������A������́i�ʏ�j���l�Ɏ����ϊ����邱�Ƃ͂ł��܂���B +������𐔒l�Ɏ����ϊ��Ɏ����ϊ��������ꍇ�� bigfloat.c �� +�u/* #define ENABLE_NUMERIC_STRING */�v�̃R�����g���O���Ă���A +�ăR���p�C���A�ăC���X�g�[������K�v������܂��B ������Ő��l��^����ꍇ�͒��ӂ��K�v�ł��B���l�ɕϊ��ł��Ȃ�����������ƁA �P�ɕϊ����~�߂邾���ŃG���[�ɂ͂Ȃ�܂���B"10XX"�Ȃ�P�O�A"XXXX"�͂O �ƈ����܂��B<BR> |