Skip to main content

The math Namespace Reference

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

Definition

namespace micro_os_plus::micro_test_plus::math { ... }

Functions Index

template <class T>
constexpr Tabs (const T t)

Computes the absolute value of a given comparable value. More...

template <class T, char... Cs>
constexpr Tden (void)

Computes the decimal part of a number represented as an array of characters. More...

template <class T, class Value_T>
constexpr Tden_size (Value_T value)

Computes the number of decimal places of a value, up to 7 digits. More...

template <class T, char... Cs>
constexpr Tden_size (void)

Computes the number of decimal places in a number represented as an array of characters. More...

template <class T>
constexpr const T &min_value (const T &lhs, const T &rhs)

Computes the minimum of two comparable values. More...

template <class T, char... Cs>
constexpr Tnum (void)

Computes the integral value of a number represented as an array of characters. More...

template <class T, class Exp_T>
constexpr Tpow (const T base, const Exp_T exp)

Generic exponentiation function to compute the power of a base raised to an exponent. More...

Description

Mathematical utilities for the µTest++ testing framework.

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.

Functions

abs()

template <class T>
T micro_os_plus::micro_test_plus::math::abs (const T t)
nodiscard constexpr

Computes the absolute value of a given comparable value.

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
T

The type of the input value. Must support comparison and unary negation.

Parameters
t

The value for which the absolute value is to be computed.

Returns

The absolute value of the input.

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

96 abs (const T t) -> T
97 {
98 return t < T{} ? -t : t;
99 }

den()

template <class T, char... Cs>
T micro_os_plus::micro_test_plus::math::den (void)
nodiscard constexpr

Computes the decimal part of a number represented as an array of characters.

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
T

The target integral type for the result.

Cs

The character pack representing the numeric value.

Parameters

None.

Returns

The parsed decimal part as an integral value of type T.

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

200 den (void) -> T
201 {
202 constexpr const std::array cs{ Cs... };
203 T result{};
204 auto i = 0u;
205 while (cs[i++] != '.')
206 {
207 }
208
209 for (auto j = i; j < sizeof...(Cs); ++j)
210 {
211 result += pow (T (10), sizeof...(Cs) - j) * T (cs[j] - '0');
212 }
213 return result;
214 }

Reference 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()

template <class T, class Value_T>
T micro_os_plus::micro_test_plus::math::den_size (Value_T value)
nodiscard constexpr

Computes the number of decimal places of a value, up to 7 digits.

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
T

The integral type for the result.

Value_T

The type of the input value, typically a floating-point type.

Parameters
value

The 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 269 of file math-inlines.h.

269 den_size (Value_T value) -> T
270 {
271 constexpr auto precision = Value_T (1e-7);
272 T result{};
273 Value_T tmp{};
274 do
275 {
276 value *= 10;
277#if defined(__GNUC__)
278#pragma GCC diagnostic push
279#if !defined(__clang__) // GCC only
280#pragma GCC diagnostic ignored "-Warith-conversion"
281#endif
282#if defined(__clang__)
283#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
284#endif
285#endif
286 tmp = value - T (value);
287#if defined(__GNUC__)
288#pragma GCC diagnostic pop
289#endif
290 ++result;
291 }
292 while (tmp > precision);
293
294 return result;
295 }

den_size()

template <class T, char... Cs>
T micro_os_plus::micro_test_plus::math::den_size (void)
nodiscard constexpr

Computes the number of decimal places in a number represented as an array of characters.

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
T

The integral type for the result.

Cs

The character pack representing the numeric value.

Parameters

None.

Returns

The number of decimal places as a value of type T.

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

233 den_size (void) -> T
234 {
235 constexpr const std::array cs{ Cs... };
236 T i{};
237#if defined(__GNUC__)
238#pragma GCC diagnostic push
239#pragma GCC diagnostic ignored "-Wconversion"
240#endif
241 while (cs[i++] != '.')
242#if defined(__GNUC__)
243#pragma GCC diagnostic pop
244#endif
245 {
246 }
247
248 return T (sizeof...(Cs)) - i + T (1);
249 }

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>
const T & micro_os_plus::micro_test_plus::math::min_value (const T & lhs, const T & rhs)
nodiscard constexpr

Computes the minimum of two comparable values.

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
T

The type of the input values. Must support comparison via the < operator.

Parameters
lhs

The first value to compare.

rhs

The second value to compare.

Returns

A reference to the minimum of the two input values.

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

117 min_value (const T& lhs, const T& rhs) -> const T&
118 {
119 return (rhs < lhs) ? rhs : lhs;
120 }

num()

template <class T, char... Cs>
T micro_os_plus::micro_test_plus::math::num (void)
nodiscard constexpr

Computes the integral value of a number represented as an array of characters.

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
T

The target integral type for the result.

Cs

The character pack representing the numeric value.

Parameters

None.

Returns

The parsed integral value of type T.

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

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

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>
T micro_os_plus::micro_test_plus::math::pow (const T base, const Exp_T exp)
nodiscard constexpr

Generic exponentiation function to compute the power of a base raised to an exponent.

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

The function recursively 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
T

The type of the base value. Must support multiplication and construction from an integer.

Exp_T

The type of the exponent. Must support subtraction and comparison to zero.

Parameters
base

The base value to be raised to the power of exp.

exp

The exponent value.

Returns

The result of raising base to the power of exp.

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

138 pow (const T base, const Exp_T exp) -> T
139 {
140 // If the exponent is 0, return 1, otherwise recurse.
141 return exp ? T (base * pow (base, exp - Exp_T (1))) : T (1);
142 }

Reference pow.

Referenced by den and pow.


The documentation for this namespace was generated from the following file:


Generated via doxygen2docusaurus by Doxygen 1.14.0.