Banjo API 0.0.1
C99 game development API
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1
24#ifndef BJ_MATH_H
25#define BJ_MATH_H
26
27#include <math.h>
28#include <float.h>
29
34
35#if defined(BJ_API_LONG_DOUBLE)
37 typedef long double bj_real;
39 #define BJ_F(x) x##L
41 #define BJ_EPSILON (LDBL_EPSILON)
42#elif defined(BJ_API_FLOAT64)
44 typedef double bj_real;
46 #define BJ_F(x) x
48 #define BJ_EPSILON (DBL_EPSILON)
49#else
51 typedef float bj_real;
53 #define BJ_F(x) x##f
55 #define BJ_EPSILON (FLT_EPSILON)
56#endif
57
61#define BJ_FI(x) BJ_F(1.0) / BJ_F(x)
62
64#define BJ_FZERO (BJ_F(0.0))
65
67
72
74#define BJ_PI_F (3.14159265358979323846f)
76#define BJ_TAU_F (6.28318530717958647692f)
78#define BJ_PI_D (3.14159265358979323846264338327950288)
80#define BJ_TAU_D (6.28318530717958647692528676655900576)
82#define BJ_PI_L (3.141592653589793238462643383279502884L)
84#define BJ_TAU_L (6.283185307179586476925286766559005768L)
86#define BJ_PI (BJ_F(3.141592653589793238462643383279502884))
88#define BJ_TAU (BJ_F(6.283185307179586476925286766559005768))
89
91
97
98#define bj_absf fabsf
99#define bj_acosf acosf
100#define bj_atan2f atan2f
101#define bj_copysignf copysignf
102#define bj_cosf cosf
103#define bj_expf expf
104#define bj_floorf floorf
105#define bj_fmodf fmodf
106#define bj_logf logf
107#define bj_maxf fmaxf
108#define bj_minf fminf
109#define bj_powf powf
110#define bj_roundf roundf
111#define bj_sinf sinf
112#define bj_sqrtf sqrtf
113#define bj_tanf tanf
114
116
122
123#define bj_absd fabs
124#define bj_acosd acos
125#define bj_atan2d atan2
126#define bj_copysignd copysign
127#define bj_cosd cos
128#define bj_expd exp
129#define bj_floord floor
130#define bj_fmodd fmod
131#define bj_logd log
132#define bj_maxd fmax
133#define bj_mind fmin
134#define bj_powd pow
135#define bj_roundd round
136#define bj_sind sin
137#define bj_sqrtd sqrt
138#define bj_tand tan
139
141
147
148#define bj_absl fabsl
149#define bj_acosl acosl
150#define bj_atan2l atan2l
151#define bj_copysignl copysignl
152#define bj_cosl cosl
153#define bj_expl expl
154#define bj_floorl floorl
155#define bj_fmodl fmodl
156#define bj_logl logl
157#define bj_maxl fmaxl
158#define bj_minl fminl
159#define bj_powl powl
160#define bj_roundl roundl
161#define bj_sinl sinl
162#define bj_sqrtl sqrtl
163#define bj_tanl tanl
164
166
172
173#if defined(BJ_API_LONG_DOUBLE)
174 #define bj_abs bj_absl
175 #define bj_acos bj_acosl
176 #define bj_atan2 bj_atan2l
177 #define bj_copysign bj_copysignl
178 #define bj_cos bj_cosl
179 #define bj_exp bj_expl
180 #define bj_floor bj_floorl
181 #define bj_fmod bj_fmodl
182 #define bj_log bj_logl
183 #define bj_max bj_maxl
184 #define bj_min bj_minl
185 #define bj_pow bj_powl
186 #define bj_round bj_roundl
187 #define bj_sin bj_sinl
188 #define bj_sqrt bj_sqrtl
189 #define bj_tan bj_tanl
190#elif defined(BJ_API_FLOAT64)
191 #define bj_abs bj_absd
192 #define bj_acos bj_acosd
193 #define bj_atan2 bj_atan2d
194 #define bj_copysign bj_copysignd
195 #define bj_cos bj_cosd
196 #define bj_exp bj_expd
197 #define bj_floor bj_floord
198 #define bj_fmod bj_fmodd
199 #define bj_log bj_logd
200 #define bj_max bj_maxd
201 #define bj_min bj_mind
202 #define bj_pow bj_powd
203 #define bj_round bj_roundd
204 #define bj_sin bj_sind
205 #define bj_sqrt bj_sqrtd
206 #define bj_tan bj_tand
207#else
208 #define bj_abs bj_absf
209 #define bj_acos bj_acosf
210 #define bj_atan2 bj_atan2f
211 #define bj_copysign bj_copysignf
212 #define bj_cos bj_cosf
213 #define bj_exp bj_expf
214 #define bj_floor bj_floorf
215 #define bj_fmod bj_fmodf
216 #define bj_log bj_logf
217 #define bj_max bj_maxf
218 #define bj_min bj_minf
219 #define bj_pow bj_powf
220 #define bj_round bj_roundf
221 #define bj_sin bj_sinf
222 #define bj_sqrt bj_sqrtf
223 #define bj_tan bj_tanf
224#endif
225
227
233
241static inline bj_real bj_clamp(bj_real x, bj_real lo, bj_real hi) {
242 return (x < lo) ? lo : (x > hi) ? hi : x;
243}
244
251static inline bj_real bj_step(bj_real edge, bj_real x) {
252 return (x < edge) ? BJ_FZERO : BJ_F(1.0);
253}
254
262static inline bj_real bj_smoothstep(bj_real e0, bj_real e1, bj_real x) {
263 bj_real t = (x - e0) / (e1 - e0);
264 t = (t < BJ_FZERO) ? BJ_FZERO : (t > BJ_F(1.0)) ? BJ_F(1.0) : t;
265 return t * t * (BJ_F(3.0) - BJ_F(2.0) * t);
266}
267
273static inline bj_real bj_fract(bj_real x) {
274 return x - bj_floor(x);
275}
276
284static inline bj_real bj_mod(bj_real x, bj_real y) {
285 bj_real m = bj_fmod(x, y);
286 if (m < BJ_FZERO) m += (y < BJ_FZERO) ? -y : y;
287 return m;
288}
289
291
297
304static inline int bj_real_eq (bj_real a, bj_real b) { return bj_abs(a - b) <= BJ_EPSILON; }
305
312static inline int bj_real_neq(bj_real a, bj_real b) { return !bj_real_eq(a, b); }
313
320static inline int bj_real_lt (bj_real a, bj_real b) { return (b - a) > BJ_EPSILON; }
321
328static inline int bj_real_gt (bj_real a, bj_real b) { return (a - b) > BJ_EPSILON; }
329
336static inline int bj_real_lte(bj_real a, bj_real b) { return !bj_real_gt(a, b); }
337
344static inline int bj_real_gte(bj_real a, bj_real b) { return !bj_real_lt(a, b); }
345
352static inline int bj_real_cmp(bj_real a, bj_real b) { return bj_real_lt(a,b) ? -1 : (bj_real_gt(a,b) ? 1 : 0); }
353
355
361
369 return bj_max(BJ_F(1.0), bj_max(bj_abs(a), bj_abs(b)));
370}
371
378static inline int bj_real_eq_rel (bj_real a, bj_real b) { bj_real s = bj_real_relative_scale(a,b); return bj_abs(a - b) <= (BJ_EPSILON * s); }
379
386static inline int bj_real_neq_rel(bj_real a, bj_real b) { return !bj_real_eq_rel(a, b); }
387
394static inline int bj_real_lt_rel (bj_real a, bj_real b) { bj_real s = bj_real_relative_scale(a,b); return (b - a) > (BJ_EPSILON * s); }
395
402static inline int bj_real_gt_rel (bj_real a, bj_real b) { bj_real s = bj_real_relative_scale(a,b); return (a - b) > (BJ_EPSILON * s); }
403
410static inline int bj_real_lte_rel(bj_real a, bj_real b) { return !bj_real_gt_rel(a, b); }
411
418static inline int bj_real_gte_rel(bj_real a, bj_real b) { return !bj_real_lt_rel(a, b); }
419
426static inline int bj_real_cmp_rel(bj_real a, bj_real b) { return bj_real_lt_rel(a,b) ? -1 : (bj_real_gt_rel(a,b) ? 1 : 0); }
427
429
435
441static inline int bj_real_is_zero(bj_real x) { return bj_abs(x) <= BJ_EPSILON; }
442
449static inline int bj_real_is_zero_scaled(bj_real x, bj_real scale) {
450 bj_real s = bj_max(BJ_F(1.0), bj_abs(scale));
451 return bj_abs(x) <= (BJ_EPSILON * s);
452}
453
459static inline bj_real bj_real_snap_zero(bj_real x) { return bj_real_is_zero(x) ? BJ_FZERO : x; }
460
467static inline bj_real bj_real_snorm_safe(bj_real x, bj_real len) { return bj_real_is_zero(len) ? BJ_FZERO : (x / len); }
468
470
474
475#endif /* BJ_MATH_H */
#define bj_floor
Floor.
Definition math.h:214
static int bj_real_gt_rel(bj_real a, bj_real b)
a > b by more than relative epsilon.
Definition math.h:402
static int bj_real_lte_rel(bj_real a, bj_real b)
a <= b within relative epsilon.
Definition math.h:410
static int bj_real_cmp_rel(bj_real a, bj_real b)
Three-way compare using relative epsilon.
Definition math.h:426
static bj_real bj_smoothstep(bj_real e0, bj_real e1, bj_real x)
Smooth Hermite step between e0 and e1.
Definition math.h:262
static bj_real bj_real_relative_scale(bj_real a, bj_real b)
Internal scale helper max(1, |a|, |b|).
Definition math.h:368
static int bj_real_eq_rel(bj_real a, bj_real b)
Equality within relative epsilon.
Definition math.h:378
static int bj_real_cmp(bj_real a, bj_real b)
Three-way compare using absolute epsilon.
Definition math.h:352
static int bj_real_neq_rel(bj_real a, bj_real b)
Inequality within relative epsilon.
Definition math.h:386
static int bj_real_gt(bj_real a, bj_real b)
a > b by more than absolute epsilon.
Definition math.h:328
static bj_real bj_clamp(bj_real x, bj_real lo, bj_real hi)
Clamp x to the closed interval [lo, hi].
Definition math.h:241
static bj_real bj_real_snorm_safe(bj_real x, bj_real len)
Safe scalar normalization.
Definition math.h:467
static int bj_real_lt(bj_real a, bj_real b)
a < b by more than absolute epsilon.
Definition math.h:320
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:64
#define bj_abs
Absolute value.
Definition math.h:208
static bj_real bj_fract(bj_real x)
Fractional part of x.
Definition math.h:273
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
Definition math.h:441
static int bj_real_gte_rel(bj_real a, bj_real b)
a >= b within relative epsilon.
Definition math.h:418
#define bj_fmod
Floating modulus.
Definition math.h:215
static int bj_real_is_zero_scaled(bj_real x, bj_real scale)
Scaled zero test using max(1, |scale|).
Definition math.h:449
static int bj_real_neq(bj_real a, bj_real b)
Inequality within absolute epsilon.
Definition math.h:312
static bj_real bj_mod(bj_real x, bj_real y)
Positive modulus with non-negative result magnitude.
Definition math.h:284
static bj_real bj_step(bj_real edge, bj_real x)
Step function.
Definition math.h:251
static int bj_real_gte(bj_real a, bj_real b)
a >= b within absolute epsilon.
Definition math.h:344
#define BJ_EPSILON
Machine epsilon for bj_real when float is selected.
Definition math.h:55
static bj_real bj_real_snap_zero(bj_real x)
Snap to exact zero under absolute epsilon.
Definition math.h:459
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:53
static int bj_real_lt_rel(bj_real a, bj_real b)
a < b by more than relative epsilon.
Definition math.h:394
static int bj_real_lte(bj_real a, bj_real b)
a <= b within absolute epsilon.
Definition math.h:336
float bj_real
Selected real type for float configuration.
Definition math.h:51
#define bj_max
Maximum of two floats.
Definition math.h:217
static int bj_real_eq(bj_real a, bj_real b)
Equality within absolute epsilon.
Definition math.h:304
C99 math shim with bj_real precision type and scalar utilities.