-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclibs_math.h
150 lines (134 loc) · 3.08 KB
/
clibs_math.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/** @file clibs_math.h
* @brief Common math operations.
* @author ByteBard
* @copyright MIT
*/
#ifndef CLIBS_CMATH_H
#define CLIBS_CMATH_H
/** @def MAX(a, b)
* @brief Get the maximal value between \a a and \a b
*/
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
/** @def MIN(a, b)
* @brief Get the minimal value between \a a and \a b
*/
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
/** @def ABS(a)
* @brief Get the absolute value of \a a
*/
#ifndef ABS
#define ABS(a) ((a) > 0.0 ? (a) : -(a))
#endif
/** @def IS_EQUAL(a, b, epsilon)
* @brief Check the equality between two floating-point numbers
*
* <b>IS_EQUAL(a, b, epsilon)</b> compares two floating-point numbers by the absolute value
* of the difference between \a a and \a b. The value should be smaller than
* \b epsilon
*/
#ifndef IS_EQUAL
#define IS_EQUAL(a, b, epsilon) (ABS((a) - (b)) <= (epsilon))
#endif
/** @def COMPARE(a, b)
* @brief Compare two numeric data
*/
#ifndef COMPARE
#define COMPARE(a, b) ((a) > (b) ? 1 : (a) == (b) ? 0 : -1)
#endif
/** @def IS_EVEN(n)
* @brief Check whether \a n is even
*/
#ifndef IS_EVEN
#define IS_EVEN(n) (!((n) & 1))
#endif
/** @def IS_ODD(n)
* @brief Check whether \a n is odd
*/
#ifndef IS_ODD
#define IS_ODD(n) ((n) & 1)
#endif
/** @def SWAP(a, b)
* @brief Swap between \a a and \a b
*
* \a a and \a b are integers
*/
#ifndef SWAP
#define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
#endif
/** @def RANDINT(min, max)
* @brief Get a random integer bewteen \a min and \a max
*
* Set a random seed by yourself.
*/
#ifndef RANDINT
#define RANDINT(min, max) ((min) + rand() % ((max) - (min) + 1))
#endif
/** @def INF
* @brief An infinity number
*/
#ifndef INF
#ifdef _MSC_VER
#include <math.h>
#define INF (-logf(0.0))
#else
#define INF (1.0 / 0.0)
#endif
#endif
/** @def IS_INF(n)
* @brief Check whether \a n is an infinity number
*/
#ifndef IS_INF
#ifdef _MSC_VER
#include <math.h>
#define IS_INF(n) (!_finite(n))
#else
#define IS_INF(n) ((n) > FLT_MAX)
#endif
#endif
/** @def NEG_INF
* @brief A negative infinity number
*/
#ifndef NEG_INF
#ifdef _MSC_VER
#include <math.h>
#define NEG_INF (logf(0.0))
#else
#define NEG_INF (-1.0 / 0.0)
#endif
#endif
/** @def IS_NEG_INF(n)
* @brief Check whether \a n is a negative infinity number
*/
#ifndef IS_NEG_INF
#ifdef _MSC_VER
#include <math.h>
#define IS_NEG_INF(n) (!_finite(n) && (n) < 0)
#else
#define IS_NEG_INF(n) (-(n) > FLT_MAX)
#endif
#endif
/** @def NaN
* @brief A NaN (not a number)
*/
#ifndef NaN
#ifdef _MSC_VER
#include <math.h>
#define NaN (logf(0.0) - logf(0.0))
#else
#define NaN (0.0 / 0.0)
#endif
#endif
/** @def IS_NaN(n)
* @brief Check whether \a n is NaN (not a number)
*/
#ifndef IS_NaN
#define IS_NaN(n) \
((n) > (n) ? 0 : \
(n) == (n) ? 0 : \
(n) < (n) ? 0 : 1)
#endif
#endif /* CLIBS_CMATH_H */