Skip to content

Commit 700acef

Browse files
committed
merge revision(s) 23730:
* numeric.c (flo_cmp): Infinity is greater than any bignum number. [ruby-dev:38672] * bignum.c (rb_big_cmp): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23730 b2dd03c8-39d4-4d8f-98ff-823fe69b080e Signed-off-by: URABE, Shyouhei <shyouhei@ruby-lang.org> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_7@34000 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 95a7aab commit 700acef

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Mon Oct 17 04:19:39 2011 Yukihiro Matsumoto <matz@ruby-lang.org>
2+
3+
* numeric.c (flo_cmp): Infinity is greater than any bignum
4+
number. [ruby-dev:38672]
5+
6+
* bignum.c (rb_big_cmp): ditto.
7+
18
Mon Oct 17 03:56:12 2011 Yusuke Endoh <mame@tsg.ne.jp>
29

310
* ext/openssl/ossl_x509store.c (ossl_x509store_initialize): initialize

bignum.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,15 @@ rb_big_cmp(x, y)
11141114
break;
11151115

11161116
case T_FLOAT:
1117-
return rb_dbl_cmp(rb_big2dbl(x), RFLOAT(y)->value);
1117+
{
1118+
double a = RFLOAT_VALUE(y);
1119+
1120+
if (isinf(a)) {
1121+
if (a > 0.0) return INT2FIX(-1);
1122+
else return INT2FIX(1);
1123+
}
1124+
return rb_dbl_cmp(rb_big2dbl(x), a);
1125+
}
11181126

11191127
default:
11201128
return rb_num_coerce_cmp(x, y);

numeric.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,10 @@ flo_cmp(x, y)
936936
break;
937937

938938
case T_BIGNUM:
939+
if (isinf(a)) {
940+
if (a > 0.0) return INT2FIX(1);
941+
else return INT2FIX(-1);
942+
}
939943
b = rb_big2dbl(y);
940944
break;
941945

test/ruby/test_float.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,31 @@ def test_to_i
144144
assert_operator((-4611686018427387905.0).to_i, :<, 0)
145145
assert_operator((-4611686018427387906.0).to_i, :<, 0)
146146
end
147+
148+
def test_cmp
149+
inf = 1.0 / 0.0
150+
nan = inf / inf
151+
assert_equal(0, 1.0 <=> 1.0)
152+
assert_equal(1, 1.0 <=> 0.0)
153+
assert_equal(-1, 1.0 <=> 2.0)
154+
assert_nil(1.0 <=> nil)
155+
assert_nil(1.0 <=> nan)
156+
assert_nil(nan <=> 1.0)
157+
158+
assert_equal(0, 1.0 <=> 1)
159+
assert_equal(1, 1.0 <=> 0)
160+
assert_equal(-1, 1.0 <=> 2)
161+
162+
assert_equal(-1, 1.0 <=> 2**32)
163+
164+
assert_equal(1, inf <=> (Float::MAX.to_i*2))
165+
assert_equal(-1, -inf <=> (-Float::MAX.to_i*2))
166+
assert_equal(-1, (Float::MAX.to_i*2) <=> inf)
167+
assert_equal(1, (-Float::MAX.to_i*2) <=> -inf)
168+
169+
assert_raise(ArgumentError) { 1.0 > nil }
170+
assert_raise(ArgumentError) { 1.0 >= nil }
171+
assert_raise(ArgumentError) { 1.0 < nil }
172+
assert_raise(ArgumentError) { 1.0 <= nil }
173+
end
147174
end

0 commit comments

Comments
 (0)