micro-test-plus 3.2.0
µTest++, a lightweight testing framework for embedded platforms
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus/)
4 * Copyright (c) 2021 Liviu Ionescu.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from <https://opensource.org/licenses/MIT/>.
11 *
12 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
13 * released under the terms of the Boost Version 1.0 Software License,
14 * which can be obtained from <https://www.boost.org/LICENSE_1_0.txt>.
15 */
16
17#ifndef MICRO_TEST_PLUS_MATH_H_
18#define MICRO_TEST_PLUS_MATH_H_
19
20// ----------------------------------------------------------------------------
21
22#ifdef __cplusplus
23
24// ----------------------------------------------------------------------------
25
26#include <array>
27
28// ----------------------------------------------------------------------------
29
30#if defined(__GNUC__)
31#pragma GCC diagnostic push
32#pragma GCC diagnostic ignored "-Wconversion"
33#if defined(__clang__)
34#pragma clang diagnostic ignored "-Wc++98-compat"
35#endif
36#endif
37
39{
40 // --------------------------------------------------------------------------
41
49 namespace math
50 {
54 template <class T>
55 [[nodiscard]] constexpr auto
56 abs (const T t) -> T
57 {
58 return t < T{} ? -t : t;
59 }
60
64 template <class T>
65 [[nodiscard]] constexpr auto
66 min_value (const T& lhs, const T& rhs) -> const T&
67 {
68 return (rhs < lhs) ? rhs : lhs;
69 }
70
74 template <class T, class Exp_T>
75 [[nodiscard]] constexpr auto
76 pow (const T base, const Exp_T exp) -> T
77 {
78 // If the exponent is 0, return 1, otherwise recurse.
79 return exp ? T (base * pow (base, exp - Exp_T (1))) : T (1);
80 }
81
86 template <class T, char... Cs>
87 [[nodiscard]] constexpr auto
88 num () -> T
89 {
90 // Assume all are digits or dot or apostrophe.
91 static_assert (
92 ((Cs == '.' or Cs == '\'' or (Cs >= '0' and Cs <= '9')) and ...));
93 T result{};
94 for (const char c : { Cs... })
95 {
96 if (c == '.')
97 {
98 break;
99 }
100 if (c >= '0' and c <= '9')
101 {
102 result = result * T (10) + T (c - '0');
103 }
104 }
105 return result;
106 }
107
112 template <class T, char... Cs>
113 [[nodiscard]] constexpr auto
114 den () -> T
115 {
116 constexpr const std::array cs{ Cs... };
117 T result{};
118 auto i = 0u;
119 while (cs[i++] != '.')
120 {
121 }
122
123 for (auto j = i; j < sizeof...(Cs); ++j)
124 {
125 result += pow (T (10), sizeof...(Cs) - j) * T (cs[j] - '0');
126 }
127 return result;
128 }
129
134 template <class T, char... Cs>
135 [[nodiscard]] constexpr auto
136 den_size () -> T
137 {
138 constexpr const std::array cs{ Cs... };
139 T i{};
140#if defined(__GNUC__)
141#pragma GCC diagnostic push
142#pragma GCC diagnostic ignored "-Wconversion"
143#endif
144 while (cs[i++] != '.')
145#if defined(__GNUC__)
146#pragma GCC diagnostic pop
147#endif
148 {
149 }
150
151 return T (sizeof...(Cs)) - i + T (1);
152 }
153
158 template <class T, class Value_T>
159 [[nodiscard]] constexpr auto
160 den_size (Value_T value) -> T
161 {
162 constexpr auto precision = Value_T (1e-7);
163 T result{};
164 Value_T tmp{};
165 do
166 {
167 value *= 10;
168#if defined(__GNUC__)
169#pragma GCC diagnostic push
170#if !defined(__clang__) // GCC only
171#pragma GCC diagnostic ignored "-Warith-conversion"
172#endif
173#if defined(__clang__)
174#pragma clang diagnostic ignored "-Wimplicit-int-float-conversion"
175#endif
176#endif
177 tmp = value - T (value);
178#if defined(__GNUC__)
179#pragma GCC diagnostic pop
180#endif
181 ++result;
182 }
183 while (tmp > precision);
184
185 return result;
186 }
187
188 } // namespace math
189
190 // --------------------------------------------------------------------------
191} // namespace micro_os_plus::micro_test_plus
192
193#if defined(__GNUC__)
194#pragma GCC diagnostic pop
195#endif
196
197// ----------------------------------------------------------------------------
198
199#endif // __cplusplus
200
201// ----------------------------------------------------------------------------
202
203#endif // MICRO_TEST_PLUS_MATH_H_
204
205// ----------------------------------------------------------------------------
constexpr auto abs(const T t) -> T
Generic absolute of any value.
Definition math.h:56
constexpr auto den() -> T
Compute the decimals of a number represented as an array of characters.
Definition math.h:114
constexpr auto pow(const T base, const Exp_T exp) -> T
Generic 'power of', to raise base to exponent (base ^ exp).
Definition math.h:76
constexpr auto num() -> T
Compute the integral value of a number represented as an array of characters.
Definition math.h:88
constexpr auto den_size() -> T
Compute the number of decimal places of a number represented as an array of characters.
Definition math.h:136
constexpr auto min_value(const T &lhs, const T &rhs) -> const T &
Generic minimum of two values.
Definition math.h:66