std::tanh(std::complex)(3)
π std::tanh(std::complex) β NAME π
okay so bestie, welcome to the glow-up era of std::tanh(std::complex) β yes, the name is literally just std::tanh(std::complex), no notes, it's giving "I contain multitudes" but the multitudes are complex hyperbolic tangent. We're in the C++ Standard Library manual, section (3), and honestly? Buckle up, this one's got β¨*math*β¨.
β¨ Synopsis β¨
first things first, this whole vibe lives in the header <complex>:
template< class T > (since C++11)
complex<T> tanh( const complex<T>& z );
and what does it do? it computes the complex hyperbolic tangent of a complex value z. that's it, that's the whole thing, and it ate π
π Parameters π
zβ a complex value. that's the whole parameter list bestie, one (1) value, and it's complex. iconic.
π₯ Return value π₯
if nothing goes wrong, you get the complex hyperbolic tangent of z back. no cap, that's the deal π«Ά
πΈ Error handling and special values πΈ
errors get reported consistent with math_errhandling.
and if the implementation supports IEEE floating-point arithmetic, here's the whole special-values saga, and yes there are eleven of them, we're not skipping a single one:
std::tanh(std::conj(z)) == std::conj(std::tanh(z)).std::tanh(-z) == -std::tanh(z).- if
zis(+0,+0), the result is(+0,+0). - if
zis(x,+β)(for any^[1] finitex), the result is(NaN,NaN)andFE_INVALIDis raised. - if
zis(x,NaN)(for any^[2] finitex), the result is(NaN,NaN)andFE_INVALIDmay be raised. - if
zis(+β,y)(for any finite positivey), the result is(1,+0). - if
zis(+β,+β), the result is(1,Β±0)(the sign of the imaginary part is unspecified). - if
zis(+β,NaN), the result is(1,Β±0)(the sign of the imaginary part is unspecified). - if
zis(NaN,+0), the result is(NaN,+0). - if
zis(NaN,y)(for any non-zeroy), the result is(NaN,NaN)andFE_INVALIDmay be raised. - if
zis(NaN,NaN), the result is(NaN,NaN).
little footnote moment, don't skip these π 1. β per C11 DR471, this only holds for non-zerox. Ifzis(0,β), the result should be(0,NaN). 2. β per C11 DR471, this only holds for non-zerox. Ifzis(0,NaN), the result should be(0,NaN).
π¦ Notes π¦
the mathematical definition of hyperbolic tangent is lowkey gorgeous:
tanh z = ( e^z - e^-z ) / ( e^z + e^-z )
hyperbolic tangent is an analytical function on the complex plane and has no branch cuts. it's periodic with respect to the imaginary component, with period Οi, and it has poles of the first order along the imaginary line, at coordinates (0, Ο(1/2 + n)). however β and this is the plot twist β no common floating-point representation is able to represent Ο/2 exactly, so there is literally no value of the argument for which a pole error occurs. we love a problem that solves itself π
π Example π
// Run this code
#include <cmath>
#include <complex>
#include <iostream>
int main()
{
std::cout << std::fixed;
std::complex<double> z(1.0, 0.0); // behaves like real tanh along the real line
std::cout << "tanh" << z << " = " << std::tanh(z)
<< " (tanh(1) = " << std::tanh(1) << ")\n";
std::complex<double> z2(0.0, 1.0); // behaves like tangent along the imaginary line
std::cout << "tanh" << z2 << " = " << std::tanh(z2)
<< " ( tan(1) = " << std::tan(1) << ")\n";
}
β Output β
tanh(1.000000,0.000000) = (0.761594,0.000000) (tanh(1) = 0.761594)
tanh(0.000000,1.000000) = (0.000000,1.557408) ( tan(1) = 1.557408)
π See also π
sinh(std::complex)β computes hyperbolic sine of a complex number (({\small\sinh{z}})sinh(z)) (function template)cosh(std::complex)β computes hyperbolic cosine of a complex number (({\small\cosh{z}})cosh(z)) (function template)atanh(std::complex)(C++11) β computes area hyperbolic tangent of a complex number (({\small\operatorname{artanh}{z}})artanh(z)) (function template)tanh,tanhf,tanhl(C++11), (C++11) β computes hyperbolic tangent (({\small\tanh{x}})tanh(x)) (function)tanh(std::valarray)β applies the functionstd::tanhto each element of valarray (function template)- C documentation for
ctanhβ go read the C one if you're feeling multilingual π§
okay that's the whole page bestie, you're officially a complex-hyperbolic-tangent expert, no cap πβ¨
source: http://cppreference.com Β· date: 2024.06.10 Β· from the std::tanh(std::complex)(3) page β go forth and tanh responsibly π
π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms