<=>(p1)
public
Returns -1, 0, or +1 depending on whether rational is less than,
equal to, or greater than numeric.
nil is returned if the two values are incomparable.
Rational(2, 3) <=> Rational(2, 3)
Rational(5) <=> 5
Rational(2, 3) <=> Rational(1, 3)
Rational(1, 3) <=> 1
Rational(1, 3) <=> 0.3
Rational(1, 3) <=> "0.3"
Show source
VALUE
rb_rational_cmp(VALUE self, VALUE other)
{
if (RB_INTEGER_TYPE_P(other)) {
{
get_dat1(self);
if (dat->den == LONG2FIX(1))
return rb_int_cmp(dat->num, other); /* c14n */
other = f_rational_new_bang1(CLASS_OF(self), other);
goto other_is_rational;
}
}
else if (RB_FLOAT_TYPE_P(other)) {
return rb_dbl_cmp(nurat_to_double(self), RFLOAT_VALUE(other));
}
else if (RB_TYPE_P(other, T_RATIONAL)) {
other_is_rational:
{
VALUE num1, num2;
get_dat2(self, other);
if (FIXNUM_P(adat->num) && FIXNUM_P(adat->den) &&
FIXNUM_P(bdat->num) && FIXNUM_P(bdat->den)) {
num1 = f_imul(FIX2LONG(adat->num), FIX2LONG(bdat->den));
num2 = f_imul(FIX2LONG(bdat->num), FIX2LONG(adat->den));
}
else {
num1 = rb_int_mul(adat->num, bdat->den);
num2 = rb_int_mul(bdat->num, adat->den);
}
return rb_int_cmp(rb_int_minus(num1, num2), ZERO);
}
}
else {
return rb_num_coerce_cmp(self, other, rb_intern("<=>"));
}
}