My Project
Loading...
Searching...
No Matches
decimal.hpp
1#ifndef DECIMAL_H
2#define DECIMAL_H
3
4#include <cmath>
5#include <string>
6
7#ifdef FOUND_FLOAT_MODE
8 typedef float decimal;
9 #define STR_TO_DECIMAL(x) std::stof(x)
10#else
11 typedef double decimal;
12 #define STR_TO_DECIMAL(x) std::stod(x)
13#endif
14
15// This should only be used sparingly.
16// It's better to verbosely typecast sometimes. Only use these to prevent promotions.
17// The reason why this isn't used everywhere instead of the wrapped macros is
18// because the code becomes hard to read when there are multiple layers of typecasting.
19// With this method, we might have more preprocessing to do BUT the code remains readable
20// as the methods remain relatively the same.
21#define DECIMAL(x) (static_cast<decimal>(x))
22
23// Math Constants wrapped with Decimal typecast
24#define DECIMAL_M_E (static_cast<decimal>(M_E)) /* e */
25#define DECIMAL_M_LOG2E (static_cast<decimal>(M_LOG2E)) /* log_2 e */
26#define DECIMAL_M_LOG10E (static_cast<decimal>(M_LOG10E)) /* log_10 e */
27#define DECIMAL_M_LN2 (static_cast<decimal>(M_LN2)) /* log_e 2 */
28#define DECIMAL_M_LN10 (static_cast<decimal>(M_LN10)) /* log_e 10 */
29#define DECIMAL_M_PI (static_cast<decimal>(M_PI)) /* pi */
30#define DECIMAL_M_PI_2 (static_cast<decimal>(M_PI_2)) /* pi/2 */
31#define DECIMAL_M_PI_4 (static_cast<decimal>(M_PI_4)) /* pi/4 */
32#define DECIMAL_M_1_PI (static_cast<decimal>(M_1_PI)) /* 1/pi */
33#define DECIMAL_M_2_PI (static_cast<decimal>(M_2_PI)) /* 2/pi */
34#define DECIMAL_M_2_SQRTPI (static_cast<decimal>(M_2_SQRTPI)) /* 2/sqrt(pi) */
35#define DECIMAL_M_SQRT2 (static_cast<decimal>(M_SQRT2)) /* sqrt(2) */
36#define DECIMAL_M_SQRT1_2 (static_cast<decimal>(M_SQRT1_2)) /* 1/sqrt(2) */
37
38// Math Functions wrapped with Decimal typecast
39#define DECIMAL_POW(base,power) (static_cast<decimal>(std::pow(base), power))
40#define DECIMAL_SQRT(x) (static_cast<decimal>(std::sqrt(x)))
41#define DECIMAL_LOG(x) (static_cast<decimal>(std::log(x)))
42#define DECIMAL_EXP(x) (static_cast<decimal>(std::exp(x)))
43#define DECIMAL_ERF(x) (static_cast<decimal>(std::erf(x)))
44
45// Rouding methods wrapped with Decimal typecast)
46#define DECIMAL_ROUND(x) (static_cast<decimal>(std::round(x)))
47#define DECIMAL_CEIL(x) (static_cast<decimal>(std::ceil(x)))
48#define DECIMAL_FLOOR(x) (static_cast<decimal>(std::floor(x)))
49#define DECIMAL_ABS(x) (static_cast<decimal>(std::abs(x)))
50
51// Trig Methods wrapped with Decimal typecast)
52#define DECIMAL_SIN(x) (static_cast<decimal>(std::sin(x)))
53#define DECIMAL_COS(x) (static_cast<decimal>(std::cos(x)))
54#define DECIMAL_TAN(x) (static_cast<decimal>(std::tan(x)))
55#define DECIMAL_ASIN(x) (static_cast<decimal>(std::asin(x)))
56#define DECIMAL_ACOS(x) (static_cast<decimal>(std::acos(x)))
57#define DECIMAL_ATAN(x) (static_cast<decimal>(std::atan(x)))
58#define DECIMAL_ATAN2(x,y) (static_cast<decimal>(std::atan2(x),y))
59
60// Float methods wrapped with Decimal typecast)
61#define DECIMAL_FMA(x,y,z) (static_cast<decimal>(std::fma(x),y,z))
62#define DECIMAL_HYPOT(x,y) (static_cast<decimal>(std::hypot(x),y))
63
64#endif // DECIMAL_H