Skip to content

Commit

Permalink
Use hypot to implement Complex.norm
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris00 committed Dec 1, 2021
1 parent 3fa2cfc commit b218ea5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -153,6 +153,9 @@ OCaml 4.14.0
OCaml.
(Damien Doligez, review by Stephen Dolan)

- #10786: The implementation of Complex.norm now uses Float.hypot.
(Christophe Troestler, review by David Allsopp and Xavier Leroy)

### Other libraries:

- #10192: Add support for Unix domain sockets on Windows and use them
Expand Down
2 changes: 2 additions & 0 deletions stdlib/.depend
Expand Up @@ -189,8 +189,10 @@ stdlib__Char.cmx : char.ml \
stdlib__Char.cmi
stdlib__Char.cmi : char.mli
stdlib__Complex.cmo : complex.ml \
stdlib__Float.cmi \
stdlib__Complex.cmi
stdlib__Complex.cmx : complex.ml \
stdlib__Float.cmx \
stdlib__Complex.cmi
stdlib__Complex.cmi : complex.mli
stdlib__Digest.cmo : digest.ml \
Expand Down
10 changes: 1 addition & 9 deletions stdlib/complex.ml
Expand Up @@ -48,15 +48,7 @@ let inv x = div one x

let norm2 x = x.re *. x.re +. x.im *. x.im

let norm x =
(* Watch out for overflow in computing re^2 + im^2 *)
let r = abs_float x.re and i = abs_float x.im in
if r = 0.0 then i
else if i = 0.0 then r
else if r >= i then
let q = i /. r in r *. sqrt(1.0 +. q *. q)
else
let q = r /. i in i *. sqrt(1.0 +. q *. q)
let norm x = Float.hypot x.re x.im

let arg x = atan2 x.im x.re

Expand Down

0 comments on commit b218ea5

Please sign in to comment.