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
57[[nodiscard]] constexpr long long pow10ll(int x) noexcept
58{
59 hi_axiom(x >= 0 && x <= 18);
60 return pow10_table[x];
61}
62
63template<typename Iterator>
64[[nodiscard]] auto 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>
75[[nodiscard]] auto 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 bool inplace_max(T& a, T const& b) noexcept
90{
91 using std::max;
92 a = max(a, b);
93 return a == b;
94}
95
96template<typename T>
97constexpr bool inplace_min(T& a, T const& b) noexcept
98{
99 using std::min;
100 a = min(a, b);
101 return a == b;
102}
103
104template<typename T>
105constexpr void inplace_clamp(T& a, T const& lo, T const& hi) noexcept
106{
107 hi_axiom(lo <= hi);
108
109 using std::clamp;
110 a = clamp(a, lo, hi);
111}
112
113template<typename T>
114[[nodiscard]] constexpr T abs(T a) noexcept
115{
116 return a < T{} ? -a : a;
117}
118
119template<std::floating_point T>
120[[nodiscard]] constexpr bool almost_equal(T a, T b) noexcept
121{
122 auto e = (a + b) * std::numeric_limits<T>::epsilon();
123 return std::abs(a - b) <= e;
124}
125
131template<std::floating_point T>
132[[nodiscard]] constexpr T to_radian(T degree) noexcept
133{
134 return degree * (std::numbers::pi_v<T> / T{180.0});
135}
136
142template<std::unsigned_integral T>
143[[nodiscard]] constexpr T floor(T value, T alignment) noexcept
144{
145 return (value / alignment) * alignment;
146}
147
153template<std::unsigned_integral T>
154[[nodiscard]] constexpr T ceil(T value, T alignment) noexcept
155{
156 return floor(value + (alignment - 1), alignment);
157}
158
159template<std::floating_point T>
160[[nodiscard]] constexpr bool isnan(T value) noexcept
161{
162 return not (value == value);
163}
164
165} // 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:132
geometry/margins.hpp
Definition assert.hpp:18
Horizontal/Vertical alignment combination.
Definition alignment.hpp:232
T accumulate(T... args)
T count(T... args)
T distance(T... args)
T max(T... args)
T min(T... args)