HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
math.hpp
1// Copyright Take Vos 2019-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "utility.hpp"
8#include "cast.hpp"
9#include "type_traits.hpp"
10#include "assert.hpp"
11#include <complex>
12#include <cmath>
13#include <limits>
14#include <span>
15#include <tuple>
16#include <numeric>
17#include <iterator>
18#include <bit>
19#include <concepts>
20#include <algorithm>
21
22#if HI_COMPILER == HI_CC_MSVC
23#include <intrin.h>
24#endif
25#if HI_PROCESSOR == HI_CPU_X64
26#include <immintrin.h>
27#endif
28
29namespace hi::inline v1 {
30
31constexpr long long pow10_table[20]{
32 1LL,
33 10LL,
34 100LL,
35 1'000LL,
36 10'000LL,
37 100'000LL,
38 1'000'000LL,
39 10'000'000LL,
40 100'000'000LL,
41 1'000'000'000LL,
42 10'000'000'000LL,
43 100'000'000'000LL,
44 1'000'000'000'000LL,
45 10'000'000'000'000LL,
46 100'000'000'000'000LL,
47 1'000'000'000'000'000LL,
48 10'000'000'000'000'000LL,
49 100'000'000'000'000'000LL,
50 1'000'000'000'000'000'000LL,
51};
52
53constexpr long long pow10ll(int x) noexcept
54{
55 hi_axiom(x >= 0 && x <= 18);
56 return pow10_table[x];
57}
58
59template<typename Iterator>
60auto mean(Iterator first, Iterator last)
61{
62 hilet init = static_cast<typename std::iterator_traits<Iterator>::value_type>(0);
63
64 hilet sum = std::reduce(first, last, init);
65 hilet count = static_cast<decltype(sum)>(std::distance(first, last));
66
67 return count > 0.0 ? sum / count : sum;
68}
69
70template<typename Iterator, typename T>
71auto stddev(Iterator first, Iterator last, T mean)
72{
73 hilet init = static_cast<typename std::iterator_traits<Iterator>::value_type>(0);
74
75 hilet sum = std::accumulate(first, last, init, [=](hilet& acc, hilet& value) {
76 hilet tmp = value - mean;
77 return acc + tmp * tmp;
78 });
79
80 hilet count = static_cast<decltype(sum)>(std::distance(first, last));
81 return count > 0.0 ? sum / count : sum;
82}
83
84template<typename T>
85constexpr void inplace_max(T& a, T const& b) noexcept
86{
87 a = std::max(a, b);
88}
89
90template<typename T>
91constexpr void inplace_min(T& a, T const& b) noexcept
92{
93 a = std::min(a, b);
94}
95
96template<typename T>
97constexpr void inplace_clamp(T& a, T const& lo, T const& hi) noexcept
98{
99 hi_axiom(lo <= hi);
100 a = std::clamp(a, lo, hi);
101}
102
103template<typename T>
104[[nodiscard]] constexpr T abs(T a) noexcept
105{
106 return a < T{} ? -a : a;
107}
108
109template<std::floating_point T>
110[[nodiscard]] constexpr bool almost_equal(T a, T b) noexcept
111{
112 auto e = (a + b) * std::numeric_limits<T>::epsilon();
113 return std::abs(a - b) <= e;
114}
115
116} // namespace hi::inline v1
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:15
The HikoGUI namespace.
Definition ascii.hpp:19
T accumulate(T... args)
T count(T... args)
T distance(T... args)
T max(T... args)
T min(T... args)