summaryrefslogtreecommitdiff
path: root/complex.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-01 07:34:31 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-09-01 07:34:31 +0000
commit929e9713bbfd76140bced29c6f398904ae9d4a85 (patch)
treeabb04468573060b8a2ee3a5c56cbd717d0cfbb1a /complex.c
parent9563db5eb18eae0e59da22f000ef21ed7516c179 (diff)
complex.c: simplify division result
* complex.c (f_divide): canonicalize rationals to simplify integer complex results. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64610 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'complex.c')
-rw-r--r--complex.c22
1 files changed, 8 insertions, 14 deletions
diff --git a/complex.c b/complex.c
index 71c24a0024..ff6e3e038e 100644
--- a/complex.c
+++ b/complex.c
@@ -774,6 +774,7 @@ f_divide(VALUE self, VALUE other,
VALUE (*func)(VALUE, VALUE), ID id)
{
if (RB_TYPE_P(other, T_COMPLEX)) {
+ VALUE r, n, x, y;
int flo;
get_dat2(self, other);
@@ -781,35 +782,28 @@ f_divide(VALUE self, VALUE other,
RB_FLOAT_TYPE_P(bdat->real) || RB_FLOAT_TYPE_P(bdat->imag));
if (f_gt_p(f_abs(bdat->real), f_abs(bdat->imag))) {
- VALUE r, n;
-
r = (*func)(bdat->imag, bdat->real);
n = f_mul(bdat->real, f_add(ONE, f_mul(r, r)));
if (flo)
return f_complex_new2(CLASS_OF(self),
(*func)(self, n),
(*func)(f_negate(f_mul(self, r)), n));
- return f_complex_new2(CLASS_OF(self),
- (*func)(f_add(adat->real,
- f_mul(adat->imag, r)), n),
- (*func)(f_sub(adat->imag,
- f_mul(adat->real, r)), n));
+ x = (*func)(f_add(adat->real, f_mul(adat->imag, r)), n);
+ y = (*func)(f_sub(adat->imag, f_mul(adat->real, r)), n);
}
else {
- VALUE r, n;
-
r = (*func)(bdat->real, bdat->imag);
n = f_mul(bdat->imag, f_add(ONE, f_mul(r, r)));
if (flo)
return f_complex_new2(CLASS_OF(self),
(*func)(f_mul(self, r), n),
(*func)(f_negate(self), n));
- return f_complex_new2(CLASS_OF(self),
- (*func)(f_add(f_mul(adat->real, r),
- adat->imag), n),
- (*func)(f_sub(f_mul(adat->imag, r),
- adat->real), n));
+ x = (*func)(f_add(f_mul(adat->real, r), adat->imag), n);
+ y = (*func)(f_sub(f_mul(adat->imag, r), adat->real), n);
}
+ x = rb_rational_canonicalize(x);
+ y = rb_rational_canonicalize(y);
+ return f_complex_new2(CLASS_OF(self), x, y);
}
if (k_numeric_p(other) && f_real_p(other)) {
get_dat1(self);