public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/107538] New: std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS
@ 2022-11-05 21:37 redi at gcc dot gnu.org
  2022-11-05 21:37 ` [Bug libstdc++/107538] " redi at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-05 21:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107538

            Bug ID: 107538
           Summary: std::pow(10, std::complex(NaN, 1)) aborts with
                    -D_GLIBCXX_ASSERTIONS
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

#define _GLIBCXX_ASSERTIONS
#include <complex>
#include <limits>

int main()
{
  double nan = std::numeric_limits<double>::quiet_NaN();
  std::pow(10, std::complex<double>(nan, 1));
}


/usr/include/c++/12/complex:703: std::complex<_Tp> std::polar(const _Tp&, const
_Tp&) [with _Tp = double]: Assertion '__rho >= 0' failed.
Aborted (core dumped)


The standard says that pow(x,y) should be exp(y * log(x)) so by using
std::polar we introduce a precondition that shouldn't be there.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/107538] std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS
  2022-11-05 21:37 [Bug libstdc++/107538] New: std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS redi at gcc dot gnu.org
@ 2022-11-05 21:37 ` redi at gcc dot gnu.org
  2022-11-05 21:45 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-05 21:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107538

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Taken from https://savannah.gnu.org/bugs/?55547#comment0 but I don't think
anybody ever reported it here.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/107538] std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS
  2022-11-05 21:37 [Bug libstdc++/107538] New: std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS redi at gcc dot gnu.org
  2022-11-05 21:37 ` [Bug libstdc++/107538] " redi at gcc dot gnu.org
@ 2022-11-05 21:45 ` redi at gcc dot gnu.org
  2022-11-09  4:38 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-05 21:45 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107538

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gdr at gcc dot gnu.org

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Our use of polar seems to have been introduced specifically to deal with a
different edge case, see r0-50043-g1db0418ae570ef for PR libstdc++/10689.

When https://wg21.link/lwg2459 added the non-negative precondition to polar
that introduced this problem for pow.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/107538] std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS
  2022-11-05 21:37 [Bug libstdc++/107538] New: std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS redi at gcc dot gnu.org
  2022-11-05 21:37 ` [Bug libstdc++/107538] " redi at gcc dot gnu.org
  2022-11-05 21:45 ` redi at gcc dot gnu.org
@ 2022-11-09  4:38 ` redi at gcc dot gnu.org
  2022-11-09 13:29 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-09  4:38 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107538

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Target Milestone|---                         |13.0
   Last reconfirmed|                            |2022-11-09

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/107538] std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS
  2022-11-05 21:37 [Bug libstdc++/107538] New: std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS redi at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2022-11-09  4:38 ` redi at gcc dot gnu.org
@ 2022-11-09 13:29 ` redi at gcc dot gnu.org
  2023-04-26  6:56 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2022-11-09 13:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107538

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #0)
> #define _GLIBCXX_ASSERTIONS
> #include <complex>
> #include <limits>
> 
> int main()
> {
>   double nan = std::numeric_limits<double>::quiet_NaN();
>   std::pow(10, std::complex<double>(nan, 1));

Actually maybe the assertion is OK. This violates [complex.numbers] p3:

"If the result of a function is not mathematically defined or not in the range
of representable values for its type, the behavior is undefined."

10^(nan + 1i) is not mathematically defined, an assertion seems OK.

If we want to prevent the assertion here we could do:

--- a/libstdc++-v3/include/std/complex
+++ b/libstdc++-v3/include/std/complex
@@ -699,12 +699,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
     }

+  template<typename _Tp>
+    inline complex<_Tp>
+    __polar(const _Tp& __rho, const _Tp& __theta)
+    { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
+
   template<typename _Tp>
     inline complex<_Tp>
     polar(const _Tp& __rho, const _Tp& __theta)
     {
+      // _GLIBCXX_RESOLVE_LIB_DEFECTS
+      // 2459. std::polar should require a non-negative rho
       __glibcxx_assert( __rho >= 0 );
-      return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
+      return std::__polar<_Tp>(__rho, __theta);
     }

   template<typename _Tp>
@@ -778,7 +785,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp>
     inline complex<_Tp>
     __complex_exp(const complex<_Tp>& __z)
-    { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
+    { return std::__polar<_Tp>(exp(__z.real()), __z.imag()); }

 #if _GLIBCXX_USE_C99_COMPLEX
   inline __complex__ float
@@ -1038,7 +1045,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
         return pow(__x.real(), __y);

       complex<_Tp> __t = std::log(__x);
-      return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
+      return std::__polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
     }

   template<typename _Tp>
@@ -1075,8 +1082,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     inline complex<_Tp>
     pow(const _Tp& __x, const complex<_Tp>& __y)
     {
-      return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
-                                          __y.imag() * log(__x))
+      return __x > _Tp() ? std::__polar<_Tp>(pow(__x, __y.real()),
+                                            __y.imag() * log(__x))
                         : std::pow(complex<_Tp>(__x), __y);
     }

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/107538] std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS
  2022-11-05 21:37 [Bug libstdc++/107538] New: std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS redi at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2022-11-09 13:29 ` redi at gcc dot gnu.org
@ 2023-04-26  6:56 ` rguenth at gcc dot gnu.org
  2023-07-27  9:24 ` rguenth at gcc dot gnu.org
  2024-02-13  4:11 ` orion at nwra dot com
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-04-26  6:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107538

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|13.0                        |13.2

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 13.1 is being released, retargeting bugs to GCC 13.2.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/107538] std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS
  2022-11-05 21:37 [Bug libstdc++/107538] New: std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS redi at gcc dot gnu.org
                   ` (4 preceding siblings ...)
  2023-04-26  6:56 ` rguenth at gcc dot gnu.org
@ 2023-07-27  9:24 ` rguenth at gcc dot gnu.org
  2024-02-13  4:11 ` orion at nwra dot com
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-27  9:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107538

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|13.2                        |13.3

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 13.2 is being released, retargeting bugs to GCC 13.3.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug libstdc++/107538] std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS
  2022-11-05 21:37 [Bug libstdc++/107538] New: std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS redi at gcc dot gnu.org
                   ` (5 preceding siblings ...)
  2023-07-27  9:24 ` rguenth at gcc dot gnu.org
@ 2024-02-13  4:11 ` orion at nwra dot com
  6 siblings, 0 replies; 8+ messages in thread
From: orion at nwra dot com @ 2024-02-13  4:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107538

--- Comment #6 from Orion Poplawski <orion at nwra dot com> ---
Any progress on determining whether or not this is a valid assertion?

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-02-13  4:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-05 21:37 [Bug libstdc++/107538] New: std::pow(10, std::complex(NaN, 1)) aborts with -D_GLIBCXX_ASSERTIONS redi at gcc dot gnu.org
2022-11-05 21:37 ` [Bug libstdc++/107538] " redi at gcc dot gnu.org
2022-11-05 21:45 ` redi at gcc dot gnu.org
2022-11-09  4:38 ` redi at gcc dot gnu.org
2022-11-09 13:29 ` redi at gcc dot gnu.org
2023-04-26  6:56 ` rguenth at gcc dot gnu.org
2023-07-27  9:24 ` rguenth at gcc dot gnu.org
2024-02-13  4:11 ` orion at nwra dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).