summaryrefslogtreecommitdiffhomepage
path: root/mrbgems
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2021-02-06 00:40:18 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2021-02-06 00:40:18 +0900
commit8ecd38d64879536b1c26c19b0a32246e520d6352 (patch)
treeeb5e7e22810d92ef86123e1107ba8b2cf6b915f0 /mrbgems
parentea0737ecfd2576693e078c0c7c614671afb0e2b0 (diff)
downloadmruby-8ecd38d64879536b1c26c19b0a32246e520d6352.tar.gz
mruby-8ecd38d64879536b1c26c19b0a32246e520d6352.zip
Reimplement complex division.
Diffstat (limited to 'mrbgems')
-rw-r--r--mrbgems/mruby-cmath/src/cmath.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/mrbgems/mruby-cmath/src/cmath.c b/mrbgems/mruby-cmath/src/cmath.c
index 19da2eae2..14e8ba670 100644
--- a/mrbgems/mruby-cmath/src/cmath.c
+++ b/mrbgems/mruby-cmath/src/cmath.c
@@ -55,11 +55,9 @@ cmath_get_complex(mrb_state *mrb, mrb_value c, mrb_float *r, mrb_float *i)
#ifdef MRB_USE_FLOAT32
typedef _Fcomplex mrb_complex;
#define CX(r,i) _FCbuild(r,i)
-#define CXMUL(x,y) _FCmulcr(x,y)
#else
typedef _Dcomplex mrb_complex;
#define CX(r,i) _Cbuild(r,i)
-#define CXMUL(x,y) _Cmulcr(x,y)
#endif
static mrb_complex
@@ -69,12 +67,28 @@ CXDIVf(mrb_complex x, mrb_float y)
}
static mrb_complex
-CXDIVc(mrb_complex x, mrb_complex y)
+CXDIVc(mrb_complex a, mrb_complex b)
{
- mrb_complex n=CXMUL(x, F(conj)(y));
- mrb_complex d=_CXMUL(y, F(conj)(y));
-
- return CX(creal(n)/creal(d), cimag(n)/cimag(d));
+ mrb_float ratio, den;
+ mrb_float abr, abi, cr, ci;
+
+ if ((abr = creal(b)) < 0)
+ abr = - abr;
+ if ((abi = cimag(b)) < 0)
+ abi = - abi;
+ if (abr <= abi) {
+ ratio = creal(b) / cimag(b) ;
+ den = cimag(a) * (1 + ratio*ratio);
+ cr = (creal(a)*ratio + cimag(a)) / den;
+ ci = (cimag(a)*ratio - creal(a)) / den;
+ }
+ else {
+ ratio = cimag(b) / creal(b) ;
+ den = creal(a) * (1 + ratio*ratio);
+ cr = (creal(a) + cimag(a)*ratio) / den;
+ ci = (cimag(a) - creal(a)*ratio) / den;
+ }
+ return CX(cr, ci);
}
#else