The 2nd arg for add,sub,mult, and div is 0, then result will be same as +,-,*,/ respectively.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4461 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shigek 2003-08-29 13:37:44 +00:00
parent 8ab2410757
commit da84ad63ee
4 changed files with 73 additions and 50 deletions

View File

@ -1,3 +1,8 @@
Fri Aug 29 22:35:00 2003 Shigeo Kobayashi <shigek@ruby-lang.org>
* bigdecimal.c *.html: The 2nd arg. for add,sub,mult, and div is 0,
then result will be the same as +,-,*,/ respectively.
Fri Aug 29 17:30:15 2003 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> Fri Aug 29 17:30:15 2003 Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp>
* process.c: bug fix * process.c: bug fix

View File

@ -284,7 +284,7 @@ GetPositiveInt(VALUE v)
S_INT n; S_INT n;
Check_Type(v, T_FIXNUM); Check_Type(v, T_FIXNUM);
n = FIX2INT(v); n = FIX2INT(v);
if(n <= 0) { if(n < 0) {
rb_raise(rb_eArgError, "argument must be positive"); rb_raise(rb_eArgError, "argument must be positive");
} }
return n; return n;
@ -785,33 +785,36 @@ BigDecimal_divmod(VALUE self, VALUE r)
static VALUE static VALUE
BigDecimal_div2(int argc, VALUE *argv, VALUE self) BigDecimal_div2(int argc, VALUE *argv, VALUE self)
{ {
ENTER(10); ENTER(5);
VALUE obj;
VALUE b,n; VALUE b,n;
int na = rb_scan_args(argc,argv,"11",&b,&n); int na = rb_scan_args(argc,argv,"11",&b,&n);
if(na==1) { /* div in Float sense */ if(na==1) { /* div in Float sense */
VALUE obj;
Real *div=NULL; Real *div=NULL;
Real *mod; Real *mod;
obj = BigDecimal_DoDivmod(self,b,&div,&mod); obj = BigDecimal_DoDivmod(self,b,&div,&mod);
if(obj!=(VALUE)0) return obj; if(obj!=(VALUE)0) return obj;
return ToValue(div); return ToValue(div);
} else { /* div in BigDecimal sense */ } else { /* div in BigDecimal sense */
Real *res=NULL;
Real *av=NULL, *bv=NULL, *cv=NULL;
U_LONG ix = (U_LONG)GetPositiveInt(n); U_LONG ix = (U_LONG)GetPositiveInt(n);
U_LONG mx = (ix+VpBaseFig()*2); if(ix==0) return BigDecimal_div(self,b);
U_LONG pl = VpSetPrecLimit(0); else {
Real *res=NULL;
Real *av=NULL, *bv=NULL, *cv=NULL;
U_LONG mx = (ix+VpBaseFig()*2);
U_LONG pl = VpSetPrecLimit(0);
GUARD_OBJ(cv,VpCreateRbObject(mx,"0")); GUARD_OBJ(cv,VpCreateRbObject(mx,"0"));
GUARD_OBJ(av,GetVpValue(self,1)); GUARD_OBJ(av,GetVpValue(self,1));
GUARD_OBJ(bv,GetVpValue(b,1)); GUARD_OBJ(bv,GetVpValue(b,1));
mx = av->Prec + bv->Prec + 2; mx = av->Prec + bv->Prec + 2;
if(mx <= cv->MaxPrec) mx = cv->MaxPrec+1; if(mx <= cv->MaxPrec) mx = cv->MaxPrec+1;
GUARD_OBJ(res,VpCreateRbObject((mx * 2 + 2)*VpBaseFig(), "#0")); GUARD_OBJ(res,VpCreateRbObject((mx * 2 + 2)*VpBaseFig(), "#0"));
VpDivd(cv,res,av,bv); VpDivd(cv,res,av,bv);
VpSetPrecLimit(pl); VpSetPrecLimit(pl);
VpLeftRound(cv,VpGetRoundMode(),ix); VpLeftRound(cv,VpGetRoundMode(),ix);
return ToValue(cv); return ToValue(cv);
}
} }
} }
@ -821,12 +824,15 @@ BigDecimal_add2(VALUE self, VALUE b, VALUE n)
ENTER(2); ENTER(2);
Real *cv; Real *cv;
U_LONG mx = (U_LONG)GetPositiveInt(n); U_LONG mx = (U_LONG)GetPositiveInt(n);
U_LONG pl = VpSetPrecLimit(0); if(mx==0) return BigDecimal_add(self,b);
VALUE c = BigDecimal_add(self,b); else {
VpSetPrecLimit(pl); U_LONG pl = VpSetPrecLimit(0);
GUARD_OBJ(cv,GetVpValue(c,1)); VALUE c = BigDecimal_add(self,b);
VpLeftRound(cv,VpGetRoundMode(),mx); VpSetPrecLimit(pl);
return ToValue(cv); GUARD_OBJ(cv,GetVpValue(c,1));
VpLeftRound(cv,VpGetRoundMode(),mx);
return ToValue(cv);
}
} }
static VALUE static VALUE
@ -835,12 +841,15 @@ BigDecimal_sub2(VALUE self, VALUE b, VALUE n)
ENTER(2); ENTER(2);
Real *cv; Real *cv;
U_LONG mx = (U_LONG)GetPositiveInt(n); U_LONG mx = (U_LONG)GetPositiveInt(n);
U_LONG pl = VpSetPrecLimit(0); if(mx==0) return BigDecimal_sub(self,b);
VALUE c = BigDecimal_sub(self,b); else {
VpSetPrecLimit(pl); U_LONG pl = VpSetPrecLimit(0);
GUARD_OBJ(cv,GetVpValue(c,1)); VALUE c = BigDecimal_sub(self,b);
VpLeftRound(cv,VpGetRoundMode(),mx); VpSetPrecLimit(pl);
return ToValue(cv); GUARD_OBJ(cv,GetVpValue(c,1));
VpLeftRound(cv,VpGetRoundMode(),mx);
return ToValue(cv);
}
} }
static VALUE static VALUE
@ -849,12 +858,15 @@ BigDecimal_mult2(VALUE self, VALUE b, VALUE n)
ENTER(2); ENTER(2);
Real *cv; Real *cv;
U_LONG mx = (U_LONG)GetPositiveInt(n); U_LONG mx = (U_LONG)GetPositiveInt(n);
U_LONG pl = VpSetPrecLimit(0); if(mx==0) BigDecimal_mult(self,b);
VALUE c = BigDecimal_mult(self,b); else {
VpSetPrecLimit(pl); U_LONG pl = VpSetPrecLimit(0);
GUARD_OBJ(cv,GetVpValue(c,1)); VALUE c = BigDecimal_mult(self,b);
VpLeftRound(cv,VpGetRoundMode(),mx); VpSetPrecLimit(pl);
return ToValue(cv); GUARD_OBJ(cv,GetVpValue(c,1));
VpLeftRound(cv,VpGetRoundMode(),mx);
return ToValue(cv);
}
} }
static VALUE static VALUE

View File

@ -238,30 +238,33 @@ For the resulting number of significant digits of c,see <A HREF="#PREC">Resultin
<LI><B>add(b,n)</B></LI><BLOCKQUOTE> <LI><B>add(b,n)</B></LI><BLOCKQUOTE>
c = a.add(b,n)<BR> c = a.add(b,n)<BR>
c = a.add(b,n) performs c = a + b. c = a.add(b,n) performs c = a + b.<BR>
If n is less than the actual significant digits of 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.<BR>
If n is zero,then the result is the same as +'s.
</BLOCKQUOTE> </BLOCKQUOTE>
<LI><B>sub(b,n)</B></LI><BLOCKQUOTE> <LI><B>sub(b,n)</B></LI><BLOCKQUOTE>
c = a.sub(b,n)<BR> c = a.sub(b,n)<BR>
c = a.sub(b,n) performs c = a - b. c = a.sub(b,n) performs c = a - b.<BR>
If n is less than the actual significant digits of 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.<BR>
If n is zero,then the result is the same as -'s.
</BLOCKQUOTE> </BLOCKQUOTE>
<LI><B>mult(b,n)</B></LI><BLOCKQUOTE> <LI><B>mult(b,n)</B></LI><BLOCKQUOTE>
c = a.mult(b,n)<BR> c = a.mult(b,n)<BR>
c = a.mult(b,n) performs c = a * b. c = a.mult(b,n) performs c = a * b.<BR>
If n is less than the actual significant digits of 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.<BR>
If n is zero,then the result is the same as *'s.
</BLOCKQUOTE> </BLOCKQUOTE>
<LI><B>div(b[,n])</B></LI><BLOCKQUOTE> <LI><B>div(b[,n])</B></LI><BLOCKQUOTE>
c = a.div(b,n)<BR> c = a.div(b,n)<BR>
c = a.div(b,n) performs c = a / b. c = a.div(b,n) performs c = a / b.<BR>
If n is less than the actual significant digits of a / b, If n is less than the actual significant digits of a / b,
then c is rounded properly according to the BigDecimal.limit.<BR> then c is rounded properly according to the BigDecimal.limit.<BR>
If n is zero,then the result is the same as /'s.
If n is not given,then the result will be an integer(BigDecimal) like Float#div. If n is not given,then the result will be an integer(BigDecimal) like Float#div.
</BLOCKQUOTE> </BLOCKQUOTE>

View File

@ -252,22 +252,24 @@ c
<LI><B>add(b,n)</B></LI><BLOCKQUOTE> <LI><B>add(b,n)</B></LI><BLOCKQUOTE>
以下のように使用します。<BR> 以下のように使用します。<BR>
c = a.add(b,n)<BR> c = a.add(b,n)<BR>
c = a + b を最大で n 桁まで計算します。 c = a + b を最大で n 桁まで計算します。<BR>
a + b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。 a + b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。<BR>
n がゼロなら + と同じです。
</BLOCKQUOTE> </BLOCKQUOTE>
<LI><B>sub(b,n)</B></LI><BLOCKQUOTE> <LI><B>sub(b,n)</B></LI><BLOCKQUOTE>
以下のように使用します。<BR> 以下のように使用します。<BR>
c = a.sub(b,n)<BR> c = a.sub(b,n)<BR>
c = a - b を最大で n 桁まで計算します。 c = a - b を最大で n 桁まで計算します。<BR>
a - b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。 a - b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。<BR>
n がゼロなら - と同じです。
</BLOCKQUOTE> </BLOCKQUOTE>
<LI><B>mult(b,n)</B></LI><BLOCKQUOTE> <LI><B>mult(b,n)</B></LI><BLOCKQUOTE>
以下のように使用します。<BR> 以下のように使用します。<BR>
c = a.mult(b,n)<BR> c = a.mult(b,n)<BR>
c = a * b を最大で n 桁まで計算します。 c = a * b を最大で n 桁まで計算します。<BR>
a * b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。 a * b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。<BR>
n がゼロなら * と同じです。
</BLOCKQUOTE> </BLOCKQUOTE>
<LI><B>div(b[,n])</B></LI><BLOCKQUOTE> <LI><B>div(b[,n])</B></LI><BLOCKQUOTE>
@ -275,6 +277,7 @@ a * b
c = a.div(b,n)<BR> c = a.div(b,n)<BR>
c = a / b を最大で n 桁まで計算します。 c = a / b を最大で n 桁まで計算します。
a / b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。<BR> a / b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。<BR>
n がゼロなら / と同じです。<BR>
n が省略されたときは Float#div と同様に結果が整数(BigDecimal)になります。 n が省略されたときは Float#div と同様に結果が整数(BigDecimal)になります。
</BLOCKQUOTE> </BLOCKQUOTE>