micro-test-plus 5.0.1
µ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_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// ----------------------------------------------------------------------------
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.