Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hypot to implement Complex.norm #10786

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
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
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