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