ORIGINAL
Loading...
Searching...
No Matches
maths.h
Go to the documentation of this file.
1#ifndef MATHS_H
2#define MATHS_H
3#include "coroutines.h"
4#include "error.h"
5
6namespace original {
7
31constexpr l_floating E = 2.7182818284590452353602874713526624977572470937000;
32
40constexpr l_floating PI = 3.1415926535897932384626433832795028841971693993751;
41
60template<typename TYPE>
62
81template<typename TYPE>
83
102template<typename TYPE>
104
133
175template<std::integral INTEGER = int>
177
178} // namespace original
179
180// ----------------- Definitions of maths.h -----------------
181
182template<typename TYPE>
183auto original::abs(TYPE a) -> TYPE
184{
185 return a > TYPE{} ? a : -a;
186}
187
188template<typename TYPE>
189auto original::maximum(TYPE a, TYPE b) -> TYPE
190{
191 return a > b ? a : b;
192}
193
194template<typename TYPE>
195auto original::minimum(TYPE a, TYPE b) -> TYPE
196{
197 return a < b ? a : b;
198}
199
200inline auto original::pow(const floating base, const integer exp) -> floating
201{
202 if (base == 0 && exp <= 0)
203 {
204 throw valueError();
205 }
206 floating res = 1.0;
207 for (integer i = 0; i < abs(exp); i += 1)
208 {
209 res *= base;
210 }
211 return exp >= 0 ? res : 1 / res;
212}
213
214template <std::integral INTEGER>
215original::coroutine::generator<INTEGER> original::rangesOf(INTEGER start, INTEGER end, INTEGER steps)
216{
217 if (steps == 0 || (start - end) * steps > 0) {
218 co_return;
219 }
220 if (steps > 0) {
221 for (INTEGER i = start; i < end; i += steps) {
222 co_yield i;
223 }
224 } else {
225 for (INTEGER i = start; i > end; i += steps) {
226 co_yield i;
227 }
228 }
229}
230
231#endif //MATHS_H
Lazy sequence generator using C++20 coroutines.
Definition coroutines.h:57
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
Exception for invalid parameter values.
Definition error.h:219
C++20 coroutine support with generator pattern implementation.
Custom exception classes and callback validation utilities.
double floating
Double-precision floating-point type.
Definition config.h:331
Main namespace for the project Original.
Definition algorithms.h:21
coroutine::generator< INTEGER > rangesOf(INTEGER start, INTEGER end, INTEGER steps=1)
Generates a sequence of integers from start to end (exclusive) with a given steps.
TYPE abs(TYPE a)
Returns the absolute value of a given number.
TYPE minimum(TYPE a, TYPE b)
Returns the smaller of two given values.
constexpr l_floating PI
The mathematical constant PI (π).
Definition maths.h:40
constexpr l_floating E
The mathematical constant E (Euler's number).
Definition maths.h:31
floating pow(floating base, integer exp)
Returns the result of raising a base to an exponent.
Definition maths.h:200
TYPE maximum(TYPE a, TYPE b)
Returns the larger of two given values.