X-Git-Url: https://jasonwoof.com/gitweb/?p=vor.git;a=blobdiff_plain;f=float.h;h=1f07d6c2b8b2df541ee2f26727d76f4d53fecf5f;hp=58f6616a8ec555d31da966fb44d2f2b5bd2e8eab;hb=HEAD;hpb=fe549d406b589b1c3db83aeea28c911292a5968e diff --git a/float.h b/float.h index 58f6616..1f07d6c 100644 --- a/float.h +++ b/float.h @@ -3,20 +3,61 @@ #include +#define SMIDGE 0.0001 + +// return true if f is OUTSIDE the range [SMIDGE..(max-SMIDGE)] static inline int fclip(float f, float max) { - return f < 0 || (float)f >= (float)max; + return f < SMIDGE || f >= (max - SMIDGE); +} + +static inline float +fconstrain(float f, float max) +{ + max -= SMIDGE; + + if(f > max) { + return max; + } + if(f < SMIDGE) { + return SMIDGE; + } + + return f; +} + +static inline float +fconstrain2(float f, float min, float max) +{ + min += SMIDGE; + max -= SMIDGE; + + if(f > max) { + return max; + } + if(f < min) { + return min; + } + + return f; } +// wrap f so it's within the range [SMIDGE..(max-SMIDGE)] +// assumes f is not outside this range by more than (max - (2 * SMIDGE)) static inline float fwrap(float f, float max) { - if((float)f >= (float)max) f = (float)f - (float)max; - else if(f < 0) { - f += max; - if((float)f >= (float)max) f = nextafterf(f, 0); + float upper = max - SMIDGE; + float range = upper - SMIDGE; + + if(f > upper) { + f -= range; } + if(f < SMIDGE) { + f += range; + } + return f; }