/* * call-seq: * cmp == object -> true or false * * Returns true if cmp equals object numerically. * * Complex(2, 3) == Complex(2, 3) #=> true * Complex(5) == 5 #=> true * Complex(0) == 0.0 #=> true * Complex('1/3') == 0.33 #=> false * Complex('1/2') == '1/2' #=> false */ static VALUE nucomp_eqeq_p(VALUE self, VALUE other) { if (k_complex_p(other)) { get_dat2(self, other); return f_boolcast(f_eqeq_p(adat->real, bdat->real) && f_eqeq_p(adat->imag, bdat->imag)); } if (k_numeric_p(other) && f_real_p(other)) { get_dat1(self); return f_boolcast(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag)); } return f_eqeq_p(other, self); }
inline static VALUE f_imul(long x, long y) { VALUE r = f_imul_orig(x, y); assert(f_eqeq_p(r, f_mul(LONG2NUM(x), LONG2NUM(y)))); return r; }
/* :nodoc: */ static VALUE nucomp_eql_p(VALUE self, VALUE other) { if (k_complex_p(other)) { get_dat2(self, other); return f_boolcast((CLASS_OF(adat->real) == CLASS_OF(bdat->real)) && (CLASS_OF(adat->imag) == CLASS_OF(bdat->imag)) && f_eqeq_p(self, other)); } return Qfalse; }
/* * call-seq: * rat == object -> true or false * * Returns true if rat equals object numerically. * * For example: * * Rational(2, 3) == Rational(2, 3) #=> true * Rational(5) == 5 #=> true * Rational(0) == 0.0 #=> true * Rational('1/3') == 0.33 #=> false * Rational('1/2') == '1/2' #=> false */ static VALUE nurat_eqeq_p(VALUE self, SEL sel, VALUE other) { switch (TYPE(other)) { case T_FIXNUM: case T_BIGNUM: { get_dat1(self); if (f_zero_p(dat->num) && f_zero_p(other)) return Qtrue; if (!FIXNUM_P(dat->den)) return Qfalse; if (FIX2LONG(dat->den) != 1) return Qfalse; if (f_eqeq_p(dat->num, other)) return Qtrue; return Qfalse; } case T_FLOAT: return f_eqeq_p(f_to_f(self), other); case T_RATIONAL: { get_dat2(self, other); if (f_zero_p(adat->num) && f_zero_p(bdat->num)) return Qtrue; return f_boolcast(f_eqeq_p(adat->num, bdat->num) && f_eqeq_p(adat->den, bdat->den)); } default: return f_eqeq_p(other, self); } }