[libcxx-commits] [libcxx] f21c247 - [libc++] Fix numeric of exp(complex) at inf

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Thu Oct 28 13:11:04 PDT 2021


Author: Xiang Gao
Date: 2021-10-28T16:10:55-04:00
New Revision: f21c2473006f1e6f9e3e28ba70f898a0aece8cb4

URL: https://github.com/llvm/llvm-project/commit/f21c2473006f1e6f9e3e28ba70f898a0aece8cb4
DIFF: https://github.com/llvm/llvm-project/commit/f21c2473006f1e6f9e3e28ba70f898a0aece8cb4.diff

LOG: [libc++] Fix numeric of exp(complex) at inf

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

Differential Revision: https://reviews.llvm.org/D112277

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libcxx/include/complex b/libcxx/include/complex
index fc52697f7749..b24a19b08635 100644
--- a/libcxx/include/complex
+++ b/libcxx/include/complex
@@ -1056,6 +1056,9 @@ complex<_Tp>
 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 @@ exp(const complex<_Tp>& __x)
             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));
 }

diff  --git a/libcxx/test/std/numerics/complex.number/cases.h b/libcxx/test/std/numerics/complex.number/cases.h
index 9904983fd67f..ccab643879cd 100644
--- a/libcxx/test/std/numerics/complex.number/cases.h
+++ b/libcxx/test/std/numerics/complex.number/cases.h
@@ -38,6 +38,24 @@ const std::complex<double> testcases[] =
     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),

diff  --git a/libcxx/test/std/numerics/complex.number/complex.transcendentals/exp.pass.cpp b/libcxx/test/std/numerics/complex.number/complex.transcendentals/exp.pass.cpp
index 9abeb319cfc9..5c9574e6399d 100644
--- a/libcxx/test/std/numerics/complex.number/complex.transcendentals/exp.pass.cpp
+++ b/libcxx/test/std/numerics/complex.number/complex.transcendentals/exp.pass.cpp
@@ -102,6 +102,10 @@ void test_edges()
             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 @@ int main(int, char**)
     test<long double>();
     test_edges();
 
-  return 0;
+    return 0;
 }


        


More information about the libcxx-commits mailing list