summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDan Rose <dan@digilabs.io>2022-08-28 21:17:18 -0500
committerGitHub <noreply@github.com>2022-08-28 22:17:18 -0400
commitcc81866da1d045c6702e1c3814fa8ea3e15d53da (patch)
tree661dc61e150028d38fc483c1dbbafead3f42c754
parent04642335c1f7be8f62ae7528070bb07c2a289b02 (diff)
downloadNim-cc81866da1d045c6702e1c3814fa8ea3e15d53da.tar.gz
Implement complex sgn (#20087)
Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
-rw-r--r--lib/pure/complex.nim7
1 files changed, 7 insertions, 0 deletions
diff --git a/lib/pure/complex.nim b/lib/pure/complex.nim
index 5492028a8..2ff52455c 100644
--- a/lib/pure/complex.nim
+++ b/lib/pure/complex.nim
@@ -81,6 +81,13 @@ func abs2*[T](z: Complex[T]): T =
   ## that is the squared distance from (0, 0) to `z`.
   ## This is more efficient than `abs(z) ^ 2`.
   result = z.re * z.re + z.im * z.im
+  
+func sgn*[T](z: Complex[T]): Complex[T] =
+  ## Returns the phase of `z` as a unit complex number,
+  ## or 0 if `z` is 0.
+  let a = abs(z)
+  if a != 0:
+    result = z / a
 
 func conjugate*[T](z: Complex[T]): Complex[T] =
   ## Returns the complex conjugate of `z` (`complex(z.re, -z.im)`).