HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
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
8#pragma once
9
10#include "utility.hpp"
11#include "cast.hpp"
12#include "type_traits.hpp"
13#include "assert.hpp"
14#include <complex>
15#include <cmath>
16#include <limits>
17#include <span>
18#include <tuple>
19#include <numeric>
20#include <iterator>
21#include <bit>
22#include <concepts>
23#include <algorithm>
24#include <numbers>
25
26#if HI_COMPILER == HI_CC_MSVC
27#include <intrin.h>
28#endif
29#if HI_PROCESSOR == HI_CPU_X64
30#include <immintrin.h>
31#endif
32
33namespace hi::inline v1 {
34
35constexpr long long pow10_table[20]{
36 1LL,
37 10LL,
38 100LL,
39 1'000LL,
40 10'000LL,
41 100'000LL,
42 1'000'000LL,
43 10'000'000LL,
44 100'000'000LL,
45 1'000'000'000LL,
46 10'000'000'000LL,
47 100'000'000'000LL,
48 1'000'000'000'000LL,
49 10'000'000'000'000LL,
50 100'000'000'000'000LL,
51 1'000'000'000'000'000LL,
52 10'000'000'000'000'000LL,
53 100'000'000'000'000'000LL,
54 1'000'000'000'000'000'000LL,
55};
56
57constexpr long long pow10ll(int x) noexcept
58{
59 hi_axiom(x >= 0 && x <= 18);
60 return pow10_table[x];
61}
62
63template<typename Iterator>
64auto mean(Iterator first, Iterator last)
65{
66 hilet init = static_cast<typename std::iterator_traits<Iterator>::value_type>(0);
67
68 hilet sum = std::reduce(first, last, init);
69 hilet count = static_cast<decltype(sum)>(std::distance(first, last));
70
71 return count > 0.0 ? sum / count : sum;
72}
73
74template<typename Iterator, typename T>
75auto stddev(Iterator first, Iterator last, T mean)
76{
77 hilet init = static_cast<typename std::iterator_traits<Iterator>::value_type>(0);
78
79 hilet sum = std::accumulate(first, last, init, [=](hilet& acc, hilet& value) {
80 hilet tmp = value - mean;
81 return acc + tmp * tmp;
82 });
83
84 hilet count = static_cast<decltype(sum)>(std::distance(first, last));
85 return count > 0.0 ? sum / count : sum;
86}
87
88template<typename T>
89constexpr void inplace_max(T& a, T const& b) noexcept
90{
91 a = std::max(a, b);
92}
93
94template<typename T>
95constexpr void inplace_min(T& a, T const& b) noexcept
96{
97 a = std::min(a, b);
98}
99
100template<typename T>
101constexpr void inplace_clamp(T& a, T const& lo, T const& hi) noexcept
102{
103 hi_axiom(lo <= hi);
104 a = std::clamp(a, lo, hi);
105}
106
107template<typename T>
108[[nodiscard]] constexpr T abs(T a) noexcept
109{
110 return a < T{} ? -a : a;
111}
112
113template<std::floating_point T>
114[[nodiscard]] constexpr bool almost_equal(T a, T b) noexcept
115{
116 auto e = (a + b) * std::numeric_limits<T>::epsilon();
117 return std::abs(a - b) <= e;
118}
119
125template<std::floating_point T>
126[[nodiscard]] constexpr T to_radian(T degree) noexcept
127{
128 return degree * (std::numbers::pi_v<T> / T{180.0});
129}
130
136template<std::unsigned_integral T>
137constexpr T floor(T value, T alignment) noexcept
138{
139 return (value / alignment) * alignment;
140}
141
147template<std::unsigned_integral T>
148constexpr T ceil(T value, T alignment) noexcept
149{
150 return floor(value + (alignment - 1), alignment);
151}
152
153} // namespace hi::inline v1
Utilities to assert and bound check.
#define hi_axiom(expression)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
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
constexpr T to_radian(T degree) noexcept
Convert degree to radian.
Definition math.hpp:126
The HikoGUI namespace.
Definition ascii.hpp:19
Definition alignment.hpp:75
T accumulate(T... args)
T count(T... args)
T distance(T... args)
T max(T... args)
T min(T... args)