Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1
49#ifndef BJ_MATH_H
50#define BJ_MATH_H
51
52#include <math.h>
53#include <float.h>
54
59
60#if defined(BJ_API_LONG_DOUBLE)
62 typedef long double bj_real;
64 #define BJ_F(x) x##L
66 #define BJ_EPSILON (LDBL_EPSILON)
67#elif defined(BJ_API_FLOAT64)
69 typedef double bj_real;
71 #define BJ_F(x) x
73 #define BJ_EPSILON (DBL_EPSILON)
74#else
76 typedef float bj_real;
78 #define BJ_F(x) x##f
80 #define BJ_EPSILON (FLT_EPSILON)
81#endif
82
86#define BJ_FI(x) BJ_F(1.0) / BJ_F(x)
87
89#define BJ_FZERO (BJ_F(0.0))
90
92
97
99#define BJ_PI_F (3.14159265358979323846f)
101#define BJ_TAU_F (6.28318530717958647692f)
103#define BJ_PI_D (3.14159265358979323846264338327950288)
105#define BJ_TAU_D (6.28318530717958647692528676655900576)
107#define BJ_PI_L (3.141592653589793238462643383279502884L)
109#define BJ_TAU_L (6.283185307179586476925286766559005768L)
111#define BJ_PI (BJ_F(3.141592653589793238462643383279502884))
113#define BJ_TAU (BJ_F(6.283185307179586476925286766559005768))
114
116
122
123#define bj_absf fabsf
124#define bj_acosf acosf
125#define bj_atan2f atan2f
126#define bj_copysignf copysignf
127#define bj_cosf cosf
128#define bj_expf expf
129#define bj_floorf floorf
130#define bj_fmodf fmodf
131#define bj_logf logf
132#define bj_maxf fmaxf
133#define bj_minf fminf
134#define bj_powf powf
135#define bj_roundf roundf
136#define bj_sinf sinf
137#define bj_sqrtf sqrtf
138#define bj_tanf tanf
139
141
147
148#define bj_absd fabs
149#define bj_acosd acos
150#define bj_atan2d atan2
151#define bj_copysignd copysign
152#define bj_cosd cos
153#define bj_expd exp
154#define bj_floord floor
155#define bj_fmodd fmod
156#define bj_logd log
157#define bj_maxd fmax
158#define bj_mind fmin
159#define bj_powd pow
160#define bj_roundd round
161#define bj_sind sin
162#define bj_sqrtd sqrt
163#define bj_tand tan
164
166
172
173#define bj_absl fabsl
174#define bj_acosl acosl
175#define bj_atan2l atan2l
176#define bj_copysignl copysignl
177#define bj_cosl cosl
178#define bj_expl expl
179#define bj_floorl floorl
180#define bj_fmodl fmodl
181#define bj_logl logl
182#define bj_maxl fmaxl
183#define bj_minl fminl
184#define bj_powl powl
185#define bj_roundl roundl
186#define bj_sinl sinl
187#define bj_sqrtl sqrtl
188#define bj_tanl tanl
189
191
197
198#if defined(BJ_API_LONG_DOUBLE)
199 #define bj_abs bj_absl
200 #define bj_acos bj_acosl
201 #define bj_atan2 bj_atan2l
202 #define bj_copysign bj_copysignl
203 #define bj_cos bj_cosl
204 #define bj_exp bj_expl
205 #define bj_floor bj_floorl
206 #define bj_fmod bj_fmodl
207 #define bj_log bj_logl
208 #define bj_max bj_maxl
209 #define bj_min bj_minl
210 #define bj_pow bj_powl
211 #define bj_round bj_roundl
212 #define bj_sin bj_sinl
213 #define bj_sqrt bj_sqrtl
214 #define bj_tan bj_tanl
215#elif defined(BJ_API_FLOAT64)
216 #define bj_abs bj_absd
217 #define bj_acos bj_acosd
218 #define bj_atan2 bj_atan2d
219 #define bj_copysign bj_copysignd
220 #define bj_cos bj_cosd
221 #define bj_exp bj_expd
222 #define bj_floor bj_floord
223 #define bj_fmod bj_fmodd
224 #define bj_log bj_logd
225 #define bj_max bj_maxd
226 #define bj_min bj_mind
227 #define bj_pow bj_powd
228 #define bj_round bj_roundd
229 #define bj_sin bj_sind
230 #define bj_sqrt bj_sqrtd
231 #define bj_tan bj_tand
232#else
233 #define bj_abs bj_absf
234 #define bj_acos bj_acosf
235 #define bj_atan2 bj_atan2f
236 #define bj_copysign bj_copysignf
237 #define bj_cos bj_cosf
238 #define bj_exp bj_expf
239 #define bj_floor bj_floorf
240 #define bj_fmod bj_fmodf
241 #define bj_log bj_logf
242 #define bj_max bj_maxf
243 #define bj_min bj_minf
244 #define bj_pow bj_powf
245 #define bj_round bj_roundf
246 #define bj_sin bj_sinf
247 #define bj_sqrt bj_sqrtf
248 #define bj_tan bj_tanf
249#endif
250
252
258
266static inline bj_real bj_clamp(bj_real x, bj_real lo, bj_real hi) {
267 return (x < lo) ? lo : (x > hi) ? hi : x;
268}
269
276static inline bj_real bj_step(bj_real edge, bj_real x) {
277 return (x < edge) ? BJ_FZERO : BJ_F(1.0);
278}
279
287static inline bj_real bj_smoothstep(bj_real e0, bj_real e1, bj_real x) {
288 bj_real t = (x - e0) / (e1 - e0);
289 t = (t < BJ_FZERO) ? BJ_FZERO : (t > BJ_F(1.0)) ? BJ_F(1.0) : t;
290 return t * t * (BJ_F(3.0) - BJ_F(2.0) * t);
291}
292
298static inline bj_real bj_fract(bj_real x) {
299 return x - bj_floor(x);
300}
301
309static inline bj_real bj_mod(bj_real x, bj_real y) {
310 bj_real m = bj_fmod(x, y);
311 if (m < BJ_FZERO) m += (y < BJ_FZERO) ? -y : y;
312 return m;
313}
314
316
335
342static inline int bj_real_eq (bj_real a, bj_real b) { return bj_abs(a - b) <= BJ_EPSILON; }
343
350static inline int bj_real_neq(bj_real a, bj_real b) { return !bj_real_eq(a, b); }
351
358static inline int bj_real_lt (bj_real a, bj_real b) { return (b - a) > BJ_EPSILON; }
359
366static inline int bj_real_gt (bj_real a, bj_real b) { return (a - b) > BJ_EPSILON; }
367
374static inline int bj_real_lte(bj_real a, bj_real b) { return !bj_real_gt(a, b); }
375
382static inline int bj_real_gte(bj_real a, bj_real b) { return !bj_real_lt(a, b); }
383
390static 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); }
391
393
399
407 return bj_max(BJ_F(1.0), bj_max(bj_abs(a), bj_abs(b)));
408}
409
416static 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); }
417
424static inline int bj_real_neq_rel(bj_real a, bj_real b) { return !bj_real_eq_rel(a, b); }
425
432static 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); }
433
440static 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); }
441
448static inline int bj_real_lte_rel(bj_real a, bj_real b) { return !bj_real_gt_rel(a, b); }
449
456static inline int bj_real_gte_rel(bj_real a, bj_real b) { return !bj_real_lt_rel(a, b); }
457
464static 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); }
465
467
473
479static inline int bj_real_is_zero(bj_real x) { return bj_abs(x) <= BJ_EPSILON; }
480
487static inline int bj_real_is_zero_scaled(bj_real x, bj_real scale) {
488 bj_real s = bj_max(BJ_F(1.0), bj_abs(scale));
489 return bj_abs(x) <= (BJ_EPSILON * s);
490}
491
497static inline bj_real bj_real_snap_zero(bj_real x) { return bj_real_is_zero(x) ? BJ_FZERO : x; }
498
505static inline bj_real bj_real_snorm_safe(bj_real x, bj_real len) { return bj_real_is_zero(len) ? BJ_FZERO : (x / len); }
506
508
512
513#endif /* BJ_MATH_H */
#define bj_floor
Floor.
Definition math.h:239
static int bj_real_gt_rel(bj_real a, bj_real b)
a > b by more than relative epsilon.
Definition math.h:440
static int bj_real_lte_rel(bj_real a, bj_real b)
a <= b within relative epsilon.
Definition math.h:448
static int bj_real_cmp_rel(bj_real a, bj_real b)
Three-way compare using relative epsilon.
Definition math.h:464
static bj_real bj_smoothstep(bj_real e0, bj_real e1, bj_real x)
Smooth Hermite step between e0 and e1.
Definition math.h:287
static bj_real bj_real_relative_scale(bj_real a, bj_real b)
Internal scale helper max(1, |a|, |b|).
Definition math.h:406
static int bj_real_eq_rel(bj_real a, bj_real b)
Equality within relative epsilon.
Definition math.h:416
static int bj_real_cmp(bj_real a, bj_real b)
Three-way compare using absolute epsilon.
Definition math.h:390
static int bj_real_neq_rel(bj_real a, bj_real b)
Inequality within relative epsilon.
Definition math.h:424
static int bj_real_gt(bj_real a, bj_real b)
a > b by more than absolute epsilon.
Definition math.h:366
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:266
static bj_real bj_real_snorm_safe(bj_real x, bj_real len)
Safe scalar normalization.
Definition math.h:505
static int bj_real_lt(bj_real a, bj_real b)
a < b by more than absolute epsilon.
Definition math.h:358
#define BJ_FZERO
Zero constant in bj_real.
Definition math.h:89
#define bj_abs
Absolute value.
Definition math.h:233
static bj_real bj_fract(bj_real x)
Fractional part of x.
Definition math.h:298
static int bj_real_is_zero(bj_real x)
Absolute-zero test.
Definition math.h:479
static int bj_real_gte_rel(bj_real a, bj_real b)
a >= b within relative epsilon.
Definition math.h:456
#define bj_fmod
Floating modulus.
Definition math.h:240
static int bj_real_is_zero_scaled(bj_real x, bj_real scale)
Scaled zero test using max(1, |scale|).
Definition math.h:487
static int bj_real_neq(bj_real a, bj_real b)
Inequality within absolute epsilon.
Definition math.h:350
static bj_real bj_mod(bj_real x, bj_real y)
Positive modulus with non-negative result magnitude.
Definition math.h:309
static bj_real bj_step(bj_real edge, bj_real x)
Step function.
Definition math.h:276
static int bj_real_gte(bj_real a, bj_real b)
a >= b within absolute epsilon.
Definition math.h:382
#define BJ_EPSILON
Machine epsilon for bj_real when float is selected.
Definition math.h:80
static bj_real bj_real_snap_zero(bj_real x)
Snap to exact zero under absolute epsilon.
Definition math.h:497
#define BJ_F(x)
Literal suffix helper for bj_real when float is selected.
Definition math.h:78
static int bj_real_lt_rel(bj_real a, bj_real b)
a < b by more than relative epsilon.
Definition math.h:432
static int bj_real_lte(bj_real a, bj_real b)
a <= b within absolute epsilon.
Definition math.h:374
float bj_real
Selected real type for float configuration.
Definition math.h:76
#define bj_max
Maximum of two floats.
Definition math.h:242
static int bj_real_eq(bj_real a, bj_real b)
Equality within absolute epsilon.
Definition math.h:342
C99 math shim with bj_real precision type and scalar utilities.