11#include "../macros.hpp"
15#include "exception.hpp"
28#if HI_COMPILER == HI_CC_MSVC
31#if HI_PROCESSOR == HI_CPU_X86_64
35hi_export_module(hikogui.utility.math);
37hi_export
namespace hi::inline
v1 {
39template<
typename Iterator>
40[[nodiscard]]
auto mean(Iterator first, Iterator last)
44 auto const sum = std::reduce(first, last, init);
50template<
typename Iterator,
typename T>
51[[nodiscard]]
auto stddev(Iterator first, Iterator last, T mean)
55 auto const sum =
std::accumulate(first, last, init, [=](
auto const& acc,
auto const& value) {
56 auto const tmp = value - mean;
57 return acc + tmp * tmp;
65constexpr bool inplace_max(T& a, T
const& b)
noexcept
73constexpr bool inplace_min(T& a, T
const& b)
noexcept
81constexpr void inplace_clamp(T& a, T
const& lo, T
const&
hi)
noexcept
90[[nodiscard]]
constexpr T abs(T a)
noexcept
92 return a < T{} ? -a : a;
95template<std::
floating_po
int T>
96[[nodiscard]]
constexpr bool almost_equal(T a, T b)
noexcept
99 return std::abs(a - b) <= e;
107template<std::
floating_po
int T>
110 return degree * (std::numbers::pi_v<T> / T{180.0});
118template<std::
unsigned_
integral T>
119[[nodiscard]]
constexpr T floor(T value, T alignment)
noexcept
121 return (value / alignment) * alignment;
129template<std::
unsigned_
integral T>
130[[nodiscard]]
constexpr T ceil(T value, T alignment)
noexcept
132 return floor(value + (alignment - 1), alignment);
135template<std::
floating_po
int T>
136[[nodiscard]]
constexpr bool isnan(T value)
noexcept
138 return not (value == value);
141constexpr unsigned long long pow10_table[20] = {
154 1'000'000'000'000ULL,
155 10'000'000'000'000ULL,
156 100'000'000'000'000ULL,
157 1'000'000'000'000'000ULL,
158 10'000'000'000'000'000ULL,
159 100'000'000'000'000'000ULL,
160 1'000'000'000'000'000'000ULL,
163[[nodiscard]]
constexpr uint64_t pow10(
unsigned int x)
noexcept
165 hi_axiom_bounds(x, 20);
166 return pow10_table[x];
169template<std::
unsigned_
integral T>
170[[nodiscard]]
constexpr unsigned int decimal_width(T x)
173 constexpr uint8_t guess_table[] = {
174 0, 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4,
175 4, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9,
176 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 14,
177 14, 14, 15, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 18, 18
181 auto const num_bits = std::bit_width(x);
182 hi_axiom_bounds(num_bits, guess_table);
183 auto const guess = guess_table[num_bits];
184 return guess + wide_cast<unsigned int>(x >= pow10(guess));
187template<std::
signed_
integral T>
188[[nodiscard]]
constexpr unsigned int decimal_width(T x)
190 return decimal_width(
narrow_cast<std::make_unsigned_t<T>>(abs(x)));
Utilities to assert and bound check.
Functions for casting values between types savely.
Utilities for throwing exceptions and terminating the application.
The HikoGUI namespace.
Definition array_generic.hpp:20
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:378
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
constexpr T to_radian(T degree) noexcept
Convert degree to radian.
Definition math.hpp:108