16#ifndef MICRO_TEST_PLUS_MATH_H_
17#define MICRO_TEST_PLUS_MATH_H_
30#pragma GCC diagnostic push
31#pragma GCC diagnostic ignored "-Wconversion"
33#pragma clang diagnostic ignored "-Wc++98-compat"
54 [[nodiscard]]
constexpr auto
57 return t < T{} ? -t : t;
64 [[nodiscard]]
constexpr auto
67 return (rhs < lhs) ? rhs : lhs;
73 template <
class T,
class Exp_T>
74 [[nodiscard]]
constexpr auto
75 pow (
const T base,
const Exp_T exp) -> T
78 return exp ? T (base *
pow (base, exp - Exp_T (1))) : T (1);
85 template <
class T,
char... Cs>
86 [[nodiscard]]
constexpr auto
91 ((Cs ==
'.' or Cs ==
'\'' or (Cs >=
'0' and Cs <=
'9')) and ...));
93 for (
const char c : { Cs... })
99 if (c >=
'0' and c <=
'9')
101 result = result * T (10) + T (c -
'0');
111 template <
class T,
char... Cs>
112 [[nodiscard]]
constexpr auto
115 constexpr const std::array cs{ Cs... };
118 while (cs[i++] !=
'.')
122 for (
auto j = i; j <
sizeof...(Cs); ++j)
124 result +=
pow (T (10),
sizeof...(Cs) - j) * T (cs[j] -
'0');
133 template <
class T,
char... Cs>
134 [[nodiscard]]
constexpr auto
137 constexpr const std::array cs{ Cs... };
140#pragma GCC diagnostic push
141#pragma GCC diagnostic ignored "-Wconversion"
143 while (cs[i++] !=
'.')
145#pragma GCC diagnostic pop
150 return T (
sizeof...(Cs)) - i + T (1);
157 template <
class T,
class Value_T>
158 [[nodiscard]]
constexpr auto
161 constexpr auto precision = Value_T (1e-7);
168#pragma GCC diagnostic push
169#if !defined(__clang__)
170#pragma GCC diagnostic ignored "-Warith-conversion"
172#if defined(__clang__)
173#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
176 tmp = value - T (value);
178#pragma GCC diagnostic pop
182 while (tmp > precision);
193#pragma GCC diagnostic pop
Local mathematical functions.
constexpr auto abs(const T t) -> T
Generic absolute of any value.
constexpr auto den() -> T
Compute the decimals of a number represented as an array of characters.
constexpr auto pow(const T base, const Exp_T exp) -> T
Generic 'power of', to raise base to exponent (base ^ exp).
constexpr auto num() -> T
Compute the integral value of a number represented as an array of characters.
constexpr auto den_size() -> T
Compute the number of decimal places of a number represented as an array of characters.
constexpr auto min_value(const T &lhs, const T &rhs) -> const T &
Generic minimum of two values.