Skip to main content

math-inlines.h File

C++ header file with inline implementations for the µTest++ mathematical utilities. More...

Included Headers

#include <cstdint>

Namespaces Index

namespacemicro_os_plus

The primary namespace for the µOS++ framework. More...

namespacemicro_test_plus

Primary namespace for the µTest++ testing framework. More...

namespacemath

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

Functions Index

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

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

template <class T, char... Cs>
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>
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) noexcept

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

template <class T, char... Cs>
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) noexcept

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

Description

C++ header file with inline implementations for the µTest++ mathematical utilities.

This header provides the inline implementations for the mathematical utility templates used within the µTest++ framework. It defines constexpr logic for common mathematical operations, including absolute value, minimum value selection, exponentiation, and compile-time parsing of numeric values from character sequences.

These utilities are designed to be lightweight and suitable for embedded environments, supporting both integral and floating-point types, and enabling expressive, type-safe, and efficient compile-time computations. Special attention is given to constexpr compatibility and minimal reliance on the standard library, ensuring portability and performance.

All definitions reside within the micro_os_plus::micro_test_plus::math namespace, ensuring clear separation from user code and minimising the risk of naming conflicts.

The header files are organised within the include/micro-os-plus/micro-test-plus folder to maintain a structured and modular codebase.

This file is intended solely for internal use within the framework and should not be included directly by user code.

Functions

abs()

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

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

98 abs (const T t) noexcept -> T
99 {
100 return t < T{} ? -t : t;
101 }

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>
T micro_os_plus::micro_test_plus::math::den (void)
nodiscard

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

204 den (void) -> T
205 {
206 static_assert ((... || (Cs == '.')),
207 "den(): no decimal point in literal");
208 constexpr const std::array cs{ Cs... };
209 T result{};
210 auto i = 0u;
211 while (cs[i++] != '.')
212 {
213 }
214
215 for (auto j = i; j < sizeof...(Cs); ++j)
216 {
217 result += pow (T (10), sizeof...(Cs) - j) * T (cs[j] - '0');
218 }
219 return result;
220 }

Reference micro_os_plus::micro_test_plus::math::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 281 of file math-inlines.h.

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

den_size()

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

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

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

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 noexcept

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

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

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>
T micro_os_plus::micro_test_plus::math::num (void)
nodiscard

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

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

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 noexcept

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.

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

140 pow (const T base, const Exp_T exp) noexcept -> T
141 {
142 T result{ 1 };
143 for (Exp_T i{}; i < exp; ++i)
144 result *= base;
145 return result;
146 }

Referenced by micro_os_plus::micro_test_plus::math::den.

File Listing

The file content with the documentation metadata removed is:

1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can be
9 * obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
47
48#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_MATH_INLINES_H_
49#define MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_MATH_INLINES_H_
50
51// ----------------------------------------------------------------------------
52
53#if defined(__cplusplus)
54
55// ----------------------------------------------------------------------------
56
57#include <cstdint>
58
59// ----------------------------------------------------------------------------
60
61#if defined(__GNUC__)
62#pragma GCC diagnostic push
63
64#pragma GCC diagnostic ignored "-Waggregate-return"
65#if defined(__clang__)
66#pragma clang diagnostic ignored "-Wc++98-compat"
67#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
68#pragma clang diagnostic ignored "-Wc++20-compat"
69#endif // defined(__clang__)
70#endif // defined(__GNUC__)
71
72// ============================================================================
73
75{
76 // --------------------------------------------------------------------------
77
78 namespace math
79 {
80
96 template <class T>
97 constexpr auto
98 abs (const T t) noexcept -> T
99 {
100 return t < T{} ? -t : t;
101 }
102
117 template <class T>
118 constexpr auto
119 min_value (const T& lhs, const T& rhs) noexcept -> const T&
120 {
121 return (rhs < lhs) ? rhs : lhs;
122 }
123
138 template <class T, class Exp_T>
139 constexpr auto
140 pow (const T base, const Exp_T exp) noexcept -> T
141 {
142 T result{ 1 };
143 for (Exp_T i{}; i < exp; ++i)
144 result *= base;
145 return result;
146 }
147
162 template <class T, char... Cs>
163 consteval auto
164 num (void) -> T
165 {
166 // Assume all are digits or dot or apostrophe.
167 static_assert (
168 ((Cs == '.' or Cs == '\'' or (Cs >= '0' and Cs <= '9')) and ...));
169 T result{};
170 for (const char c : { Cs... })
171 {
172 if (c == '.')
173 {
174 break;
175 }
176 if (c >= '0' and c <= '9')
177 {
178 result = static_cast<T> (result * static_cast<T> (10)
179 + static_cast<T> (c - '0'));
180 }
181 }
182 return result;
183 }
184
202 template <class T, char... Cs>
203 consteval auto
204 den (void) -> T
205 {
206 static_assert ((... || (Cs == '.')),
207 "den(): no decimal point in literal");
208 constexpr const std::array cs{ Cs... };
209 T result{};
210 auto i = 0u;
211 while (cs[i++] != '.')
212 {
213 }
214
215 for (auto j = i; j < sizeof...(Cs); ++j)
216 {
217 result += pow (T (10), sizeof...(Cs) - j) * T (cs[j] - '0');
218 }
219 return result;
220 }
221
237 template <class T, char... Cs>
238 consteval auto
239 den_size (void) -> T
240 {
241 static_assert ((... || (Cs == '.')),
242 "den_size(): no decimal point in literal");
243 constexpr const std::array cs{ Cs... };
244 T i{};
245
246#if defined(__GNUC__)
247#pragma GCC diagnostic push
248
249#pragma GCC diagnostic ignored "-Wconversion"
250#endif // defined(__GNUC__)
251
252 while (cs[i++] != '.')
253 {
254 }
255
256#if defined(__GNUC__)
257#pragma GCC diagnostic pop
258#endif // defined(__GNUC__)
259
260 return T (sizeof...(Cs)) - i + T (1);
261 }
262
279 template <class T, class Value_T>
280 constexpr auto
281 den_size (Value_T value) -> T
282 {
283 constexpr auto precision = Value_T (1e-7);
284 T result{};
285 Value_T tmp{};
286 do
287 {
288 value *= 10;
289
290#if defined(__GNUC__)
291#pragma GCC diagnostic push
292
293#if defined(__clang__)
294#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
295#else // GCC only
296#pragma GCC diagnostic ignored "-Warith-conversion"
297#endif // defined(__clang__)
298#endif // defined(__GNUC__)
299
300 tmp = value - T (value);
301
302#if defined(__GNUC__)
303#pragma GCC diagnostic pop
304#endif // defined(__GNUC__)
305 ++result;
306 }
307 while (tmp > precision);
308
309 return result;
310 }
311
312 // ------------------------------------------------------------------------
313 } // namespace math
314
315 // --------------------------------------------------------------------------
316} // namespace micro_os_plus::micro_test_plus
317
318#if defined(__GNUC__)
319#pragma GCC diagnostic pop
320#endif // defined(__GNUC__)
321
322// ----------------------------------------------------------------------------
323
324#endif // defined(__cplusplus)
325
326// ----------------------------------------------------------------------------
327
328#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_MATH_INLINES_H_
329
330// ----------------------------------------------------------------------------

Generated via doxygen2docusaurus 2.2.2 by Doxygen 1.17.0.