Skip to main content

The math.h File Reference

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

Included Headers

#include <array>

Namespaces Index

namespacemicro_os_plus

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

namespacemicro_os_plus::micro_test_plus

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

namespacemicro_os_plus::micro_test_plus::math

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

Functions Index

constexpr Tabs (const T t)

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

constexpr Tden (void)

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

constexpr Tden_size (Value_T value)

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

constexpr Tden_size (void)

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

constexpr const T &min_value (const T &lhs, const T &rhs)

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

constexpr Tnum (void)

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

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

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

This header provides the declarations for the mathematical utility templates used within the µTest++ framework. It defines interfaces for a suite of constexpr mathematical functions, 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 across a wide range of platforms.

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)
nodiscardconstexpr

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

den()

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

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

den_size()

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

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

den_size()

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

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

min_value()

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

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

num()

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

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

pow()

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

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

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 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any 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
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&#95;1&#95;0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
48
49#ifndef MICRO_TEST_PLUS_MATH_H_
50#define MICRO_TEST_PLUS_MATH_H_
51
52// ----------------------------------------------------------------------------
53
54#ifdef __cplusplus
55
56// ----------------------------------------------------------------------------
57
58#include <array>
59
60// ----------------------------------------------------------------------------
61
62#if defined(__GNUC__)
63#pragma GCC diagnostic push
64#pragma GCC diagnostic ignored "-Wconversion"
65#if defined(__clang__)
66#pragma clang diagnostic ignored "-Wc++98-compat"
67#endif
68#endif
69
71{
72 // --------------------------------------------------------------------------
73
93 namespace math
94 {
104 template <class T>
105 [[nodiscard]] constexpr auto
106 abs (const T t) -> T;
107
118 template <class T>
119 [[nodiscard]] constexpr auto
120 min_value (const T& lhs, const T& rhs) -> const T&;
121
135 template <class T, class Exp_T>
136 [[nodiscard]] constexpr auto
137 pow (const T base, const Exp_T exp) -> T;
138
150 template <class T, char... Cs>
151 [[nodiscard]] constexpr auto
152 num (void) -> T;
153
165 template <class T, char... Cs>
166 [[nodiscard]] constexpr auto
167 den (void) -> T;
168
180 template <class T, char... Cs>
181 [[nodiscard]] constexpr auto
182 den_size (void) -> T;
183
195 template <class T, class Value_T>
196 [[nodiscard]] constexpr auto
197 den_size (Value_T value) -> T;
198
199 // ------------------------------------------------------------------------
200 } // namespace math
201
202 // --------------------------------------------------------------------------
203} // namespace micro_os_plus::micro_test_plus
204
205#if defined(__GNUC__)
206#pragma GCC diagnostic pop
207#endif
208
209// ----------------------------------------------------------------------------
210
211#endif // __cplusplus
212
213// ----------------------------------------------------------------------------
214
215#endif // MICRO_TEST_PLUS_MATH_H_
216
217// ----------------------------------------------------------------------------

Generated via docusaurus-plugin-doxygen by Doxygen 1.14.0.