|
micro-test-plus 6.0.0
µTest++ Testing Framework
|
Internal mathematical utilities for the µTest++ testing framework. More...
Classes | |
| struct | integer_literal |
| The result of parsing the characters of an integer literal. 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, class Value_T> | |
| constexpr auto | count_decimals (Value_T value) -> T |
| Computes the number of decimal places of a value, with a precision of about seven digits. | |
| template<bool Fraction, char... Cs> | |
| consteval auto | count_literal_digits (void) -> std::size_t |
| Counts the decimal digits of a floating-point literal, before or after the decimal point. | |
| template<class Value_T> | |
| constexpr auto | decimal_epsilon (Value_T value) -> Value_T |
| Computes the default comparison tolerance of a floating-point value, one unit of its last decimal. | |
| 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, char... Cs> | |
| consteval auto | den_size (void) -> T |
| Counts the decimal places of a number represented as an array of characters. | |
| template<class T, char... Cs> | |
| consteval auto | integer_magnitude (void) -> unsigned long long |
Computes the value of an integer literal of type T, before any negation. | |
| 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 part of a decimal floating-point number represented as an array of characters. | |
| template<char... Cs> | |
| consteval auto | parse_integer_literal (void) -> integer_literal |
| Parses the characters of an integer literal. | |
| 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. | |
The detail::math namespace offers a suite of constexpr mathematical function templates and utilities, used by the literals and by the floating-point comparisons of 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.
The names of these functions suggest general mathematical functions, but their scope is limited to the needs of the framework (for example, pow() accepts only integral exponents); like everything in detail, they are not part of the public API and may be modified without prior notice.
|
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.
| T | The type of the input value. Must support comparison and unary negation. |
| t | The value for which the absolute value is to be computed. |
Definition at line 88 of file math-inlines.h.
References abs().
Referenced by abs(), count_decimals(), micro_os_plus::micro_test_plus::detail::differ_by_at_most(), and micro_os_plus::micro_test_plus::detail::within_tolerance().
|
nodiscardconstexpr |
| T | The integral type for the result. |
| Value_T | The type of the input value, a floating-point type. |
| value | The value whose decimal precision is to be determined; it may be negative, infinite, or a NaN. |
This function template determines the number of decimal (fractional) digits present in a floating-point value, with a precision of about seven digits. Being constexpr, it can also be evaluated at compile time.
The function repeatedly multiplies the absolute value by ten, incrementing a counter until the distance to the nearest integer is at most a defined precision threshold (1e-7). Using the distance to the nearest integer, not the fractional part, prevents the rounding noise of a value just below an integer (e.g. 2674.9999999999995) from being taken as more decimals.
The nearest integer is never zero: while the scaled value is below one, its distance to one is used, so that the leading zeros of a small value are counted as decimals, as in its notation (12 for 1e-12), instead of the count stopping as soon as the scaled value is below the threshold. A zero value has no significant digit, and the result is zero.
The loop also stops when the value reaches the magnitude from which the type can represent only integers (2^23 for float, 2^52 for double), limited to 2^63 so that the conversion to unsigned long long is always defined. This bounds the number of steps for large values, infinities, and NaNs, for which the result is one; for small values, the number of steps is bounded by the decimal exponent of the smallest value of the type.
Definition at line 258 of file deprecated-inlines.h.
References abs(), count_decimals(), and min_value().
Referenced by count_decimals(), and decimal_epsilon().
|
nodiscardconsteval |
The digit separators (') are not counted.
| Fraction | true to count the digits after the decimal point, false to count those before it. |
| Cs | The characters of the literal. |
Walks the characters once, counting the decimal digits before the first decimal point (Fraction false) or after it (Fraction true); the digit separators are skipped.
Definition at line 101 of file deprecated-inlines.h.
References count_literal_digits().
Referenced by count_literal_digits(), den(), den_size(), and num().
|
nodiscardconstexpr |
| Value_T | The type of the value, a floating-point type. |
| value | The value whose tolerance is to be determined; it may be negative, infinite, or a NaN. |
The number of decimal places is computed by count_decimals(), and the tolerance is one divided by ten raised to that number, as for the floating-point literals, which use the decimals written.
A zero value has no decimals, and gets a zero tolerance, so that it is compared exactly; a tolerance of one unit of an arbitrary decimal would make the zero equal to unrelated small values. A zero tolerance is also returned when ten raised to the number of decimals would exceed the range of Value_T (values below about 1e-308 for double), instead of computing an infinite power.
Definition at line 326 of file deprecated-inlines.h.
References count_decimals(), decimal_epsilon(), and pow().
Referenced by decimal_epsilon().
|
nodiscardconsteval |
| T | The target integral type for the result. |
| Cs | The character pack representing the numeric value; it must contain a decimal point. |
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 and skipping the digit separators (e.g. 1.25 yields 25), so that den() / 10^den_size() gives the fraction. If the result could exceed T, the compilation fails with an explicit message.
Definition at line 177 of file deprecated-inlines.h.
References count_literal_digits(), and den().
Referenced by den(), 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().
|
nodiscardconsteval |
| T | The integral type for the result. |
| Cs | The character pack representing the numeric value; it must contain a decimal point. |
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 returns the number of digits that follow the first dot (the digit separators are not counted; e.g. 2 for 1.25), which is the scale of the value returned by den().
Definition at line 220 of file deprecated-inlines.h.
References count_literal_digits(), and den_size().
Referenced by den_size(), 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().
|
nodiscardconsteval |
| T | The integral type of the literal. |
| Cs | The characters of the literal, as received by a literal operator template. |
The literal must be an integer literal, in any of the C++ forms (decimal, hexadecimal, binary, or octal, with digit separators), and its value must fit in T; otherwise the compilation fails with an explicit message.
Negative values are obtained with the unary minus, so the literal itself is never negative. For the signed types, the limit is the absolute value of the minimum, max () + 1, so that the minimum can be written (e.g. -128_i8); the caller allows this magnitude only when it is negated.
When a check fails, zero is returned, so that the callers report no further errors.
Definition at line 448 of file deprecated-inlines.h.
References integer_magnitude(), micro_os_plus::micro_test_plus::detail::math::integer_literal::is_integer, micro_os_plus::micro_test_plus::detail::math::integer_literal::is_too_large, parse_integer_literal(), and micro_os_plus::micro_test_plus::detail::math::integer_literal::value.
Referenced by integer_magnitude(), and micro_os_plus::micro_test_plus::detail::integral_literal().
|
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.
| T | The type of the input values. Must support comparison via the < operator. |
| lhs | The first value to compare. |
| rhs | The second value to compare. |
Definition at line 106 of file math-inlines.h.
References min_value().
Referenced by count_decimals(), min_value(), and micro_os_plus::micro_test_plus::detail::within_tolerance().
|
nodiscardconsteval |
Used by the floating-point literal operators; the integer literals use integer_magnitude(), which also accepts the C++ prefixes and checks the range.
| T | The target integral type for the result. |
| Cs | The character pack representing the numeric value. |
This function template performs compile-time parsing of a numeric value from a sequence of characters, typically provided as a template parameter pack.
The characters must be decimal digits, a dot (.), or digit separators ('), which are skipped; otherwise (e.g. an exponent) the compilation fails with an explicit message, as it does if the integral part has more digits than T can always hold. Parsing stops at the first dot, allowing the function to extract only the integral part of the number.
Definition at line 134 of file deprecated-inlines.h.
References count_literal_digits(), and num().
Referenced by num(), 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().
|
nodiscardconsteval |
| Cs | The characters of the literal, as received by a literal operator template. |
The characters are those of a valid C++ literal, already checked by the compiler (digits valid for the base, digit separators in valid positions). The base is given by the prefix: 0x or 0X for hexadecimal, 0b or 0B for binary, a leading 0 for octal, and none for decimal. The digit separators (') are skipped.
Any other character (a decimal point, an exponent, or a digit not valid for the base, as in the floating-point literal 09.5) marks the literal as not an integer. The value is accumulated in unsigned long long, and the accumulation stops if it would overflow.
Definition at line 354 of file deprecated-inlines.h.
References parse_integer_literal().
Referenced by integer_magnitude(), and parse_integer_literal().
|
nodiscardconstexprnoexcept |
| T | The type of the base value. Must support multiplication assignment and construction from an integer. |
| Exp_T | The type of the exponent. Must be default-constructible (to zero), and support the < and prefix ++ operators. |
| base | The base value to be raised to the power of exp. |
| exp | The exponent value. |
base to the power of exp.This function template provides a constexpr implementation for raising a base value to a given exponent, supporting any types that allow multiplication.
The function starts from one and multiplies the result by the base exp times. If the exponent is zero (or negative), the function returns one (the multiplicative identity for the type).
Definition at line 85 of file deprecated-inlines.h.
References pow().
Referenced by decimal_epsilon(), and pow().