GCC Code Coverage Report


Directory: include/polyfloat/
File: include/polyfloat/module/math/tanh.hpp
Date: 2026-11-09 15:28:50
Exec Total Coverage
Lines: 12 12 100.0%
Functions: 44 44 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 //======================================================================================================================
2 /*
3 POLYFLOAT - Extended precision floating points
4 Copyright : POLYFLOAT Contributors & Maintainers
5 SPDX-License-Identifier: BSL-1.0
6 */
7 //======================================================================================================================
8 #pragma once
9
10 #include <polyfloat/details/callable.hpp>
11 #include <polyfloat/types/concepts.hpp>
12 #include <polyfloat/types/traits.hpp>
13 #include <type_traits>
14 #include <polyfloat/module/math/cosh.hpp>
15 #include <polyfloat/module/math/sinh.hpp>
16 #include <polyfloat/module/math/log.hpp>
17
18 namespace plf
19 {
20
21 template<typename Options>
22 struct tanh_t : plf::elementwise_callable<tanh_t, eve::tanh_t, Options, raw_option, pedantic_option>
23 {
24 94 template<concepts::polyfloat_like Z> POLYFLOAT_ABI constexpr Z operator()(Z z) const noexcept
25 {
26 94 return POLYFLOAT_CALL(z);
27 }
28
29 84 POLYFLOAT_CALLABLE_OBJECT(tanh_t, tanh_);
30 };
31 //======================================================================================================================
32 //! @addtogroup math
33 //! @{
34 //! @var tanh
35 //! @brief return the hyperbolic tangent value.
36 //!
37 //! @groupheader{Header file}
38 //!
39 //! @code
40 //! #include <polyfloat/core.hpp>
41 //! @endcode
42 //!
43 //! @groupheader{Callable Signatures}
44 //!
45 //! @code
46 //! namespace polyfloat
47 //! {
48 //! template<polyfloat::concepts::polyfloat_like T> constexpr auto tanh(T z) noexcept;
49 //! }
50 //! @endcode
51 //!
52 //! **Parameters**
53 //!
54 //! * `z`: Value to process.
55 //!
56 //! **Return value**
57 //!
58 //! Returns the hyperbolic tangent of z.
59 //!
60 //! @groupheader{Example}
61 //!
62 //! @godbolt{doc/math/tanh.cpp}
63 //======================================================================================================================
64
65 inline constexpr auto tanh = eve::functor<tanh_t>;
66 //======================================================================================================================
67 //! @}
68 //======================================================================================================================
69 }
70
71 namespace plf::_
72 {
73 84 template<typename T, eve::callable_options O> constexpr auto tanh_(POLYFLOAT_DELAY(), O const& o, T a0) noexcept
74 {
75 84 auto x = plf::abs(a0 + a0);
76 84 auto test = x > T(0.5493) * 2;
77 84 auto t = plf::expm1[o](x);
78 84 auto rt2 = plf::rec[eve::pedantic](t + T(2));
79 84 auto z1 = plf::fnma(T(2), rt2, T(1));
80 84 auto z2 = t * rt2;
81 84 auto r = plf::if_else(test, z1, z2);
82 84 return if_else(is_infinite(a0), a0, plf::copysign(r, a0));
83 }
84 }
85