[libcxx-commits] [PATCH] D112277: Fix numeric of exp(complex) at inf

Gao, Xiang via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 21 15:51:56 PDT 2021


zasdfgbnm created this revision.
zasdfgbnm added a reviewer: ldionne.
zasdfgbnm added a project: libc++.
zasdfgbnm requested review of this revision.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

This fixes the bug that `exp({501, 0})` returns `{inf, nan}` instead of `{inf, 0}`


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112277

Files:
  libcxx/include/complex
  libcxx/test/std/numerics/complex.number/cases.h
  libcxx/test/std/numerics/complex.number/complex.transcendentals/exp.pass.cpp


Index: libcxx/test/std/numerics/complex.number/complex.transcendentals/exp.pass.cpp
===================================================================
--- libcxx/test/std/numerics/complex.number/complex.transcendentals/exp.pass.cpp
+++ libcxx/test/std/numerics/complex.number/complex.transcendentals/exp.pass.cpp
@@ -102,6 +102,10 @@
             assert(!std::signbit(r.real()));
             assert(std::signbit(r.imag()) == std::signbit(testcases[i].imag()));
         }
+        else if (std::isinf(r.real()) && testcases[i].imag() == 0) {
+            assert(r.imag() == 0);
+            assert(std::signbit(testcases[i].imag()) == std::signbit(r.imag()));
+        }
     }
 }
 
@@ -112,5 +116,5 @@
     test<long double>();
     test_edges();
 
-  return 0;
+    return 0;
 }
Index: libcxx/test/std/numerics/complex.number/cases.h
===================================================================
--- libcxx/test/std/numerics/complex.number/cases.h
+++ libcxx/test/std/numerics/complex.number/cases.h
@@ -38,6 +38,24 @@
     std::complex<double>(-1.e+6, -1.e+6),
     std::complex<double>( 1.e+6, -1.e+6),
 
+    std::complex<double>(-0, -1.e-6),
+    std::complex<double>(-0,  1.e-6),
+    std::complex<double>(-0,  1.e+6),
+    std::complex<double>(-0, -1.e+6),
+    std::complex<double>( 0, -1.e-6),
+    std::complex<double>( 0,  1.e-6),
+    std::complex<double>( 0,  1.e+6),
+    std::complex<double>( 0, -1.e+6),
+
+    std::complex<double>(-1.e-6, -0),
+    std::complex<double>( 1.e-6, -0),
+    std::complex<double>( 1.e+6, -0),
+    std::complex<double>(-1.e+6, -0),
+    std::complex<double>(-1.e-6,  0),
+    std::complex<double>( 1.e-6,  0),
+    std::complex<double>( 1.e+6,  0),
+    std::complex<double>(-1.e+6,  0),
+
     std::complex<double>(NAN, NAN),
     std::complex<double>(-INFINITY, NAN),
     std::complex<double>(-2, NAN),
Index: libcxx/include/complex
===================================================================
--- libcxx/include/complex
+++ libcxx/include/complex
@@ -1056,6 +1056,9 @@
 exp(const complex<_Tp>& __x)
 {
     _Tp __i = __x.imag();
+    if (__i == 0) {
+        return complex<_Tp>(exp(__x.real()), copysign(_Tp(0), __x.imag()));
+    }
     if (__libcpp_isinf_or_builtin(__x.real()))
     {
         if (__x.real() < _Tp(0))
@@ -1070,8 +1073,6 @@
             return complex<_Tp>(__x.real(), __i);
         }
     }
-    else if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
-        return __x;
     _Tp __e = exp(__x.real());
     return complex<_Tp>(__e * cos(__i), __e * sin(__i));
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112277.381413.patch
Type: text/x-patch
Size: 2566 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211021/17d249b0/attachment-0001.bin>


More information about the libcxx-commits mailing list