cdhash_cmp: can also take complex
There are complex literals `123i`, which can also be a case condition.
This commit is contained in:
parent
d0e6c6e682
commit
cc0dc67bbb
Notes:
git
2021-05-12 10:31:14 +09:00
15
compile.c
15
compile.c
@ -2005,10 +2005,15 @@ cdhash_cmp(VALUE val, VALUE lit)
|
|||||||
else if (tlit == T_FLOAT) {
|
else if (tlit == T_FLOAT) {
|
||||||
return rb_float_cmp(lit, val);
|
return rb_float_cmp(lit, val);
|
||||||
}
|
}
|
||||||
else if (tlit == T_RATIONAL) {
|
else if (tlit == T_RATIONAL) {
|
||||||
const struct RRational *dat1 = RRATIONAL(val);
|
const struct RRational *rat1 = RRATIONAL(val);
|
||||||
const struct RRational *dat2 = RRATIONAL(lit);
|
const struct RRational *rat2 = RRATIONAL(lit);
|
||||||
return (dat1->num == dat2->num) && (dat1->den == dat2->den);
|
return (rat1->num == rat2->num) && (rat1->den == rat2->den);
|
||||||
|
}
|
||||||
|
else if (tlit == T_COMPLEX) {
|
||||||
|
const struct RComplex *comp1 = RCOMPLEX(val);
|
||||||
|
const struct RComplex *comp2 = RCOMPLEX(lit);
|
||||||
|
return (comp1->real == comp2->real) && (comp1->imag == comp2->imag);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
UNREACHABLE_RETURN(-1);
|
UNREACHABLE_RETURN(-1);
|
||||||
@ -2030,6 +2035,8 @@ cdhash_hash(VALUE a)
|
|||||||
return rb_dbl_long_hash(RFLOAT_VALUE(a));
|
return rb_dbl_long_hash(RFLOAT_VALUE(a));
|
||||||
case T_RATIONAL:
|
case T_RATIONAL:
|
||||||
return rb_rational_hash(a);
|
return rb_rational_hash(a);
|
||||||
|
case T_COMPLEX:
|
||||||
|
return rb_complex_hash(a);
|
||||||
default:
|
default:
|
||||||
UNREACHABLE_RETURN(0);
|
UNREACHABLE_RETURN(0);
|
||||||
}
|
}
|
||||||
|
12
complex.c
12
complex.c
@ -1326,8 +1326,8 @@ nucomp_numerator(VALUE self)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* :nodoc: */
|
/* :nodoc: */
|
||||||
static VALUE
|
st_index_t
|
||||||
nucomp_hash(VALUE self)
|
rb_complex_hash(VALUE self)
|
||||||
{
|
{
|
||||||
st_index_t v, h[2];
|
st_index_t v, h[2];
|
||||||
VALUE n;
|
VALUE n;
|
||||||
@ -1338,7 +1338,13 @@ nucomp_hash(VALUE self)
|
|||||||
n = rb_hash(dat->imag);
|
n = rb_hash(dat->imag);
|
||||||
h[1] = NUM2LONG(n);
|
h[1] = NUM2LONG(n);
|
||||||
v = rb_memhash(h, sizeof(h));
|
v = rb_memhash(h, sizeof(h));
|
||||||
return ST2FIX(v);
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
nucomp_hash(VALUE self)
|
||||||
|
{
|
||||||
|
return ST2FIX(rb_complex_hash(self));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* :nodoc: */
|
/* :nodoc: */
|
||||||
|
@ -25,5 +25,6 @@ struct RComplex {
|
|||||||
|
|
||||||
/* complex.c */
|
/* complex.c */
|
||||||
VALUE rb_dbl_complex_new_polar_pi(double abs, double ang);
|
VALUE rb_dbl_complex_new_polar_pi(double abs, double ang);
|
||||||
|
st_index_t rb_complex_hash(VALUE comp);
|
||||||
|
|
||||||
#endif /* INTERNAL_COMPLEX_H */
|
#endif /* INTERNAL_COMPLEX_H */
|
||||||
|
@ -839,6 +839,10 @@ class Rational_Test < Test::Unit::TestCase
|
|||||||
n = case 3/2r when 1.5r then true else false end
|
n = case 3/2r when 1.5r then true else false end
|
||||||
assert_equal(n, true, '[ruby-core:103759] [Bug #17854]')
|
assert_equal(n, true, '[ruby-core:103759] [Bug #17854]')
|
||||||
RUBY
|
RUBY
|
||||||
|
assert_separately([], <<-RUBY)
|
||||||
|
n = case 1i when 1i then true else false end
|
||||||
|
assert_equal(n, true, '[ruby-core:103759] [Bug #17854]')
|
||||||
|
RUBY
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_Rational_with_invalid_exception
|
def test_Rational_with_invalid_exception
|
||||||
|
Loading…
x
Reference in New Issue
Block a user