* bignum.c (big_gt, big_ge, big_lt, big_ge): added Bignum#>, >=, < and

<= to allow to compare with BigDecimal.  [ruby-dev:40167]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@26574 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2010-02-04 15:36:29 +00:00
parent 72ed7981fb
commit f574df443a
2 changed files with 116 additions and 0 deletions

View File

@ -1,3 +1,8 @@
Fri Feb 5 00:34:24 2010 Yusuke Endoh <mame@tsg.ne.jp>
* bignum.c (big_gt, big_ge, big_lt, big_ge): added Bignum#>, >=, < and
<= to allow to compare with BigDecimal. [ruby-dev:40167]
Thu Feb 4 15:47:27 2010 NARUSE, Yui <naruse@ruby-lang.org>
* thread_pthread.c (native_thread_init_stack): use get_stack.

111
bignum.c
View File

@ -1329,6 +1329,113 @@ rb_big_cmp(VALUE x, VALUE y)
(RBIGNUM_SIGN(x) ? INT2FIX(-1) : INT2FIX(1));
}
static VALUE
big_op(VALUE x, VALUE y, int op)
{
long xlen = RBIGNUM_LEN(x);
BDIGIT *xds, *yds;
VALUE rel;
int n;
switch (TYPE(y)) {
case T_FIXNUM:
case T_BIGNUM:
rel = rb_big_cmp(x, y);
break;
case T_FLOAT:
{
double a = RFLOAT_VALUE(y);
if (isinf(a)) {
if (a > 0.0) return INT2FIX(-1);
else return INT2FIX(1);
}
rel = rb_dbl_cmp(rb_big2dbl(x), a);
break;
}
default:
{
ID id = 0;
switch (op) {
case 0: id = '>'; break;
case 1: id = rb_intern(">="); break;
case 2: id = '<'; break;
case 3: id = rb_intern("<="); break;
}
return rb_num_coerce_relop(x, y, id);
}
}
if (NIL_P(rel)) return Qfalse;
n = FIX2INT(rel);
switch (op) {
case 0: return n > 0 ? Qtrue : Qfalse;
case 1: return n >= 0 ? Qtrue : Qfalse;
case 2: return n < 0 ? Qtrue : Qfalse;
case 3: return n <= 0 ? Qtrue : Qfalse;
}
return Qundef;
}
/*
* call-seq:
* big > real -> true or false
*
* Returns <code>true</code> if the value of <code>big</code> is
* greater than that of <code>real</code>.
*/
static VALUE
big_gt(VALUE x, VALUE y)
{
return big_op(x, y, 0);
}
/*
* call-seq:
* big >= real -> true or false
*
* Returns <code>true</code> if the value of <code>big</code> is
* greater than or equal to that of <code>real</code>.
*/
static VALUE
big_ge(VALUE x, VALUE y)
{
return big_op(x, y, 1);
}
/*
* call-seq:
* big < real -> true or false
*
* Returns <code>true</code> if the value of <code>big</code> is
* less than that of <code>real</code>.
*/
static VALUE
big_lt(VALUE x, VALUE y)
{
return big_op(x, y, 2);
}
/*
* call-seq:
* big <= real -> true or false
*
* Returns <code>true</code> if the value of <code>big</code> is
* less than or equal to that of <code>real</code>.
*/
static VALUE
big_le(VALUE x, VALUE y)
{
return big_op(x, y, 3);
}
/*
* call-seq:
* big == obj => true or false
@ -3286,6 +3393,10 @@ Init_Bignum(void)
rb_define_method(rb_cBignum, "<=>", rb_big_cmp, 1);
rb_define_method(rb_cBignum, "==", rb_big_eq, 1);
rb_define_method(rb_cBignum, ">", big_gt, 1);
rb_define_method(rb_cBignum, ">=", big_ge, 1);
rb_define_method(rb_cBignum, "<", big_lt, 1);
rb_define_method(rb_cBignum, "<=", big_le, 1);
rb_define_method(rb_cBignum, "===", rb_big_eq, 1);
rb_define_method(rb_cBignum, "eql?", rb_big_eql, 1);
rb_define_method(rb_cBignum, "hash", rb_big_hash, 0);