Does someone have a good CTFE fmod() function ? The problem is that std.math.fmod() is itself not available at CT, neither do floor() or similar functions necessary to get the quotient when the input value is two times over/under the bounds.

Currently I have this one...

---
auto warp(T)(T x, T min, T max)
{
    if (x > max)
    {
        T rng = max - min;
        while (x > max + rng)
            x -= rng * 2;
        if (x > max)
            x -= rng;
    }
    else if (x < min)
    {
        T rng = max - min;
        while (x < min - rng)
            x += rng * 2;
        if (x < min)
            x += rng;
    }
    return x;
}
---

...but it fails to compile with certain float values. This example will consume a lot of memory ( I guess it's the while() loop in the CTFE VM who's responsible):

---
static assert(warp(2357136044, -5f, 5f).approxEqual(-1f));
---

Any suggestion ? Or maybe this is a limit ?

  • CTFE fmod ? userABCabc123 via Digitalmars-d-learn

Reply via email to