mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* bignum.c (rb_big_float_cmp): compare an integer and float precisely.
[ruby-core:31376] [Bug #3589] reported by Tomasz Wegrzanowski. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36404 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
984729ba30
commit
94735e9e54
3 changed files with 83 additions and 1 deletions
|
@ -1,3 +1,8 @@
|
|||
Mon Jul 16 17:57:54 2012 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* bignum.c (rb_big_float_cmp): compare an integer and float precisely.
|
||||
[ruby-core:31376] [Bug #3589] reported by Tomasz Wegrzanowski.
|
||||
|
||||
Mon Jul 16 17:29:45 2012 Tanaka Akira <akr@fsij.org>
|
||||
|
||||
* bignum.c (rb_big_float_cmp): support fixnum for argument x.
|
||||
|
|
30
bignum.c
30
bignum.c
|
@ -1435,6 +1435,8 @@ VALUE
|
|||
rb_big_float_cmp(VALUE x, VALUE y)
|
||||
{
|
||||
double a = RFLOAT_VALUE(y);
|
||||
double yi, yf;
|
||||
VALUE rel;
|
||||
|
||||
if (isnan(a))
|
||||
return Qnil;
|
||||
|
@ -1442,15 +1444,41 @@ rb_big_float_cmp(VALUE x, VALUE y)
|
|||
if (a > 0.0) return INT2FIX(-1);
|
||||
else return INT2FIX(1);
|
||||
}
|
||||
yf = modf(a, &yi);
|
||||
if (FIXNUM_P(x)) {
|
||||
#if SIZEOF_LONG * CHAR_BIT < DBL_MANT_DIG /* assume FLT_RADIX == 2 */
|
||||
double xd = (double)FIX2LONG(x);
|
||||
if (xd < a)
|
||||
return INT2FIX(-1);
|
||||
if (xd > a)
|
||||
return INT2FIX(1);
|
||||
return INT2FIX(0);
|
||||
#else
|
||||
long xl, yl;
|
||||
if (yi < LONG_MIN)
|
||||
return INT2FIX(1);
|
||||
if (LONG_MAX < yi)
|
||||
return INT2FIX(-1);
|
||||
xl = FIX2LONG(x);
|
||||
yl = (long)yi;
|
||||
if (xl < yl)
|
||||
return INT2FIX(-1);
|
||||
if (xl > yl)
|
||||
return INT2FIX(1);
|
||||
if (yf < 0.0)
|
||||
return INT2FIX(1);
|
||||
if (0.0 < yf)
|
||||
return INT2FIX(-1);
|
||||
return INT2FIX(0);
|
||||
#endif
|
||||
}
|
||||
return rb_dbl_cmp(rb_big2dbl(x), a);
|
||||
y = rb_dbl2big(yi);
|
||||
rel = rb_big_cmp(x, y);
|
||||
if (yf == 0.0 || rel != INT2FIX(0))
|
||||
return rel;
|
||||
if (yf < 0.0)
|
||||
return INT2FIX(1);
|
||||
return INT2FIX(-1);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
|
@ -57,6 +57,55 @@ class TestFloat < Test::Unit::TestCase
|
|||
assert_equal(a == b, b == a)
|
||||
end
|
||||
|
||||
def test_cmp_int
|
||||
100.times {|i|
|
||||
int0 = 1 << i
|
||||
[int0, -int0].each {|int|
|
||||
flt = int.to_f
|
||||
bigger = int + 1
|
||||
smaller = int - 1
|
||||
assert_operator(flt, :==, int)
|
||||
assert_operator(flt, :>, smaller)
|
||||
assert_operator(flt, :>=, smaller)
|
||||
assert_operator(flt, :<, bigger)
|
||||
assert_operator(flt, :<=, bigger)
|
||||
assert_equal(0, flt <=> int)
|
||||
assert_equal(-1, flt <=> bigger)
|
||||
assert_equal(1, flt <=> smaller)
|
||||
assert_operator(int, :==, flt)
|
||||
assert_operator(bigger, :>, flt)
|
||||
assert_operator(bigger, :>=, flt)
|
||||
assert_operator(smaller, :<, flt)
|
||||
assert_operator(smaller, :<=, flt)
|
||||
assert_equal(0, int <=> flt)
|
||||
assert_equal(-1, smaller <=> flt)
|
||||
assert_equal(1, bigger <=> flt)
|
||||
[
|
||||
[int, flt + 0.5, bigger],
|
||||
[smaller, flt - 0.5, int]
|
||||
].each {|smaller2, flt2, bigger2|
|
||||
next if flt2 == flt2.round
|
||||
assert_operator(flt2, :!=, smaller2)
|
||||
assert_operator(flt2, :!=, bigger2)
|
||||
assert_operator(flt2, :>, smaller2)
|
||||
assert_operator(flt2, :>=, smaller2)
|
||||
assert_operator(flt2, :<, bigger2)
|
||||
assert_operator(flt2, :<=, bigger2)
|
||||
assert_equal(-1, flt2 <=> bigger2)
|
||||
assert_equal(1, flt2 <=> smaller2)
|
||||
assert_operator(smaller2, :!=, flt2)
|
||||
assert_operator(bigger2, :!=, flt2)
|
||||
assert_operator(bigger2, :>, flt2)
|
||||
assert_operator(bigger2, :>=, flt2)
|
||||
assert_operator(smaller2, :<, flt2)
|
||||
assert_operator(smaller2, :<=, flt2)
|
||||
assert_equal(-1, smaller2 <=> flt2)
|
||||
assert_equal(1, bigger2 <=> flt2)
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def test_strtod
|
||||
a = Float("0")
|
||||
assert(a.abs < Float::EPSILON)
|
||||
|
|
Loading…
Reference in a new issue