Browse Source

Add overflow check to tanh(complex) and reduce to finite answer. Fixes http://llvm.org/bugs/show_bug.cgi?id=13874

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@164266 91177308-0d34-0410-b5e6-96231b3b80d8
Howard Hinnant 13 years ago
parent
commit
2d3f4ee99f

+ 5 - 1
include/complex

@@ -1351,7 +1351,11 @@ tanh(const complex<_Tp>& __x)
     _Tp __2r(_Tp(2) * __x.real());
     _Tp __2r(_Tp(2) * __x.real());
     _Tp __2i(_Tp(2) * __x.imag());
     _Tp __2i(_Tp(2) * __x.imag());
     _Tp __d(cosh(__2r) + cos(__2i));
     _Tp __d(cosh(__2r) + cos(__2i));
-    return  complex<_Tp>(sinh(__2r)/__d, sin(__2i)/__d);
+    _Tp __2rsh(sinh(__2r));
+    if (isinf(__2rsh) && isinf(__d))
+        return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
+                            __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
+    return  complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
 }
 }
 
 
 // asin
 // asin

+ 1 - 0
test/numerics/complex.number/complex.transcendentals/tan.pass.cpp

@@ -30,6 +30,7 @@ void
 test()
 test()
 {
 {
     test(std::complex<T>(0, 0), std::complex<T>(0, 0));
     test(std::complex<T>(0, 0), std::complex<T>(0, 0));
+    test(std::complex<T>(10000, -10000), std::complex<T>(0, -1));
 }
 }
 
 
 void test_edges()
 void test_edges()