ORIGINAL
Loading...
Searching...
No Matches
maths.h
Go to the documentation of this file.
1#ifndef MATHS_H
2#define MATHS_H
3#include "error.h"
4
5namespace original {
6
15
22constexpr long double E = 2.7182818284590452353602874713526624977572470937000L;
23
29constexpr long double PI = 3.1415926535897932384626433832795028841971693993751L;
30
45template<typename TYPE>
46TYPE abs(TYPE a);
47
63template<typename TYPE>
64TYPE max(TYPE a, TYPE b);
65
81template<typename TYPE>
82TYPE min(TYPE a, TYPE b);
83
105double pow(double base, int exp);
106
107} // namespace original
108
109// ----------------- Definitions of maths.h -----------------
110
111template<typename TYPE>
112auto original::abs(TYPE a) -> TYPE
113{
114 return a > TYPE{} ? a : -a;
115}
116
117template<typename TYPE>
118auto original::max(TYPE a, TYPE b) -> TYPE
119{
120 return a > b ? a : b;
121}
122
123template<typename TYPE>
124auto original::min(TYPE a, TYPE b) -> TYPE
125{
126 return a < b ? a : b;
127}
128
129inline auto original::pow(const double base, const int exp) -> double
130{
131 if (base == 0 && exp <= 0)
132 {
133 throw valueError();
134 }
135 double res = 1.0;
136 for (int i = 0; i < abs(exp); i += 1)
137 {
138 res *= base;
139 }
140 return exp >= 0 ? res : 1 / res;
141}
142
143#endif //MATHS_H
Exception for invalid parameter values.
Definition error.h:97
Custom exception classes and callback validation utilities.
Main namespace for the project Original.
Definition algorithms.h:21
TYPE min(TYPE a, TYPE b)
Returns the smaller of two given values.
TYPE max(TYPE a, TYPE b)
Returns the larger of two given values.
TYPE abs(TYPE a)
Returns the absolute value of a given number.
constexpr long double PI
The mathematical constant PI (π).
Definition maths.h:29
double pow(double base, int exp)
Returns the result of raising a base to an exponent.
Definition maths.h:129
constexpr long double E
The mathematical constant E (Euler's number).
Definition maths.h:22