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

Fix: coerce in comparisons.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@4323 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
shigek 2003-08-05 04:07:35 +00:00
parent 5f7be2983b
commit 73b86b2f8e

View file

@ -540,16 +540,32 @@ BigDecimal_sub(VALUE self, VALUE r)
return ToValue(c);
}
static S_INT
BigDecimalCmp(VALUE self, VALUE r)
static VALUE
BigDecimalCmp(VALUE self, VALUE r,char op)
/*
* op: '='
*/
{
ENTER(5);
S_INT e;
Real *a, *b;
GUARD_OBJ(a,GetVpValue(self,1));
b = GetVpValue(r,0);
if(!b) return DoSomeOne(self,r);
if(!b) return DoSomeOne(self,r);
SAVE(b);
return VpComp(a, b);
e = VpComp(a, b);
if(e==999) return rb_float_new(VpGetDoubleNaN());
switch(op)
{
case '*': return INT2FIX(e); /* any op */
case '=': if(e==0) return Qtrue ; return Qfalse;
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;
}
rb_bug("Undefined operation in BigDecimalCmp()");
}
static VALUE
@ -569,70 +585,43 @@ BigDecimal_nonzero(VALUE self)
static VALUE
BigDecimal_comp(VALUE self, VALUE r)
{
S_INT e;
e = BigDecimalCmp(self, r);
if(e==999) return rb_float_new(VpGetDoubleNaN());
return INT2FIX(e);
return BigDecimalCmp(self, r, '*');
}
static VALUE
BigDecimal_eq(VALUE self, VALUE r)
{
ENTER(5);
Real *a, *b;
GUARD_OBJ(a,GetVpValue(self,1));
b = GetVpValue(r,0);
if(!b) return Qfalse; /* Not comparable */
SAVE(b);
return VpComp(a, b)? Qfalse:Qtrue;
return BigDecimalCmp(self, r, '=');
}
static VALUE
BigDecimal_ne(VALUE self, VALUE r)
{
ENTER(5);
Real *a, *b;
GUARD_OBJ(a,GetVpValue(self,1));
b = GetVpValue(r,0);
if(!b) return Qtrue; /* Not comparable */
SAVE(b);
return VpComp(a, b) ? Qtrue : Qfalse;
return BigDecimalCmp(self, r, '!');
}
static VALUE
BigDecimal_lt(VALUE self, VALUE r)
{
S_INT e;
e = BigDecimalCmp(self, r);
if(e==999) return Qfalse;
return(e < 0) ? Qtrue : Qfalse;
return BigDecimalCmp(self, r, '<');
}
static VALUE
BigDecimal_le(VALUE self, VALUE r)
{
S_INT e;
e = BigDecimalCmp(self, r);
if(e==999) return Qfalse;
return(e <= 0) ? Qtrue : Qfalse;
return BigDecimalCmp(self, r, 'L');
}
static VALUE
BigDecimal_gt(VALUE self, VALUE r)
{
S_INT e;
e = BigDecimalCmp(self, r);
if(e==999) return Qfalse;
return(e > 0) ? Qtrue : Qfalse;
return BigDecimalCmp(self, r, '>');
}
static VALUE
BigDecimal_ge(VALUE self, VALUE r)
{
S_INT e;
e = BigDecimalCmp(self, r);
if(e==999) return Qfalse;
return(e >= 0) ? Qtrue : Qfalse;
return BigDecimalCmp(self, r, 'G');
}
static VALUE