GCC Code Coverage Report


Directory: include/polyfloat/
File: include/polyfloat/module/math/harmmean.hpp
Date: 2026-11-09 15:28:50
Exec Total Coverage
Lines: 7 7 100.0%
Functions: 73 73 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
15 namespace plf
16 {
17
18 template<typename Options>
19 struct harmmean_t : eve::strict_tuple_callable<harmmean_t, Options, raw_option, pedantic_option>
20 {
21 template<typename... Ts> struct result : as_polyfloat_like<Ts...>
22 {
23 };
24
25 template<concepts::polyfloat_like Z1, concepts::polyfloat_like... Zs>
26 76 POLYFLOAT_ABI constexpr as_polyfloat_like_t<Z1, Zs...> operator()(Z1 z1, Zs... zs) const noexcept
27 {
28 76 return POLYFLOAT_CALL(z1, zs...);
29 }
30
31 template<eve::non_empty_product_type Tup>
32 POLYFLOAT_ABI constexpr kumi::apply_traits_t<result, Tup> operator()(Tup tup) const noexcept
33 requires(eve::same_lanes_or_scalar_tuple<Tup> && kumi::size_v<Tup> >= 1)
34 {
35 return POLYFLOAT_CALL(tup);
36 }
37
38 74 POLYFLOAT_CALLABLE_OBJECT(harmmean_t, harmmean_);
39 };
40 //======================================================================================================================
41 //! @addtogroup math
42 //! @{
43 //! @var harmmean
44 //! @brief Callable object computing the harmonic mean of the inputs.\f$ \frac{n}{\sum \frac1{z_i}} \f$.
45 //!
46 //! @groupheader{Header file}
47 //!
48 //! @code
49 //! #include <polyfloat/core.hpp>
50 //! @endcode
51 //!
52 //! @groupheader{Callable Signatures}
53 //!
54 //! @code
55 //! namespace polyfloat
56 //! {
57 //! template<polyfloat::concepts::polyfloat_like ... Zs> constexpr auto harmmean(T1 zs...) noexcept;
58 //! }
59 //! @endcode
60 //!
61 //! **Parameters**
62 //!
63 //! * `zs...`: Values to process.
64 //!
65 //! **Return value**
66 //!
67 //! Returns the harmmean of the parameters.
68 //!
69 //! @groupheader{Example}
70 //!
71 //! @godbolt{doc/math/harmmean.cpp}
72 //======================================================================================================================
73
74 inline constexpr auto harmmean = eve::functor<harmmean_t>;
75 //======================================================================================================================
76 //! @}
77 //======================================================================================================================
78 }
79
80 namespace plf::_
81 {
82 template<typename T1, typename... Ts, eve::callable_options O>
83 74 constexpr auto harmmean_(POLYFLOAT_DELAY(), O const& o, T1 a0, Ts... args) noexcept
84 {
85 if constexpr (sizeof...(Ts) == 0) return a0;
86 else
87 {
88 using r_t = as_polyfloat_like_t<T1, Ts...>;
89 using e_t = eve::element_type_t<r_t>;
90 100 auto cvt = [](auto a) { return plf::convert(a, as<e_t>()); };
91 274 return plf::rec[eve::pedantic](
92 274 plf::average[o](plf::rec[eve::pedantic](cvt(a0)), plf::rec[pedantic](cvt(args))...));
93 }
94 }
95 }
96