summaryrefslogtreecommitdiff
path: root/ext/bigdecimal/bigdecimal.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-06-13 09:51:48 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-06-13 09:51:48 +0000
commit3bd8b390de64b002329729aecc370218035ee655 (patch)
tree403d28900fe0f51fe47b85aaaf685458956a9635 /ext/bigdecimal/bigdecimal.c
parent58bae71a7b023b5bb5fdcfefb46232f3f14bc519 (diff)
* ext/bigdecimal/bigdecimal.c (BigDecimalCmp): use GetVpValueWithPrec
for Float and Rational arguments. * test/bigdecimal/test_bigdecimal.rb (test_new, test_cmp, test_power): add and modify tests for the above change. * ext/bigdecimal/bigdecimal.c (BigDecimalCmp): modify coding style to match ruby's standard. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32052 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/bigdecimal/bigdecimal.c')
-rw-r--r--ext/bigdecimal/bigdecimal.c96
1 files changed, 76 insertions, 20 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 388dbbd..c21dd8c 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -794,34 +794,90 @@ BigDecimalCmp(VALUE self, VALUE r,char op)
{
ENTER(5);
SIGNED_VALUE e;
- Real *a, *b;
+ Real *a, *b=0;
GUARD_OBJ(a,GetVpValue(self,1));
- b = GetVpValue(r,0);
- if(!b) {
+ switch (TYPE(r)) {
+ case T_DATA:
+ if (!rb_typeddata_is_kind_of(r, &BigDecimal_data_type)) break;
+ /* fall through */
+ case T_FIXNUM:
+ /* fall through */
+ case T_BIGNUM:
+ GUARD_OBJ(b, GetVpValue(r,0));
+ break;
+
+ case T_FLOAT:
+ GUARD_OBJ(b, GetVpValueWithPrec(r, DBL_DIG+1, 0));
+ break;
+
+ case T_RATIONAL:
+ GUARD_OBJ(b, GetVpValueWithPrec(r, a->Prec*VpBaseFig(), 0));
+ break;
+
+ default:
+ break;
+ }
+ if (b == NULL) {
ID f = 0;
- switch(op)
- {
- case '*': return rb_num_coerce_cmp(self,r,rb_intern("<=>"));
- case '=': return RTEST(rb_num_coerce_cmp(self,r,rb_intern("=="))) ? Qtrue : Qfalse;
- case 'G': f = rb_intern(">="); break;
- case 'L': f = rb_intern("<="); break;
- case '>': case '<': f = (ID)op; break;
+ switch (op) {
+ case '*':
+ return rb_num_coerce_cmp(self, r, rb_intern("<=>"));
+
+ case '=':
+ return RTEST(rb_num_coerce_cmp(self, r, rb_intern("=="))) ? Qtrue : Qfalse;
+
+ case 'G':
+ f = rb_intern(">=");
+ break;
+
+ case 'L':
+ f = rb_intern("<=");
+ break;
+
+ case '>':
+ /* fall through */
+ case '<':
+ f = (ID)op;
+ break;
+
+ default:
+ break;
}
- return rb_num_coerce_relop(self,r,f);
+ return rb_num_coerce_relop(self, r, f);
}
SAVE(b);
e = VpComp(a, b);
- if(e==999) return (op == '*') ? Qnil : Qfalse;
- switch(op)
- {
- case '*': return INT2FIX(e); /* any op */
- case '=': if(e==0) return Qtrue ; return Qfalse;
- case 'G': if(e>=0) return Qtrue ; return Qfalse;
- case '>': if(e> 0) return Qtrue ; return Qfalse;
- case 'L': if(e<=0) return Qtrue ; return Qfalse;
- case '<': if(e< 0) return Qtrue ; return Qfalse;
+ if (e == 999)
+ return (op == '*') ? Qnil : Qfalse;
+ switch (op) {
+ case '*':
+ return INT2FIX(e); /* any op */
+
+ case '=':
+ if(e==0) return Qtrue;
+ return Qfalse;
+
+ case 'G':
+ if(e>=0) return Qtrue;
+ return Qfalse;
+
+ case '>':
+ if(e> 0) return Qtrue;
+ return Qfalse;
+
+ case 'L':
+ if(e<=0) return Qtrue;
+ return Qfalse;
+
+ case '<':
+ if(e< 0) return Qtrue;
+ return Qfalse;
+
+ default:
+ break;
}
+
rb_bug("Undefined operation in BigDecimalCmp()");
}