From 5d8f3617e0528ca169c7c6bf32833c9cc9afbe5a Mon Sep 17 00:00:00 2001 From: shigek Date: Fri, 1 Aug 2003 04:48:32 +0000 Subject: Specs adjusted for FLoat. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4258 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/bigdecimal/bigdecimal.c | 76 ++++++++++++++++++++++++++++++--------- ext/bigdecimal/bigdecimal.h | 7 +++- ext/bigdecimal/bigdecimal_en.html | 42 ++++++---------------- ext/bigdecimal/bigdecimal_ja.html | 36 ++++++------------- 4 files changed, 87 insertions(+), 74 deletions(-) (limited to 'ext/bigdecimal') diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index d01dbc4f5d..2acc0e67c4 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -31,7 +31,6 @@ * */ -#include "ruby.h" #include #include #include @@ -39,6 +38,8 @@ #include #include #include +#include "ruby.h" +#include "math.h" #include "version.h" /* #define ENABLE_NUMERIC_STRING */ @@ -720,6 +721,17 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod) if(!b) return DoSomeOne(self,r); SAVE(b); + if(VpIsNaN(a) || VpIsNaN(b)) goto NaN; + if(VpIsInf(a) || VpIsInf(b)) goto NaN; + if(VpIsZero(b)) goto NaN; + if(VpIsZero(a)) { + GUARD_OBJ(c,VpCreateRbObject(1, "0")); + GUARD_OBJ(d,VpCreateRbObject(1, "0")); + *div = d; + *mod = c; + return (VALUE)0; + } + mx = a->Prec; if(mxPrec) mx = b->Prec; mx =(mx + 1) * VpBaseFig(); @@ -728,9 +740,23 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, Real **div, Real **mod) VpDivd(c, res, a, b); mx = c->Prec *(VpBaseFig() + 1); GUARD_OBJ(d,VpCreateRbObject(mx, "0")); - VpActiveRound(d,c,VP_ROUND_FLOOR,0); + VpActiveRound(d,c,VP_ROUND_DOWN,0); VpMult(res,d,b); VpAddSub(c,a,res,-1); + if(!VpIsZero(c) && (VpGetSign(a)*VpGetSign(b)<0)) { + VpAddSub(res,d,VpOne(),-1); + VpAddSub(d ,c,b, 1); + *div = res; + *mod = d; + } else { + *div = d; + *mod = c; + } + return (VALUE)0; + +NaN: + GUARD_OBJ(c,VpCreateRbObject(1, "NaN")); + GUARD_OBJ(d,VpCreateRbObject(1, "NaN")); *div = d; *mod = c; return (VALUE)0; @@ -813,22 +839,32 @@ BigDecimal_divmod(VALUE self, VALUE r) } static VALUE -BigDecimal_div2(VALUE self, VALUE b, VALUE n) +BigDecimal_div2(int argc, VALUE *argv, VALUE self) { ENTER(10); VALUE obj; - Real *res=NULL; - Real *av=NULL, *bv=NULL, *cv=NULL; - U_LONG ix = (U_LONG)GetPositiveInt(n); - U_LONG mx = (ix+VpBaseFig()*2); - GUARD_OBJ(cv,VpCreateRbObject(mx,"0")); - GUARD_OBJ(av,GetVpValue(self,1)); - GUARD_OBJ(bv,GetVpValue(b,1)); - mx = cv->MaxPrec+1; - GUARD_OBJ(res,VpCreateRbObject((mx * 2 + 2)*VpBaseFig(), "#0")); - VpDivd(cv,res,av,bv); - VpLeftRound(cv,VpGetRoundMode(),ix); - return ToValue(cv); + VALUE b,n; + int na = rb_scan_args(argc,argv,"11",&b,&n); + if(na==1) { /* div in Float sense */ + Real *div=NULL; + Real *mod; + obj = BigDecimal_DoDivmod(self,b,&div,&mod); + if(obj!=(VALUE)0) return obj; + return ToValue(div); + } else { /* div in BigDecimal sense */ + Real *res=NULL; + Real *av=NULL, *bv=NULL, *cv=NULL; + U_LONG ix = (U_LONG)GetPositiveInt(n); + U_LONG mx = (ix+VpBaseFig()*2); + GUARD_OBJ(cv,VpCreateRbObject(mx,"0")); + GUARD_OBJ(av,GetVpValue(self,1)); + GUARD_OBJ(bv,GetVpValue(b,1)); + mx = cv->MaxPrec+1; + GUARD_OBJ(res,VpCreateRbObject((mx * 2 + 2)*VpBaseFig(), "#0")); + VpDivd(cv,res,av,bv); + VpLeftRound(cv,VpGetRoundMode(),ix); + return ToValue(cv); + } } static VALUE @@ -1318,10 +1354,11 @@ Init_bigdecimal(void) rb_define_method(rb_cBigDecimal, "add", BigDecimal_add2, 2); rb_define_method(rb_cBigDecimal, "sub", BigDecimal_sub2, 2); rb_define_method(rb_cBigDecimal, "mult", BigDecimal_mult2, 2); - rb_define_method(rb_cBigDecimal, "div",BigDecimal_div2, 2); + rb_define_method(rb_cBigDecimal, "div",BigDecimal_div2, -1); rb_define_method(rb_cBigDecimal, "hash", BigDecimal_hash, 0); rb_define_method(rb_cBigDecimal, "to_s", BigDecimal_to_s, -1); rb_define_method(rb_cBigDecimal, "to_i", BigDecimal_to_i, 0); + rb_define_method(rb_cBigDecimal, "to_int", BigDecimal_to_i, 0); rb_define_method(rb_cBigDecimal, "split", BigDecimal_split, 0); rb_define_method(rb_cBigDecimal, "+", BigDecimal_add, 1); rb_define_method(rb_cBigDecimal, "-", BigDecimal_sub, 1); @@ -1329,6 +1366,7 @@ Init_bigdecimal(void) rb_define_method(rb_cBigDecimal, "-@", BigDecimal_neg, 0); rb_define_method(rb_cBigDecimal, "*", BigDecimal_mult, 1); rb_define_method(rb_cBigDecimal, "/", BigDecimal_div, 1); + rb_define_method(rb_cBigDecimal, "quo", BigDecimal_div, 1); rb_define_method(rb_cBigDecimal, "%", BigDecimal_mod, 1); rb_define_method(rb_cBigDecimal, "modulo", BigDecimal_mod, 1); rb_define_method(rb_cBigDecimal, "remainder", BigDecimal_remainder, 1); @@ -1820,6 +1858,12 @@ VpInit(U_LONG BaseVal) return DBLE_FIG; } +VP_EXPORT Real * +VpOne() +{ + return VpConstOne; +} + /* If exponent overflows,then raise exception or returns 0 */ static int AddExponent(Real *a,S_INT n) diff --git a/ext/bigdecimal/bigdecimal.h b/ext/bigdecimal/bigdecimal.h index d8fa35d3c5..0af0b1dfc5 100644 --- a/ext/bigdecimal/bigdecimal.h +++ b/ext/bigdecimal/bigdecimal.h @@ -150,11 +150,16 @@ VP_EXPORT void VpMidRound(Real *y, int f, int nf); VP_EXPORT void VpLeftRound(Real *y, int f, int nf); VP_EXPORT void VpFrac(Real *y,Real *x); VP_EXPORT int VpPower(Real *y,Real *x,S_INT n); + +/* VP constants */ +VP_EXPORT Real *VpOne(); + +#ifdef ENABLE_TRIAL_METHOD VP_EXPORT void VpPi(Real *y); VP_EXPORT void VpExp1(Real *y); VP_EXPORT void VpExp(Real *y,Real *x); VP_EXPORT void VpSinCos(Real *psin,Real *pcos,Real *x); - +#endif /* ENABLE_TRIAL_METHOD */ /* * ------------------ * MACRO definitions. diff --git a/ext/bigdecimal/bigdecimal_en.html b/ext/bigdecimal/bigdecimal_en.html index 2d86adcc6b..a8ced21e01 100644 --- a/ext/bigdecimal/bigdecimal_en.html +++ b/ext/bigdecimal/bigdecimal_en.html @@ -233,40 +233,35 @@ division(c = a / b)
For the resulting number of significant digits of c,see Resulting number of significant digits. -
  • add
  • +
  • add(b,n)
  • c = a.add(b,n)
    c = a.add(b,n) performs c = a + b. If n is less than the actual significant digits of a + b, then c is rounded properly according to the BigDecimal.limit.
    -
  • sub
  • +
  • sub(b,n)
  • c = a.sub(b,n)
    c = a.sub(b,n) performs c = a - b. If n is less than the actual significant digits of a - b, then c is rounded properly according to the BigDecimal.limit.
    -
  • mult
  • +
  • mult(b,n)
  • c = a.mult(b,n)
    c = a.mult(b,n) performs c = a * b. If n is less than the actual significant digits of a * b, then c is rounded properly according to the BigDecimal.limit.
    -
  • div
  • +
  • div(b[,n])
  • c = a.div(b,n)
    c = a.div(b,n) performs c = a / b. If n is less than the actual significant digits of a / b, -then c is rounded properly according to the BigDecimal.limit. - +then c is rounded properly according to the BigDecimal.limit.
    +If n is not given,then the result will be an integer(BigDecimal) like Float#div.
    -
  • %
  • -r = a%b
    -is the same as:
    -r = a-((a/b).floor)*b
    -
  • fix
  • c = a.fix
    returns integer part of a.
    @@ -350,26 +345,6 @@ If n<0,then the n-th digit counted from the decimal point in integer part is pro c = BigDecimal::new("1.23456").truncate(4) # ==> 1.2345 c = BigDecimal::new("15.23456").truncate(-1) # ==> 10.0 - -
    -
  • divmod
  • -c,r = a.divmod(b) # a = c*b + r
    -returns the quotient and remainder of a/b.
    -a = c * b + r is always satisfied.
    -where c is the integer satisfying -c = (a/b).floor
    -and,therefore -r = a - c*b
    - -
    -
  • remainder
  • -r=a.remainder(b)
    -returns the remainder of a/b.
    -where c is the integer satisfying -c = (a/b).fix
    -and,therefore: -r = a - c*b
    -
  • abs
  • c = a.abs
    @@ -483,6 +458,11 @@ The same as ** method.
    c = a.power(n)
    returns the value of a powered by n(c=a**n). n must be an integer.
    +
    + +
  • divmod,quo,modulo,%,remainder
  • +See,corresponding methods in Float class. +
  • <=>
  • diff --git a/ext/bigdecimal/bigdecimal_ja.html b/ext/bigdecimal/bigdecimal_ja.html index 31e852bd02..4fc3a4eb84 100644 --- a/ext/bigdecimal/bigdecimal_ja.html +++ b/ext/bigdecimal/bigdecimal_ja.html @@ -247,39 +247,35 @@ c
    -
  • add
  • +
  • add(b,n)
  • 以下のように使用します。
    c = a.add(b,n)
    c = a + b を最大で n 桁まで計算します。 a + b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。
    -
  • sub
  • +
  • sub(b,n)
  • 以下のように使用します。
    c = a.sub(b,n)
    c = a - b を最大で n 桁まで計算します。 a - b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。
    -
  • mult
  • +
  • mult(b,n)
  • 以下のように使用します。
    c = a.mult(b,n)
    c = a * b を最大で n 桁まで計算します。 a * b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。
    -
  • div
  • +
  • div(b[,n])
  • 以下のように使用します。
    c = a.div(b,n)
    c = a / b を最大で n 桁まで計算します。 -a / b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。 +a / b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。
    +n が省略されたときは Float#div と同様に結果が整数(BigDecimal)になります。
    -
  • %
  • -r = a%b
    -a/b の余りを計算します。以下の計算と同じものです。
    -r = a-((a/b).floor)*b
    -
  • fix
  • a の小数点以下の切り捨て。
    c = a.fix @@ -359,23 +355,7 @@ n c = BigDecimal("1.23456").truncate(4) # ==> 1.2345 c = BigDecimal("15.23456").truncate(-1) # ==> 10.0 - -
    -
  • divmod
  • -商と剰余の配列を返します。
    -c,r = a.divmod(b) # a = c*b + r
    -divmodメソッドは a = c * b + r となる a / b の浮動小数点型の商 c と剰余 r を -計算します。ここで c は整数(少数部分のない実数)になります。
    -c = (a/b).floor
    -r = a - c*b
    -で計算されます。
    -
  • remainder
  • -r=a.remainder(b)
    -a/b の剰余 r を計算します。
    -c = (a/b).fix
    -r = a - c*b
    -で計算されます。
  • abs
  • @@ -482,6 +462,10 @@ a c = a.sqrt(n)
    +
  • divmod,quo,modulo,%,remainder
  • +詳細は対応する Float の各メソッドを参照して下さい。 +
    +
  • <=>
  • a==b なら 0、a > b なら 1、a < b なら -1 になります。
    c = a <=> b -- cgit v1.2.3