This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of C++17 status.

2459. std::polar should require a non-negative rho

Section: 28.4.7 [complex.value.ops] Status: C++17 Submitter: Marshall Clow Opened: 2014-10-22 Last modified: 2017-07-30

Priority: 0

View all other issues in [complex.value.ops].

View all issues with C++17 status.

Discussion:

Different implementations give different answers for the following code:

#include <iostream>
#include <complex>

int main ()
{
  std::cout << std::polar(-1.0, -1.0) << '\n';
  return 0;
}

One implementation prints:

(nan, nan)

Another:

(-0.243068, 0.243068)

Which is correct? Or neither?

In this list, Howard Hinnant wrote:

I've read this over several times. I've consulted C++11, C11, and IEC 10967-3. [snip]

I'm finding:

  1. The magnitude of a complex number == abs(c) == hypot(c.real(), c.imag()) and is always non-negative (by all three of the above standards).

  2. Therefore no complex number exists for which abs(c) < 0.

  3. Therefore when the first argument to std::polar (which is called rho) is negative, no complex number can be formed which meets the post-conidtion that abs(c) == rho.

One could argue that this is already covered in 28.4 [complex.numbers]/3, but I think it's worth making explicit.

[2015-02, Cologne]

Discussion on whether theta should also be constrained.
TK: infinite theta doesn't make sense, whereas infinite rho does (theta is on a compact domain, rho is on a non-compact domain).
AM: We already have a narrow contract, so I don't mind adding further requirements. Any objections to requiring that theta be finite?
Some more discussion, but general consensus. Agreement that if someone finds the restrictions problematic, they should write a proper paper to address how std::polar should behave. For now, we allow infinite rho (but not NaN and not negative), and require finite theta.

No objections to tentatively ready.

Proposed resolution:

This wording is relative to N4296.

  1. Change 28.4.7 [complex.value.ops] around p9 as indicated

    template<class T> complex<T> polar(const T& rho, const T& theta = 0);
    

    -?- Requires: rho shall be non-negative and non-NaN. theta shall be finite.

    -9- Returns: The complex value corresponding to a complex number whose magnitude is rho and whose phase angle is theta.