1 #ifndef VOR_FLOAT_H
2 #define VOR_FLOAT_H
3
4 #include <math.h>
5
6 #define SMIDGE 0.0001
7
8 // return true if f is OUTSIDE the range [SMIDGE..(max-SMIDGE)]
9 static inline int
fclip(float f,float max)10 fclip(float f, float max)
11 {
12 return f < SMIDGE || f >= (max - SMIDGE);
13 }
14
15 static inline float
fconstrain(float f,float max)16 fconstrain(float f, float max)
17 {
18 max -= SMIDGE;
19
20 if(f > max) {
21 return max;
22 }
23 if(f < SMIDGE) {
24 return SMIDGE;
25 }
26
27 return f;
28 }
29
30 static inline float
fconstrain2(float f,float min,float max)31 fconstrain2(float f, float min, float max)
32 {
33 min += SMIDGE;
34 max -= SMIDGE;
35
36 if(f > max) {
37 return max;
38 }
39 if(f < min) {
40 return min;
41 }
42
43 return f;
44 }
45
46 // wrap f so it's within the range [SMIDGE..(max-SMIDGE)]
47 // assumes f is not outside this range by more than (max - (2 * SMIDGE))
48 static inline float
fwrap(float f,float max)49 fwrap(float f, float max)
50 {
51 float upper = max - SMIDGE;
52 float range = upper - SMIDGE;
53
54 if(f > upper) {
55 f -= range;
56 }
57 if(f < SMIDGE) {
58 f += range;
59 }
60
61 return f;
62 }
63
64 #endif // VOR_FLOAT_H
65