inline static VALUE f_rational_new_bang2(VALUE klass, VALUE x, VALUE y) { assert(f_positive_p(y)); assert(f_nonzero_p(y)); return nurat_s_new_internal(klass, x, y); }
inline static VALUE f_gcd(VALUE x, VALUE y) { VALUE r = f_gcd_orig(x, y); if (f_nonzero_p(r)) { assert(f_zero_p(f_mod(x, r))); assert(f_zero_p(f_mod(y, r))); } return r; }
/* * call-seq: * cmp.to_r -> rational * * Returns the value as a rational if possible (the imaginary part * should be exactly zero). * * Complex(1, 0).to_r #=> (1/1) * Complex(1, 0.0).to_r # RangeError * Complex(1, 2).to_r # RangeError * * See rationalize. */ static VALUE nucomp_to_r(VALUE self) { get_dat1(self); if (k_inexact_p(dat->imag) || f_nonzero_p(dat->imag)) { rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational", self); } return f_to_r(dat->real); }
/* * call-seq: * cmp.to_r -> rational * * Returns the value as a rational if possible. */ static VALUE nucomp_to_r(VALUE self) { get_dat1(self); if (k_inexact_p(dat->imag) || f_nonzero_p(dat->imag)) { VALUE s = f_to_s(self); rb_raise(rb_eRangeError, "can't convert %s into Rational", StringValuePtr(s)); } return f_to_r(dat->real); }
/* * call-seq: * cmp.rationalize([eps]) -> rational * * Returns the value as a rational if possible (the imaginary part * should be exactly zero). * * Complex(1.0/3, 0).rationalize #=> (1/3) * Complex(1, 0.0).rationalize # RangeError * Complex(1, 2).rationalize # RangeError * * See to_r. */ static VALUE nucomp_rationalize(int argc, VALUE *argv, VALUE self) { get_dat1(self); rb_scan_args(argc, argv, "01", NULL); if (k_inexact_p(dat->imag) || f_nonzero_p(dat->imag)) { rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational", self); } return rb_funcall2(dat->real, rb_intern("rationalize"), argc, argv); }
static VALUE nucomp_expt(VALUE self, VALUE other) { if (k_exact_p(other) && f_zero_p(other)) return f_complex_new_bang1(CLASS_OF(self), ONE); if (k_rational_p(other) && f_one_p(f_denominator(other))) other = f_numerator(other); /* good? */ if (k_complex_p(other)) { VALUE a, r, theta, ore, oim, nr, ntheta; get_dat1(other); a = f_polar(self); r = RARRAY_PTR(a)[0]; theta = RARRAY_PTR(a)[1]; ore = dat->real; oim = dat->imag; nr = m_exp_bang(f_sub(f_mul(ore, m_log_bang(r)), f_mul(oim, theta))); ntheta = f_add(f_mul(theta, ore), f_mul(oim, m_log_bang(r))); return f_complex_polar(CLASS_OF(self), nr, ntheta); } if (k_integer_p(other)) { if (f_gt_p(other, ZERO)) { VALUE x, z, n; x = self; z = x; n = f_sub(other, ONE); while (f_nonzero_p(n)) { VALUE a; while (a = f_divmod(n, TWO), f_zero_p(RARRAY_PTR(a)[1])) { get_dat1(x); x = f_complex_new2(CLASS_OF(self), f_sub(f_mul(dat->real, dat->real), f_mul(dat->imag, dat->imag)), f_mul(f_mul(TWO, dat->real), dat->imag)); n = RARRAY_PTR(a)[0]; } z = f_mul(z, x); n = f_sub(n, ONE); } return z; } return f_expt(f_div(f_to_r(ONE), self), f_negate(other)); } if (k_numeric_p(other) && f_real_p(other)) { VALUE a, r, theta; a = f_polar(self); r = RARRAY_PTR(a)[0]; theta = RARRAY_PTR(a)[1]; return f_complex_polar(CLASS_OF(self), f_expt(r, other), f_mul(theta, other)); } return rb_num_coerce_bin(self, other, id_expt); }