clibs
Tiny but handy utility libraries for C
Loading...
Searching...
No Matches
clibs_math.h
Go to the documentation of this file.
1
6#ifndef CLIBS_CMATH_H
7#define CLIBS_CMATH_H
8
12#ifndef MAX
13 #define MAX(a, b) ((a) > (b) ? (a) : (b))
14#endif
15
19#ifndef MIN
20 #define MIN(a, b) ((a) < (b) ? (a) : (b))
21#endif
22
26#ifndef ABS
27 #define ABS(a) ((a) > 0.0 ? (a) : -(a))
28#endif
29
37#ifndef IS_EQUAL
38 #define IS_EQUAL(a, b, epsilon) (ABS((a) - (b)) <= (epsilon))
39#endif
40
44#ifndef COMPARE
45 #define COMPARE(a, b) ((a) > (b) ? 1 : (a) == (b) ? 0 : -1)
46#endif
47
51#ifndef IS_EVEN
52 #define IS_EVEN(n) (!((n) & 1))
53#endif
54
58#ifndef IS_ODD
59 #define IS_ODD(n) ((n) & 1)
60#endif
61
67#ifndef SWAP
68 #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
69#endif
70
76#ifndef RANDINT
77 #define RANDINT(min, max) ((min) + rand() % ((max) - (min) + 1))
78#endif
79
83#ifndef INF
84 #ifdef _MSC_VER
85 #include <math.h>
86 #define INF (-logf(0.0))
87 #else
88 #define INF (1.0 / 0.0)
89 #endif
90#endif
91
95#ifndef IS_INF
96 #ifdef _MSC_VER
97 #include <math.h>
98 #define IS_INF(n) (!_finite(n))
99 #else
100 #define IS_INF(n) ((n) > FLT_MAX)
101 #endif
102#endif
103
107#ifndef NEG_INF
108 #ifdef _MSC_VER
109 #include <math.h>
110 #define NEG_INF (logf(0.0))
111 #else
112 #define NEG_INF (-1.0 / 0.0)
113 #endif
114#endif
115
119#ifndef IS_NEG_INF
120 #ifdef _MSC_VER
121 #include <math.h>
122 #define IS_NEG_INF(n) (!_finite(n) && (n) < 0)
123 #else
124 #define IS_NEG_INF(n) (-(n) > FLT_MAX)
125 #endif
126#endif
127
131#ifndef NaN
132 #ifdef _MSC_VER
133 #include <math.h>
134 #define NaN (logf(0.0) - logf(0.0))
135 #else
136 #define NaN (0.0 / 0.0)
137 #endif
138#endif
139
143#ifndef IS_NaN
144 #define IS_NaN(n) \
145 ((n) > (n) ? 0 : \
146 (n) == (n) ? 0 : \
147 (n) < (n) ? 0 : 1)
148#endif
149
150#endif /* CLIBS_CMATH_H */