micro-test-plus 4.1.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
micro_os_plus::micro_test_plus::math Namespace Reference

Mathematical utilities for the µTest++ testing framework. More...

Functions

template<class T>
constexpr auto abs (const T t) noexcept -> T
 Computes the absolute value of a given comparable value.
template<class T, char... Cs>
consteval auto den (void) -> T
 Computes the decimal part of a number represented as an array of characters.
template<class T, class Value_T>
constexpr auto den_size (Value_T value) -> T
 Computes the number of decimal places of a value, up to 7 digits.
template<class T, char... Cs>
consteval auto den_size (void) -> T
 Computes the number of decimal places in a number represented as an array of characters.
template<class T>
constexpr auto min_value (const T &lhs, const T &rhs) noexcept -> const T &
 Computes the minimum of two comparable values.
template<class T, char... Cs>
consteval auto num (void) -> T
 Computes the integral value of a number represented as an array of characters.
template<class T, class Exp_T>
constexpr auto pow (const T base, const Exp_T exp) noexcept -> T
 Generic exponentiation function to compute the power of a base raised to an exponent.

Detailed Description

The math namespace offers a suite of constexpr mathematical function templates and utilities for use within the µTest++ framework.

These functions include generic implementations for absolute value, minimum value, exponentiation, and compile-time parsing of numeric values from character arrays. The utilities are designed to be lightweight and suitable for embedded environments, where standard library alternatives may be unavailable, less efficient, or not constexpr.

All definitions within this namespace are intended to facilitate mathematical operations in a type-safe and efficient manner, and are implemented in the include/micro-os-plus folder to maintain a structured and modular codebase.

Function Documentation

◆ abs()

template<class T>
auto micro_os_plus::micro_test_plus::math::abs ( const T t) -> T
nodiscardconstexprnoexcept

This function template provides a generic, constexpr implementation for obtaining the absolute value of any type that supports comparison and unary negation.

The function returns the non-negative value of the input. If the input is less than the default-constructed value of its type (typically zero), the negated value is returned; otherwise, the original value is returned.

This utility is designed to be lightweight and suitable for embedded environments, where standard library alternatives may be unavailable, less efficient, or not constexpr.

Template Parameters
TThe type of the input value. Must support comparison and unary negation.
Parameters
tThe value for which the absolute value is to be computed.
Returns
The absolute value of the input.

Definition at line 97 of file math-inlines.h.

98 {
99 return t < T{} ? -t : t;
100 }

Referenced by micro_os_plus::micro_test_plus::detail::eq_< Lhs_T, Rhs_T >::eq_(), and micro_os_plus::micro_test_plus::detail::ne_< Lhs_T, Rhs_T >::ne_().

◆ den()

template<class T, char... Cs>
auto micro_os_plus::micro_test_plus::math::den ( void ) -> T
nodiscardconsteval

This function template performs compile-time extraction of the decimal (fractional) part from a sequence of characters, typically provided as a template parameter pack.

The function expects the character sequence to represent a numeric value, where all characters are either digits, a dot (.), or an apostrophe ('). Parsing begins after the first dot, accumulating the decimal digits as an integer value, each weighted by its decimal position.

This utility is particularly useful for user-defined literals and other compile-time constant expressions, enabling efficient and type-safe conversion from character sequences to the decimal part of numeric values.

Template Parameters
TThe target integral type for the result.
CsThe character pack representing the numeric value.
Parameters
None.
Returns
The parsed decimal part as an integral value of type T.

Definition at line 203 of file math-inlines.h.

204 {
205 static_assert ((... || (Cs == '.')),
206 "den(): no decimal point in literal");
207 constexpr const std::array cs{ Cs... };
208 T result{};
209 auto i = 0u;
210 while (cs[i++] != '.')
211 {
212 }
213
214 for (auto j = i; j < sizeof...(Cs); ++j)
215 {
216 result += pow (T (10), sizeof...(Cs) - j) * T (cs[j] - '0');
217 }
218 return result;
219 }
constexpr auto pow(const T base, const Exp_T exp) noexcept -> T
Generic exponentiation function to compute the power of a base raised to an exponent.

References pow().

Referenced by micro_os_plus::micro_test_plus::literals::operator""_d(), micro_os_plus::micro_test_plus::literals::operator""_f(), and micro_os_plus::micro_test_plus::literals::operator""_ld().

◆ den_size() [1/2]

template<class T, class Value_T>
auto micro_os_plus::micro_test_plus::math::den_size ( Value_T value) -> T
nodiscardconstexpr

This function template determines, at compile time, the number of decimal (fractional) digits present in a floating-point value, up to a maximum of seven digits of precision.

The function repeatedly multiplies the input value by ten, incrementing a counter until the fractional part is less than a defined precision threshold (1e-7). This approach provides a robust means of estimating decimal precision for values where exact representation is not possible due to floating-point limitations.

This utility is particularly useful for user-defined literals and compile-time constant expressions, enabling efficient and type-safe determination of decimal precision from floating-point values.

Template Parameters
TThe integral type for the result.
Value_TThe type of the input value, typically a floating-point type.
Parameters
valueThe value whose decimal precision is to be determined.
Returns
The number of decimal places, as a value of type T, up to a maximum of seven.

Definition at line 276 of file math-inlines.h.

277 {
278 constexpr auto precision = Value_T (1e-7);
279 T result{};
280 Value_T tmp{};
281 do
282 {
283 value *= 10;
284#if defined(__GNUC__)
285#pragma GCC diagnostic push
286#if defined(__clang__)
287#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
288#else // GCC only
289#pragma GCC diagnostic ignored "-Warith-conversion"
290#endif
291#endif
292 tmp = value - T (value);
293#if defined(__GNUC__)
294#pragma GCC diagnostic pop
295#endif
296 ++result;
297 }
298 while (tmp > precision);
299
300 return result;
301 }

◆ den_size() [2/2]

template<class T, char... Cs>
auto micro_os_plus::micro_test_plus::math::den_size ( void ) -> T
nodiscardconsteval

This function template determines, at compile time, the number of decimal (fractional) digits present in a numeric value represented by a character sequence, typically provided as a template parameter pack.

The function expects the character sequence to represent a numeric value, where all characters are either digits, a dot (.), or an apostrophe ('). It locates the first dot and counts the number of digits that follow, returning the count as the number of decimal places.

This utility is particularly useful for user-defined literals and other compile-time constant expressions, enabling efficient and type-safe determination of decimal precision from character sequences.

Template Parameters
TThe integral type for the result.
CsThe character pack representing the numeric value.
Parameters
None.
Returns
The number of decimal places as a value of type T.

Definition at line 238 of file math-inlines.h.

239 {
240 static_assert ((... || (Cs == '.')),
241 "den_size(): no decimal point in literal");
242 constexpr const std::array cs{ Cs... };
243 T i{};
244#if defined(__GNUC__)
245#pragma GCC diagnostic push
246#pragma GCC diagnostic ignored "-Wconversion"
247#endif
248 while (cs[i++] != '.')
249#if defined(__GNUC__)
250#pragma GCC diagnostic pop
251#endif
252 {
253 }
254
255 return T (sizeof...(Cs)) - i + T (1);
256 }

Referenced by micro_os_plus::micro_test_plus::literals::operator""_d(), micro_os_plus::micro_test_plus::literals::operator""_f(), and micro_os_plus::micro_test_plus::literals::operator""_ld().

◆ min_value()

template<class T>
auto micro_os_plus::micro_test_plus::math::min_value ( const T & lhs,
const T & rhs ) -> const T &
nodiscardconstexprnoexcept

This function template provides a generic, constexpr implementation for determining the minimum of two values of any type that supports comparison.

The function returns a reference to the lesser of the two input values, as determined by the < operator. If the second argument is less than the first, it is returned; otherwise, the first argument is returned.

This utility is designed to be lightweight and suitable for embedded environments, where standard library alternatives may be unavailable, less efficient, or not constexpr.

Template Parameters
TThe type of the input values. Must support comparison via the < operator.
Parameters
lhsThe first value to compare.
rhsThe second value to compare.
Returns
A reference to the minimum of the two input values.

Definition at line 118 of file math-inlines.h.

119 {
120 return (rhs < lhs) ? rhs : lhs;
121 }

Referenced by micro_os_plus::micro_test_plus::detail::eq_< Lhs_T, Rhs_T >::eq_(), and micro_os_plus::micro_test_plus::detail::ne_< Lhs_T, Rhs_T >::ne_().

◆ num()

template<class T, char... Cs>
auto micro_os_plus::micro_test_plus::math::num ( void ) -> T
nodiscardconsteval

This function template performs compile-time parsing of a numeric value from a sequence of characters, typically provided as a template parameter pack.

The function assumes that all characters are either digits, a dot (.), or an apostrophe ('). Parsing stops at the first dot, allowing the function to extract only the integral part of the number.

This utility is particularly useful for user-defined literals and other compile-time constant expressions, enabling efficient and type-safe conversion from character sequences to integral values.

Template Parameters
TThe target integral type for the result.
CsThe character pack representing the numeric value.
Parameters
None.
Returns
The parsed integral value of type T.

Definition at line 163 of file math-inlines.h.

164 {
165 // Assume all are digits or dot or apostrophe.
166 static_assert (
167 ((Cs == '.' or Cs == '\'' or (Cs >= '0' and Cs <= '9')) and ...));
168 T result{};
169 for (const char c : { Cs... })
170 {
171 if (c == '.')
172 {
173 break;
174 }
175 if (c >= '0' and c <= '9')
176 {
177 result = static_cast<T> (result * static_cast<T> (10)
178 + static_cast<T> (c - '0'));
179 }
180 }
181 return result;
182 }

Referenced by micro_os_plus::micro_test_plus::literals::operator""_c(), micro_os_plus::micro_test_plus::literals::operator""_d(), micro_os_plus::micro_test_plus::literals::operator""_f(), micro_os_plus::micro_test_plus::literals::operator""_i(), micro_os_plus::micro_test_plus::literals::operator""_i16(), micro_os_plus::micro_test_plus::literals::operator""_i32(), micro_os_plus::micro_test_plus::literals::operator""_i64(), micro_os_plus::micro_test_plus::literals::operator""_i8(), micro_os_plus::micro_test_plus::literals::operator""_l(), micro_os_plus::micro_test_plus::literals::operator""_ld(), micro_os_plus::micro_test_plus::literals::operator""_ll(), micro_os_plus::micro_test_plus::literals::operator""_s(), micro_os_plus::micro_test_plus::literals::operator""_sc(), micro_os_plus::micro_test_plus::literals::operator""_u(), micro_os_plus::micro_test_plus::literals::operator""_u16(), micro_os_plus::micro_test_plus::literals::operator""_u32(), micro_os_plus::micro_test_plus::literals::operator""_u64(), micro_os_plus::micro_test_plus::literals::operator""_u8(), micro_os_plus::micro_test_plus::literals::operator""_uc(), micro_os_plus::micro_test_plus::literals::operator""_ul(), micro_os_plus::micro_test_plus::literals::operator""_ull(), and micro_os_plus::micro_test_plus::literals::operator""_us().

◆ pow()

template<class T, class Exp_T>
auto micro_os_plus::micro_test_plus::math::pow ( const T base,
const Exp_T exp ) -> T
nodiscardconstexprnoexcept

This function template provides a constexpr implementation for raising a base value to a given exponent, supporting any types that allow multiplication.

The function iteratively multiplies the base by itself exponent times. If the exponent is zero, the function returns one (the multiplicative identity for the type).

This utility is designed to be lightweight and suitable for embedded environments, where standard library alternatives may be unavailable, less efficient, or not constexpr.

Template Parameters
TThe type of the base value. Must support multiplication and construction from an integer.
Exp_TThe type of the exponent. Must support subtraction and comparison to zero.
Parameters
baseThe base value to be raised to the power of exp.
expThe exponent value.
Returns
The result of raising base to the power of exp.

Definition at line 139 of file math-inlines.h.

140 {
141 T result{ 1 };
142 for (Exp_T i{}; i < exp; ++i)
143 result *= base;
144 return result;
145 }

Referenced by den().