Backport #2349 ยป bigdec.diff
ext/bigdecimal/bigdecimal.c  

724  724  
725  725 
switch(op) 
726  726 
{ 
727 
case '*': f = rb_intern("<=>");break; 

728 
case '=': f = rb_intern("=="); break; 

729 
case '!': f = rb_intern("!="); break; 

727 
case '*': return rb_num_coerce_cmp(self,r,rb_intern("<=>")); 

728 
case '=': return RTEST(rb_num_coerce_cmp(self,r,rb_intern("=="))) ? Qtrue : Qfalse; 

730  729 
case 'G': f = rb_intern(">="); break; 
731  730 
case 'L': f = rb_intern("<="); break; 
732  731 
case '>': case '<': f = (ID)op; break; 
733  732 
} 
734 
return rb_num_coerce_cmp(self,r,f);


733 
return rb_num_coerce_relop(self,r,f);


735  734 
} 
736  735 
SAVE(b); 
737  736 
e = VpComp(a, b); 
738 
if(e==999) return Qnil;


737 
if(e==999) return (op == '*') ? Qnil : Qfalse;


739  738 
switch(op) 
740  739 
{ 
741  740 
case '*': return INT2FIX(e); /* any op */ 
742  741 
case '=': if(e==0) return Qtrue ; return Qfalse; 
743 
case '!': if(e!=0) return Qtrue ; return Qfalse; 

744  742 
case 'G': if(e>=0) return Qtrue ; return Qfalse; 
745  743 
case '>': if(e> 0) return Qtrue ; return Qfalse; 
746  744 
case 'L': if(e<=0) return Qtrue ; return Qfalse; 
test/bigdecimal/test_bigdecimal.rb  

61  61 
x = BigDecimal.new("0.1") 
62  62 
100.times do 
63  63 
x *= x 
64 
break if x == false 

65  64 
end 
66  65 
end 
67  66 
end 
...  ...  
71  70 
x = BigDecimal.new("10") 
72  71 
100.times do 
73  72 
x *= x 
74 
break if x == false 

75  73 
end 
76  74 
end 
77  75 
end 
...  ...  
219  217 
assert_operator(1, :<, inf) 
220  218 
end 
221  219  
222 
def test_cmp_corece 

220 
def test_cmp_nan 

221 
n1 = BigDecimal.new("1") 

222 
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false) 

223 
assert_equal(nil, BigDecimal.new("NaN") <=> n1) 

224 
assert_equal(false, BigDecimal.new("NaN") > n1) 

225 
end 

226  
227 
def test_cmp_failing_coercion 

228 
n1 = BigDecimal.new("1") 

229 
assert_equal(nil, n1 <=> nil) 

230 
assert_raise(ArgumentError){n1 > nil} 

231 
end 

232  
233 
def test_cmp_coerce 

223  234 
n1 = BigDecimal.new("1") 
224  235 
n2 = BigDecimal.new("2") 
225  236 
o1 = Object.new; def o1.coerce(x); [x, BigDecimal.new("1")]; end 