Is my math wrong or is Unity's?

I put in

Debug.Log ((x - x % 64) + ", " + (y - y % 64));
Debug.Log ((x % 64) + ", " + (y % 64));

into the start function. x = -1, y = -1. The output it gives me is 0, 0 and -1, -1. Shouldn’t it be -64, -64 and 63, 63?

Modulo is the remainder of the left value divided by the right value.

a % b = remainder of a / b

As order of operations go, modulo has the same precedence as a multiplicative operator (* / %).

So: x - x % 64 operates as: x - (x % 64)

-1 / 64 will be 0 with a remainder of -1
-1 - -1 = 0

I’m assuming you expect -1 % 64 to equal 63 because it appears to wrap positive values. But it actually doesn’t wrap, it only appears to if both a and b are positive.

I have found myself needing this ‘wrapping’ and wrote this for it:

        public static float Wrap(float value, float max, float min)
        {
            value -= min;
            max -= min;
            if (max == 0)
                return min;

            value = value % max;
            value += min;
            while (value < min)
            {
                value += max;
            }

            return value;
        }
        public static float Wrap(float value, float max)
        {
            float min = 0;

            value -= min;
            max -= min;
            if (max == 0)
                return min;

            value = (value % max);
            value += min;
            while (value < min)
            {
                value += max;
            }

            return value;
        }

        public static float ArithWrap(float value, float max, float min)
        {
            max -= min;
            if (max == 0)
                return min;

            return value - max * (float)Math.Floor((value - min) / max);
        }
        public static float ArithWrap(float value, float max)
        {
            return ArithWrap(value, max, 0);
        }

Can be found here:

I wrote 2 versions, one that does it as a while loop. The other arithmetically. Haven’t decided which I like more. The while loop is slow for large values, but ArithWrap is slow for small values.

1 Like

That makes sense…So what did I do different here than? This outcome is ideally what I want:

I edited my post probably during the time you were posting that, as I figured you’d probably want that answer.

I apologize, but it is 2am, which is probably why I am having a hard time understanding the function :stuck_out_tongue: What is min and what is max? I think you already guessed what I am trying to do correctly, I want to be able to put in a number and have it return the grid point that it falls in (for that cell system you were helping me with earlier). So if I give it the point -100 and I wanted it on a grid with a size 64 it would give me -128 (grid points are in the bottom left).

Actually, I don’t think I guessed what you want correctly.

I don’t know how -100 would turn into -128 for a grid of size 64…

I figured it out :slight_smile:

int GetGrid(int x, int size)
{
    return x - (x % size + size) % size;
}

cool, yeah, so not what I was thinking you wanted.

But I can see what you meant.