summaryrefslogtreecommitdiff
path: root/ext/bigdecimal
diff options
context:
space:
mode:
Diffstat (limited to 'ext/bigdecimal')
-rw-r--r--ext/bigdecimal/bigdecimal.c84
-rw-r--r--ext/bigdecimal/bigdecimal_en.html19
-rw-r--r--ext/bigdecimal/bigdecimal_ja.html17
3 files changed, 69 insertions, 51 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 029c6f4c9d..75db02c3b7 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -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);
-
- 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);
+ 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);
+ }
}
}
@@ -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
diff --git a/ext/bigdecimal/bigdecimal_en.html b/ext/bigdecimal/bigdecimal_en.html
index 1197be15b9..7f6faa39b9 100644
--- a/ext/bigdecimal/bigdecimal_en.html
+++ b/ext/bigdecimal/bigdecimal_en.html
@@ -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>
diff --git a/ext/bigdecimal/bigdecimal_ja.html b/ext/bigdecimal/bigdecimal_ja.html
index a1b996b22f..b43278e616 100644
--- a/ext/bigdecimal/bigdecimal_ja.html
+++ b/ext/bigdecimal/bigdecimal_ja.html
@@ -252,22 +252,24 @@ c の精度については「<A HREF="#PREC">計算精度について</A>」を参照してください。
<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 の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます
c = a.div(b,n)<BR>
c = a / b を最大で n 桁まで計算します。
a / b の精度が n より大きいときは BigDecimal.mode で指定された方法で丸められます。<BR>
+n がゼロなら / と同じです。<BR>
n が省略されたときは Float#div と同様に結果が整数(BigDecimal)になります。
</BLOCKQUOTE>