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
Enhancement request 13022: add std.complex.abs2
#2297
Conversation
A trivial improvement, but it was missing. |
I would prefer either prefix or postfix |
The use of |
Add `std.complex.abs2`, which returns the squared modulus of a `Complex` or real number. Rationale: * It is common to need the squared modulus of a complex number, and using `abs(z)*abs(z)` would be inefficient due to the computation of the square root. * The name `abs2` has been chosen for consistency with `abs`, and for being mathematically more correct than `norm`, used by C++ STL for the equivalent function. * For genericity, the function `abs2` is also declared for real floating point numbers, in which case it simply returns its square. UPDATE: use `sqAbs` instead of `abs2`.
Rebased and changed the name for |
LGTM. |
I just searched how this function is called in other languages. |
LGTM. |
Auto-merge toggled on |
Enhancement request 13022: add `std.complex.abs2`
Add
std.complex.abs2
, which returns the squared modulus of aComplex
or real number.Rationale:
and using
abs(z)*abs(z)
would be inefficient due to thecomputation of the square root. Moreover,
z*conj(z)
returns acomplex number, usually with nonzero imaginary part due to
rounding, and cannot be used in a generic algorithm that can work
with real numbers as well.
abs2
has been chosen for consistency withabs
, andfor being mathematically more correct than
norm
, used by C++ STLfor the equivalent function.
abs2
is also declared for realfloating point numbers, in which case it simply returns its
square.
UPDATE: use
sqAbs
instead ofabs2
.https://issues.dlang.org/show_bug.cgi?id=13022