static VALUE
nurat_expt(VALUE self, VALUE other)
{
if (k_numeric_p(other) && k_exact_zero_p(other))
return f_rational_new_bang1(CLASS_OF(self), ONE);
if (k_rational_p(other)) {
get_dat1(other);
if (f_one_p(dat->den))
other = dat->num; /* c14n */
}
/* Deal with special cases of 0**n and 1**n */
if (k_numeric_p(other) && k_exact_p(other)) {
get_dat1(self);
if (f_one_p(dat->den)) {
if (f_one_p(dat->num)) {
return f_rational_new_bang1(CLASS_OF(self), ONE);
}
else if (f_minus_one_p(dat->num) && k_integer_p(other)) {
return f_rational_new_bang1(CLASS_OF(self), INT2FIX(f_odd_p(other) ? -1 : 1));
}
else if (f_zero_p(dat->num)) {
if (FIX2INT(f_cmp(other, ZERO)) == -1) {
rb_raise_zerodiv();
}
else {
return f_rational_new_bang1(CLASS_OF(self), ZERO);
}
}
}
}
/* General case */
if (RB_TYPE_P(other, T_FIXNUM)) {
{
VALUE num, den;
get_dat1(self);
switch (FIX2INT(f_cmp(other, ZERO))) {
case 1:
num = f_expt(dat->num, other);
den = f_expt(dat->den, other);
break;
case -1:
num = f_expt(dat->den, f_negate(other));
den = f_expt(dat->num, f_negate(other));
break;
default:
num = ONE;
den = ONE;
break;
}
return f_rational_new2(CLASS_OF(self), num, den);
}
}
else if (RB_TYPE_P(other, T_BIGNUM)) {
rb_warn("in a**b, b may be too big");
return f_expt(f_to_f(self), other);
}
else if (RB_TYPE_P(other, T_FLOAT) || RB_TYPE_P(other, T_RATIONAL)) {
return f_expt(f_to_f(self), other);
}
else {
return rb_num_coerce_bin(self, other, id_expt);
}
}