micro-test-plus 3.2.2
µ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 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
9 * be 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// #include "type-traits.h"
60
61// ----------------------------------------------------------------------------
62
63#if defined(__GNUC__)
64#pragma GCC diagnostic push
65#pragma GCC diagnostic ignored "-Waggregate-return"
66#if defined(__clang__)
67#pragma clang diagnostic ignored "-Wc++98-compat"
68#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
69#endif
70#endif
71
73{
74 // --------------------------------------------------------------------------
75
76 namespace math
77 {
78
94 template <class T>
95 [[nodiscard]] constexpr auto
96 abs (const T t) -> T
97 {
98 return t < T{} ? -t : t;
99 }
100
115 template <class T>
116 [[nodiscard]] constexpr auto
117 min_value (const T& lhs, const T& rhs) -> const T&
118 {
119 return (rhs < lhs) ? rhs : lhs;
120 }
121
136 template <class T, class Exp_T>
137 [[nodiscard]] constexpr auto
138 pow (const T base, const Exp_T exp) -> T
139 {
140 // If the exponent is 0, return 1, otherwise recurse.
141 return exp ? T (base * pow (base, exp - Exp_T (1))) : T (1);
142 }
143
158 template <class T, char... Cs>
159 [[nodiscard]] constexpr auto
160 num (void) -> T
161 {
162 // Assume all are digits or dot or apostrophe.
163 static_assert (
164 ((Cs == '.' or Cs == '\'' or (Cs >= '0' and Cs <= '9')) and ...));
165 T result{};
166 for (const char c : { Cs... })
167 {
168 if (c == '.')
169 {
170 break;
171 }
172 if (c >= '0' and c <= '9')
173 {
174 result = static_cast<T> (result * static_cast<T> (10)
175 + static_cast<T> (c - '0'));
176 }
177 }
178 return result;
179 }
180
198 template <class T, char... Cs>
199 [[nodiscard]] constexpr auto
200 den (void) -> T
201 {
202 constexpr const std::array cs{ Cs... };
203 T result{};
204 auto i = 0u;
205 while (cs[i++] != '.')
206 {
207 }
208
209 for (auto j = i; j < sizeof...(Cs); ++j)
210 {
211 result += pow (T (10), sizeof...(Cs) - j) * T (cs[j] - '0');
212 }
213 return result;
214 }
215
231 template <class T, char... Cs>
232 [[nodiscard]] constexpr auto
233 den_size (void) -> T
234 {
235 constexpr const std::array cs{ Cs... };
236 T i{};
237#if defined(__GNUC__)
238#pragma GCC diagnostic push
239#pragma GCC diagnostic ignored "-Wconversion"
240#endif
241 while (cs[i++] != '.')
242#if defined(__GNUC__)
243#pragma GCC diagnostic pop
244#endif
245 {
246 }
247
248 return T (sizeof...(Cs)) - i + T (1);
249 }
250
267 template <class T, class Value_T>
268 [[nodiscard]] constexpr auto
269 den_size (Value_T value) -> T
270 {
271 constexpr auto precision = Value_T (1e-7);
272 T result{};
273 Value_T tmp{};
274 do
275 {
276 value *= 10;
277#if defined(__GNUC__)
278#pragma GCC diagnostic push
279#if !defined(__clang__) // GCC only
280#pragma GCC diagnostic ignored "-Warith-conversion"
281#endif
282#if defined(__clang__)
283#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
284#endif
285#endif
286 tmp = value - T (value);
287#if defined(__GNUC__)
288#pragma GCC diagnostic pop
289#endif
290 ++result;
291 }
292 while (tmp > precision);
293
294 return result;
295 }
296
297 // ------------------------------------------------------------------------
298 } // namespace math
299
300 // --------------------------------------------------------------------------
301} // namespace micro_os_plus::micro_test_plus
302
303#if defined(__GNUC__)
304#pragma GCC diagnostic pop
305#endif
306
307// ----------------------------------------------------------------------------
308
309#endif // __cplusplus
310
311// ----------------------------------------------------------------------------
312
313#endif // MICRO_TEST_PLUS_MATH_INLINES_H_
314
315// ----------------------------------------------------------------------------
Mathematical utilities for the µTest++ testing framework.
constexpr auto den(void) -> T
Computes the decimal part of a number represented as an array of characters.
constexpr auto abs(const T t) -> T
Computes the absolute value of a given comparable value.
constexpr auto pow(const T base, const Exp_T exp) -> T
Generic exponentiation function to compute the power of a base raised to an exponent.
constexpr auto den_size(void) -> T
Computes the number of decimal places in a number represented as an array of characters.
constexpr auto num(void) -> T
Computes the integral value of a number represented as an array of characters.
constexpr auto min_value(const T &lhs, const T &rhs) -> const T &
Computes the minimum of two comparable values.
Primary namespace for the µTest++ testing framework.