Title
std::polar should require a non-negative rho
Status
c++17
Section
[complex.value.ops]
Submitter
Marshall Clow

Created on 2014-10-22.00:00:00 last changed 89 months ago

Messages

Date: 2015-03-22.19:05:09

Proposed resolution:

This wording is relative to N4296.

  1. Change [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.

Date: 2015-03-22.19:05:09

[ 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.

Date: 2014-10-22.00:00:00

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 [complex.numbers]/3, but I think it's worth making explicit.

History
Date User Action Args
2017-07-30 20:15:43adminsetstatus: wp -> c++17
2015-05-22 18:31:21adminsetstatus: ready -> wp
2015-03-22 19:05:09adminsetmessages: + msg7246
2015-03-22 19:05:09adminsetstatus: new -> ready
2014-11-25 18:50:34adminsetmessages: + msg7210
2014-10-22 00:00:00admincreate