[libcxx-commits] [PATCH] D112252: Fix buggy numerics of tanh(complex) at inf #361

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


zasdfgbnm created this revision.
zasdfgbnm added reviewers: howard.hinnant, mclow.lists, dexonsmith.
zasdfgbnm added a project: libc++.
zasdfgbnm requested review of this revision.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

Because:
lim[x->inf, tanh(x+iy)] = 1
lim[x->-inf, tanh(x+iy)] = -1

See also:
https://github.com/NVIDIA/libcudacxx/pull/210


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112252

Files:
  libcxx/include/complex
  libcxx/test/std/numerics/complex.number/complex.transcendentals/tanh.pass.cpp


Index: libcxx/test/std/numerics/complex.number/complex.transcendentals/tanh.pass.cpp
===================================================================
--- libcxx/test/std/numerics/complex.number/complex.transcendentals/tanh.pass.cpp
+++ libcxx/test/std/numerics/complex.number/complex.transcendentals/tanh.pass.cpp
@@ -57,19 +57,19 @@
         }
         else if (std::isinf(testcases[i].real()) && std::isfinite(testcases[i].imag()))
         {
-            assert(r.real() == 1);
-            assert(r.imag() == 0);
-            assert(std::signbit(r.imag()) == std::signbit(sin(2*testcases[i].imag())));
+          assert(r.real() == (testcases[i].real() > 0 ? 1 : -1));
+          assert(r.imag() == 0);
+          assert(std::signbit(r.imag()) == std::signbit(sin(2 * testcases[i].imag())));
         }
         else if (std::isinf(testcases[i].real()) && std::isinf(testcases[i].imag()))
         {
-            assert(r.real() == 1);
-            assert(r.imag() == 0);
+          assert(r.real() == (testcases[i].real() > 0 ? 1 : -1));
+          assert(r.imag() == 0);
         }
         else if (std::isinf(testcases[i].real()) && std::isnan(testcases[i].imag()))
         {
-            assert(r.real() == 1);
-            assert(r.imag() == 0);
+          assert(r.real() == (testcases[i].real() > 0 ? 1 : -1));
+          assert(r.imag() == 0);
         }
         else if (std::isnan(testcases[i].real()) && testcases[i].imag() == 0)
         {
Index: libcxx/include/complex
===================================================================
--- libcxx/include/complex
+++ libcxx/include/complex
@@ -1269,8 +1269,8 @@
     if (__libcpp_isinf_or_builtin(__x.real()))
     {
         if (!__libcpp_isfinite_or_builtin(__x.imag()))
-            return complex<_Tp>(_Tp(1), _Tp(0));
-        return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
+            return complex<_Tp>(copysign(_Tp(1), __x.real()), _Tp(0));
+        return complex<_Tp>(copysign(_Tp(1), __x.real()), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
     }
     if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
         return __x;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112252.381330.patch
Type: text/x-patch
Size: 2150 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20211021/1c66aa01/attachment.bin>


More information about the libcxx-commits mailing list