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

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>
* process.c: bug fix

View file

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

View file

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