micro-test-plus 4.1.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
math-inlines.h
Go to the documentation of this file.
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.0 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
47
48#ifndef MICRO_TEST_PLUS_MATH_INLINES_H_
49#define MICRO_TEST_PLUS_MATH_INLINES_H_
50
51// ----------------------------------------------------------------------------
52
53#ifdef __cplusplus
54
55// ----------------------------------------------------------------------------
56
57#include <cstdint>
58
59// ----------------------------------------------------------------------------
60
61#if defined(__GNUC__)
62#pragma GCC diagnostic push
63#pragma GCC diagnostic ignored "-Waggregate-return"
64#if defined(__clang__)
65#pragma clang diagnostic ignored "-Wc++98-compat"
66#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
67#pragma clang diagnostic ignored "-Wc++20-compat"
68#endif
69#endif
70
71// ============================================================================
72
74{
75 // --------------------------------------------------------------------------
76
77 namespace math
78 {
79
95 template <class T>
96 constexpr auto
97 abs (const T t) noexcept -> T
98 {
99 return t < T{} ? -t : t;
100 }
101
116 template <class T>
117 constexpr auto
118 min_value (const T& lhs, const T& rhs) noexcept -> const T&
119 {
120 return (rhs < lhs) ? rhs : lhs;
121 }
122
137 template <class T, class Exp_T>
138 constexpr auto
139 pow (const T base, const Exp_T exp) noexcept -> T
140 {
141 T result{ 1 };
142 for (Exp_T i{}; i < exp; ++i)
143 result *= base;
144 return result;
145 }
146
161 template <class T, char... Cs>
162 consteval auto
163 num (void) -> T
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 }
183
201 template <class T, char... Cs>
202 consteval auto
203 den (void) -> T
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 }
220
236 template <class T, char... Cs>
237 consteval auto
238 den_size (void) -> T
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 }
257
274 template <class T, class Value_T>
275 constexpr auto
276 den_size (Value_T value) -> T
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 }
302
303 // ------------------------------------------------------------------------
304 } // namespace math
305
306 // --------------------------------------------------------------------------
307} // namespace micro_os_plus::micro_test_plus
308
309#if defined(__GNUC__)
310#pragma GCC diagnostic pop
311#endif
312
313// ----------------------------------------------------------------------------
314
315#endif // __cplusplus
316
317// ----------------------------------------------------------------------------
318
319#endif // MICRO_TEST_PLUS_MATH_INLINES_H_
320
321// ----------------------------------------------------------------------------
Mathematical utilities for the µTest++ testing framework.
constexpr auto min_value(const T &lhs, const T &rhs) noexcept -> const T &
Computes the minimum of two comparable values.
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.
consteval auto den(void) -> T
Computes the decimal part of a number represented as an array of characters.
constexpr auto abs(const T t) noexcept -> T
Computes the absolute value of a given comparable value.
consteval auto den_size(void) -> T
Computes the number of decimal places in a number represented as an array of characters.
consteval auto num(void) -> T
Computes the integral value of a number represented as an array of characters.
Primary namespace for the µTest++ testing framework.