C: How to wrap a float to the interval [-pi, pi)

前端 未结 15 945
死守一世寂寞
死守一世寂寞 2020-11-28 21:47

I\'m looking for some nice C code that will accomplish effectively:

while (deltaPhase >= M_PI) deltaPhase -= M_TWOPI;
while (deltaPhase < -M_PI) deltaP         


        
15条回答
  •  生来不讨喜
    2020-11-28 22:44

    One-liner constant-time solution:

    Okay, it's a two-liner if you count the second function for [min,max) form, but close enough — you could merge them together anyways.

    /* change to `float/fmodf` or `long double/fmodl` or `int/%` as appropriate */
    
    /* wrap x -> [0,max) */
    double wrapMax(double x, double max)
    {
        /* integer math: `(max + x % max) % max` */
        return fmod(max + fmod(x, max), max);
    }
    /* wrap x -> [min,max) */
    double wrapMinMax(double x, double min, double max)
    {
        return min + wrapMax(x - min, max - min);
    }
    

    Then you can simply use deltaPhase = wrapMinMax(deltaPhase, -M_PI, +M_PI).

    The solutions is constant-time, meaning that the time it takes does not depend on how far your value is from [-PI,+PI) — for better or for worse.

    Verification:

    Now, I don't expect you to take my word for it, so here are some examples, including boundary conditions. I'm using integers for clarity, but it works much the same with fmod() and floats:

    • Positive x:
      • wrapMax(3, 5) == 3: (5 + 3 % 5) % 5 == (5 + 3) % 5 == 8 % 5 == 3
      • wrapMax(6, 5) == 1: (5 + 6 % 5) % 5 == (5 + 1) % 5 == 6 % 5 == 1
    • Negative x:
      • Note: These assume that integer modulo copies left-hand sign; if not, you get the above ("Positive") case.
      • wrapMax(-3, 5) == 2: (5 + (-3) % 5) % 5 == (5 - 3) % 5 == 2 % 5 == 2
      • wrapMax(-6, 5) == 4: (5 + (-6) % 5) % 5 == (5 - 1) % 5 == 4 % 5 == 4
    • Boundaries:
      • wrapMax(0, 5) == 0: (5 + 0 % 5) % 5 == (5 + 0) % 5 == 5 % 5 == 0
      • wrapMax(5, 5) == 0: (5 + 5 % 5) % 5 == (5 + 0) % 5== 5 % 5 == 0
      • wrapMax(-5, 5) == 0: (5 + (-5) % 5) % 5 == (5 + 0) % 5 == 5 % 5 == 0
        • Note: Possibly -0 instead of +0 for floating-point.

    The wrapMinMax function works much the same: wrapping x to [min,max) is the same as wrapping x - min to [0,max-min), and then (re-)adding min to the result.

    I don't know what would happen with a negative max, but feel free to check that yourself!

提交回复
热议问题