diff --git a/ChangeLog b/ChangeLog index 6ba3c7b6cd..0997df6c77 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +Mon Jul 16 15:00:45 2012 Tanaka Akira + + * internal.h (rb_big_float_cmp): declared. + + * bignum.c (rb_big_float_cmp): extracted from rb_big_cmp and big_op. + (rb_big_cmp): use rb_big_float_cmp. + (big_op): ditto. + + * numeric.c (flo_cmp): use rb_big_float_cmp. + (flo_gt): ditto. + (flo_ge): ditto. + (flo_lt): ditto. + (flo_le): ditto. + Mon Jul 16 14:14:21 2012 Tanaka Akira * bignum.c (enum big_op_t): new type. diff --git a/bignum.c b/bignum.c index 43669db4a7..839a866ae9 100644 --- a/bignum.c +++ b/bignum.c @@ -1431,6 +1431,18 @@ rb_big_to_f(VALUE x) return DBL2NUM(rb_big2dbl(x)); } +VALUE +rb_big_float_cmp(VALUE x, VALUE y) +{ + double a = RFLOAT_VALUE(y); + + if (isinf(a)) { + if (a > 0.0) return INT2FIX(-1); + else return INT2FIX(1); + } + return rb_dbl_cmp(rb_big2dbl(x), a); +} + /* * call-seq: * big <=> numeric -> -1, 0, +1 or nil @@ -1456,15 +1468,7 @@ rb_big_cmp(VALUE x, VALUE y) break; case T_FLOAT: - { - double a = RFLOAT_VALUE(y); - - if (isinf(a)) { - if (a > 0.0) return INT2FIX(-1); - else return INT2FIX(1); - } - return rb_dbl_cmp(rb_big2dbl(x), a); - } + return rb_big_float_cmp(x, y); default: return rb_num_coerce_cmp(x, y, rb_intern("<=>")); @@ -1507,17 +1511,8 @@ big_op(VALUE x, VALUE y, enum big_op_t op) break; case T_FLOAT: - { - double a = RFLOAT_VALUE(y); - - if (isinf(a)) { - if (a > 0.0) rel = INT2FIX(-1); - else rel = INT2FIX(1); - break; - } - rel = rb_dbl_cmp(rb_big2dbl(x), a); - break; - } + rel = rb_big_float_cmp(x, y); + break; default: { diff --git a/internal.h b/internal.h index cc4a263694..d213539b25 100644 --- a/internal.h +++ b/internal.h @@ -49,6 +49,7 @@ VALUE rb_ary_cat(VALUE, const VALUE *, long); /* bignum.c */ VALUE rb_big_fdiv(VALUE x, VALUE y); VALUE rb_big_uminus(VALUE x); +VALUE rb_big_float_cmp(VALUE x, VALUE y); /* class.c */ VALUE rb_obj_methods(int argc, VALUE *argv, VALUE obj); diff --git a/numeric.c b/numeric.c index 42e923fe5a..cf4fc3f181 100644 --- a/numeric.c +++ b/numeric.c @@ -1128,12 +1128,12 @@ flo_cmp(VALUE x, VALUE y) break; case T_BIGNUM: - if (isinf(a)) { - if (a > 0.0) return INT2FIX(1); - else return INT2FIX(-1); - } - b = rb_big2dbl(y); - break; + { + VALUE rel = rb_big_float_cmp(y, x); + if (FIXNUM_P(rel)) + return INT2FIX(-FIX2INT(rel)); + return rel; + } case T_FLOAT: b = RFLOAT_VALUE(y); @@ -1173,8 +1173,12 @@ flo_gt(VALUE x, VALUE y) break; case T_BIGNUM: - b = rb_big2dbl(y); - break; + { + VALUE rel = rb_big_float_cmp(y, x); + if (FIXNUM_P(rel)) + return -FIX2INT(rel) > 0 ? Qtrue : Qfalse; + return Qfalse; + } case T_FLOAT: b = RFLOAT_VALUE(y); @@ -1212,8 +1216,12 @@ flo_ge(VALUE x, VALUE y) break; case T_BIGNUM: - b = rb_big2dbl(y); - break; + { + VALUE rel = rb_big_float_cmp(y, x); + if (FIXNUM_P(rel)) + return -FIX2INT(rel) >= 0 ? Qtrue : Qfalse; + return Qfalse; + } case T_FLOAT: b = RFLOAT_VALUE(y); @@ -1250,8 +1258,12 @@ flo_lt(VALUE x, VALUE y) break; case T_BIGNUM: - b = rb_big2dbl(y); - break; + { + VALUE rel = rb_big_float_cmp(y, x); + if (FIXNUM_P(rel)) + return -FIX2INT(rel) < 0 ? Qtrue : Qfalse; + return Qfalse; + } case T_FLOAT: b = RFLOAT_VALUE(y); @@ -1289,8 +1301,12 @@ flo_le(VALUE x, VALUE y) break; case T_BIGNUM: - b = rb_big2dbl(y); - break; + { + VALUE rel = rb_big_float_cmp(y, x); + if (FIXNUM_P(rel)) + return -FIX2INT(rel) <= 0 ? Qtrue : Qfalse; + return Qfalse; + } case T_FLOAT: b = RFLOAT_VALUE(y);