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 Nov 21, 2021
1 parent 3fa2cfc commit f35b3a3
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
2 changes: 2 additions & 0 deletions stdlib/.depend
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 f35b3a3

Please sign in to comment.