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 017cd3a
Show file tree
Hide file tree
Showing 3 changed files with 5 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
2 changes: 2 additions & 0 deletions tools/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ lintapidiff.cmx : \
../typing/ident.cmx
make_opcodes.cmo :
make_opcodes.cmx :
myocamlbuild_config.cmo :
myocamlbuild_config.cmx :
objinfo.cmo : \
../bytecomp/symtable.cmi \
../middle_end/symbol.cmi \
Expand Down

0 comments on commit 017cd3a

Please sign in to comment.