# Simple Math Question

#### Carcophan

##### Member
Trying to understand a simple formula in some of the code I found in a tutorial. Hoping it isn't just wrong, so assuming it isn't "wrong", then why is it "right"?

The tutorial formula is: Var X = _cX * _var1 + _var1 / _var2;

Which works out specifically to: Var X = 16 * 32 + 32 / 2

Yes - They have Var1 + Var1

We all understand the order of operations as: P>E>M>D>A>S, but with no parenthesis, the final number I get is 272, and this numerically does not make sense in the code for what it is used for.

If I put my own parenthesis on it, I can work into (16 X 64) / 2 = 512 which is something a little more realistic, all things considered. But how can you get 'one from the other' if you didn't know?

#### FrostyCat

##### Member
If this is in the context of converting grid positions to pixel positions, the formula you showed is perfectly correct, and your interpretation of it is wrong.
• _cX is the integer part of horizontal grid position (i.e. number of squares from the left edge of the grid)
• _var1 is the width of each square on the grid
• _var2 is the the fractional part of the horizontal grid position
The expression you showed (16 * 32 + 32 / 2) is trying to find the total width of 16 whole squares plus half a square. In that case, the correct placement of parentheses is (16 * 32) + (32 / 2), which is perfectly compliant with PEMDAS. This gives (16 * 32) + (32 / 2) = 512 + 16 = 528.

If you got 272, it's because you just blindly punched in the symbols on a regular calculator in sequence. Plain calculators don't do PEMDAS, they blindly do arithmetic in the order that you entered. Instead of calculating (16*32)+(32/2), you calculated ((16*32)+32)/2.

• ParodyKnaveBob

#### Carcophan

##### Member
If this is in the context of converting grid positions to pixel positions, the formula you showed is perfectly correct, and your interpretation of it is wrong.
• _cX is the integer part of horizontal grid position (i.e. number of squares from the left edge of the grid)
• _var1 is the width of each square on the grid
• _var2 is the the fractional part of the horizontal grid position
The expression you showed (16 * 32 + 32 / 2) is trying to find the total width of 16 whole squares plus half a square. In that case, the correct placement of parentheses is (16 * 32) + (32 / 2), which is perfectly compliant with PEMDAS. This gives (16 * 32) + (32 / 2) = 512 + 16 = 528.

If you got 272, it's because you just blindly punched in the symbols on a regular calculator in sequence. Plain calculators don't do PEMDAS, they blindly do arithmetic in the order that you entered. Instead of calculating (16*32)+(32/2), you calculated ((16*32)+32)/2.
Good job on blowing my simplistic mind! Your comments make sense, thanks for the input FrostyCat.

Other than you knowing basic math, and the (correct) hunch about "If this is in the context of converting grid positions to pixel positions" - how would you know?

Meaning - if you didn't know that was the end goal/problem to begin with - how can you tell just by looking at something linear with simply plus and minus signs? You did a SUPERB job reverse engineering my poorly worded question. Kudos.

• ParodyKnaveBob

#### FrostyCat

##### Member
Other than you knowing basic math, and the (correct) hunch about "If this is in the context of converting grid positions to pixel positions" - how would you know?
I know it instantly because this is one of the most common positioning formulas in 2D computer graphics. Any draftsperson, digital graphics designer, front-end web developer or game programmer would have committed it to memory fairly early in their training, and cemented it through regular use.

Here's the form that people like me instantly recognize:

x = x0 + cx*cw
y = y0 + cy*ch

Where the grid starts at (x0, y0), each cell is cw by ch, and the grid position to convert is (cx, cy). Once you factor your expression into (16+1/2)*32 (i.e. applying distributive law in reverse), this fits the form exactly with the grid's starting point at zero. Another view is to treat the grid as being offset by half a square (i.e. x0 is 32*1/2, cx is 16). Either will give you the same conclusion in global coordinate space.

Along with the formulas for centering a subregion and dividing a region into guttered columns/rows, these formulas form the backbone of most 2D layout tasks. If you seek to enter any field that involves placing things exactly on a blueprint or a screen, memorizing them is an imperative rite of passage.

#### Carcophan

##### Member
memorizing them is an imperative rite of passage.
Challenge accepted!!!

Your follow up was enlightening. I feel like I owe you some tuition money or something, it was was so well worded. I will keep-on-keeping-on, and will continue the learning. Thanks again.