• Create Account

### #ActualHodgman

Posted 28 April 2013 - 07:47 PM

I'm not really sure how to answer it, as it's the same as any other operation -- how do you know when to use addition or division?

A lot of the time you need to know how to do an operation and the opposite of that operation. e.g. if an attack subtracts 5 from health, then to 'undo' that attack, you add 5 to health.
In these cases, then mod will come up whenever you're trying to do the opposite of integer division. Keep in mind though that this is only for integers -- with floats there's no remainder in division because they can store fractions as well as whole numbers.

One of the most common examples is calculating an index for a 2D array (when emulating a 2D array using a 1D array).
int* array2d = new int[width*height];

To calculate a 1D index from 2D x/y values, we can use:
int index = x + y*width;

If you want to then turn this index back into an x and y value, we need the opposite of this function.
Mathematically, we can rearrange the above formula to solve for x/y:
x = index - y*width
y = index/width - x/width

or working with real numbers, we can find y first then use it to find x:
y = RoundDown( index / width ) (this works because we know x < width, so x/width < 1)
x = index - y*width

or still working with real numbers, we can find x first then use it to find y:
x = Remainder( index / width ) (again, this works because we know x will be in the fractional part and y in the whole part)
y = index/width - x/width

or working with integers, we can take the two bold bits above that map easily over to integer code:
int y = index / width; (integer division rounds down)
int x = index % width;

### #7Hodgman

Posted 28 April 2013 - 07:47 PM

I'm not really sure how to answer it, as it's the same as any other operation -- how do you know when to use addition or division?

A lot of the time you need to know how to do an operation and the opposite of that operation. e.g. if an attack subtracts 5 from health, then to 'undo' that attack, you add 5 to health.
In these cases, then mod will come up whenever you're trying to do the opposite of integer division. Keep in mind though that this is only for integers -- with floats there's no remainder in division because they can store fractions as well as whole numbers.

One of the most common examples is calculating an index for a 2D array (when emulating a 2D array using a 1D array).
int* array2d = new int[width*height];

To calculate a 1D index from 2D x/y values, we can use:
int index = x + y*width;

If you want to then turn this index back into an x and y value, we need the opposite of this function.
Mathematically, we can rearrange the above formula to solve for x/y:
x = index - y*width
y = index/width - x/width

or working with real numbers, we can find y first then use it to find x:
y = RoundDown( index / width ) (this works because we know x < width, so x/width < 1)
x = index - y*width

or still working with real numbers, we can find x first then use it to find y:
x = Remainder( index / width ) (this works because we know x will be in the fractional part and y in the whole part)
y = index/width - x/width

or working with integers, we can take the two bold bits above that map easily over to integer code:
int y = index / width; (integer division rounds down)
int x = index % width;

### #6Hodgman

Posted 28 April 2013 - 07:46 PM

I'm not really sure how to answer it, as it's the same as any other operation -- how do you know when to use addition or division?

A lot of the time you need to know how to do an operation and the opposite of that operation. e.g. if an attack subtracts 5 from health, then to 'undo' that attack, you add 5 to health.
In these cases, then mod will come up whenever you're trying to do the opposite of integer division. Keep in mind though that this is only for integers -- with floats there's no remainder in division because they can store fractions as well as whole numbers.

One of the most common examples is calculating an index for a 2D array (when emulating a 2D array using a 1D array).
int* array2d = new int[width*height];

To calculate a 1D index from 2D x/y values, we can use:
int index = x + y*width;

If you want to then turn this index back into an x and y value, we need the opposite of this function.
Mathematically, we can rearrange the above formula to solve for x/y:
x = index - y*width
y = index/width - x/width

or (working with real numbers):
y = RoundDown( index / width ) (this works because we know x < width, so x/width < 1)
x = index - y*width

or (still working with real numbers):
x = Remainder( index / width ) (this works because we know x will be in the fractional part and y in the whole part)
y = index/width - x/width

or (working with integers):
int y = index / width; (integer division rounds down)
int x = index % width;

### #5Hodgman

Posted 28 April 2013 - 07:45 PM

I'm not really sure how to answer it, as it's the same as any other operation -- how do you know when to use addition or division?

A lot of the time you need to know how to do an operation and the opposite of that operation. e.g. if an attack subtracts 5 from health, then to 'undo' that attack, you add 5 to health.
In these cases, then mod will come up whenever you're trying to do the opposite of integer division. Keep in mind though that this is only for integers -- with floats there's no remainder in division because they can store fractions as well as whole numbers.

One of the most common examples is calculating an index for a 2D array (when emulating a 2D array using a 1D array).
int* array2d = new int[width*height];

To calculate a 1D index from 2D x/y values, we can use:
int index = x + y*width;

If you want to then turn this index back into an x and y value, we need the opposite of this function.
Mathematically, we can rearrange the above formula to solve for x/y:
x = index - y*width
y = index/width - x/width

or (working with real numbers):
y = RoundDown( index / width ) (this works because we know x < width, so x/width < 1)
x = index - y*width

or (still working with real numbers):
x = Remainder( index / width ) (this works because we know x will be in the fractional part and y in the whole part)
y = index/width - x/width

or (working with integers):
int y = index / width; (integer division rounds down)
int x = index % width;

### #4Hodgman

Posted 28 April 2013 - 07:44 PM

I'm not really sure how to answer it, as it's the same as any other operation -- how do you know when to use addition or division?

A lot of the time you need to know how to do an operation and the opposite of that operation. e.g. if an attack subtracts 5 from health, then to 'undo' that attack, you add 5 to health.
In these cases, then mod will come up whenever you're trying to do the opposite of integer division. Keep in mind though that this is only for integers -- with floats there's no remainder in division because they can store fractions as well as whole numbers.

One of the most common examples is calculating an index for a 2D array (when emulating a 2D array using a 1D array).
int* array2d = new int[width*height];

To calculate a 1D index from 2D x/y values, we can use:
int index = x + y*width;

If you want to then turn this index back into an x and y value, we need the opposite of this function.
Mathematically, we can rearrange the above formula to get:
x = index - y*width
and
index/width = x/width + y
y = index/width - x/width

or (working with real numbers):
y = RoundDown( index / width ) (this works because we know x < width, so x/width < 1)
x = index - y*width

or (still working with real numbers):
x = Remainder( index / width ) (this works because we know x will be in the fractional part and y in the whole part)
y = index/width - x/width

or (working with integers):
int y = index / width; (integer division rounds down)
int x = index % width;

### #3Hodgman

Posted 28 April 2013 - 07:40 PM

I'm not really sure how to answer it, as it's the same as any other operation -- how do you know when to use addition or division?

A lot of the time you need to know how to do an operation and the opposite of that operation. e.g. if an attack subtracts 5 from health, then to 'undo' that attack, you add 5 to health.
In these cases, then mod will come up whenever you're trying to do the opposite of integer division. Keep in mind though that this is only for integers -- with floats there's no remainder in division because they can store fractions as well as whole numbers.

One of the most common examples is calculating an index for a 2D array (when emulating a 2D array using a 1D array).
int* array2d = new int[width*height];

To calculate a 1D index from 2D x/y values, we can use:
int index = x + y*width;

If you want to then turn this index back into an x and y value, we need the opposite of this function.
Mathematically, we can rearrange the above to:
x = index - y*width
and
index/width = x/width + y
y = index/width - x/width

or
y = roundedDown( index / width ) (this works because we know x < width, so x/width < 1)
x = index - y*width
or
x = remainder( index / width ) (this works because we know x will be in the fractional part and y in the whole part)

or
int y = index / width; (integer division rounds down)
int x = index % width;

PARTNERS