Simpler complex division implementation (#20088)

This commit is contained in:
Dan Rose 2022-09-01 10:44:07 -05:00 • committed by GitHub
commit c2cdc752c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -163,18 +163,7 @@ func `/`*[T](x: T; y: Complex[T]): Complex[T] =
func `/`*[T](x, y: Complex[T]): Complex[T] = func `/`*[T](x, y: Complex[T]): Complex[T] =
## Divides two complex numbers. ## Divides two complex numbers.
var r, den: T x * conjugate(y) / abs2(y)
if abs(y.re) < abs(y.im):
r = y.re / y.im
den = y.im + r * y.re
result.re = (x.re * r + x.im) / den
result.im = (x.im * r - x.re) / den
else:
r = y.im / y.re
den = y.re + r * y.im
result.re = (x.re + r * x.im) / den
result.im = (x.im - r * x.re) / den
func `+=`*[T](x: var Complex[T]; y: Complex[T]) = func `+=`*[T](x: var Complex[T]; y: Complex[T]) =
## Adds `y` to `x`. ## Adds `y` to `x`.