• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker (Solved) Help with a small math problem D':

Gasil

Member
EDIT: Holy crap, my english is truly bad, I'm sorry about that. I meant this:

Step Event.

A = 15;
B = 15;
C = 20;

if (A + B > C)
{
A += B - ...;
}

At the next step B is adding its value to A (15 + 15 = 30), however B will make A to become higher than C (30 > 20), and I don't want A to overshoot C, so I thought I should check that if in the last step A + B are greater than C, then subtract B the difference (30 - 20 = 10) and add it to A so it can finally be A == C.

I think that's better? Somehow, I think I answered my own question. I will check all your solutions and see which one I'm looking for.

Thank you all for your help <3
 
Last edited:
T

Taddio

Guest
Please don't be harsh, I just can't come up with the formula I need. It must be a simple thing but I just can't see it D':

Step Event.

A = 15;
B = 15;
C = 20;

if (A + B > C)
{
A += B - ...;
}

I need to check that if A + B is greater than C, then subtract B the excess over C that A + B make (10), so A += B can make A equals to C. I want to prevent A to go over C in the same check instead of placing another check below like;

If (A > C)
A = C;

Thank you.
Like this? Not sure I got your wording right, but that's what I understood:
Code:
var _d
_d = c-(a+b);
b -= _d;
 
The wording is really confusing, hahaha, but I think it's more along these lines:
Code:
if (a+b > c) {
  b = c-a;
}
a += b;
 
Last edited:

NightFrost

Member
If I understood that correctly... you wouldn't need a second check, because after you subtract the excess from A+B, it will be equal to C and never over. Anyway, you can do the math in one go by
Code:
A = min(A+B, C);
So A becomes A+B or C, whichever is smaller. That is, if this was what you were asking about.
 
D

dannyjenn

Guest
If I'm understanding you, you want to adjust A such that A + B will always equal C and such that A will always be less than or equal to C? And you only want to use a single line of code? I'm thinking you could do it like this:
Code:
A = (((C-B)>C)*C)+(((C-B)<=C)*(C-B));
(Note: I haven't actually tested that formula...)
However, that formula is extremely confusing. I'd just go with the if statements. It'll be a few extra lines of code but a lot less trouble.

edit - On second thought, maybe I'm misunderstanding your question. Because my formula makes A's initial value to be irrelevant... A ends up being the same regardless of whether it started off as 15, 99999, 0, or -10...
 

Gasil

Member
The wording is really confusing, hahaha, but I think it's more along these lines:
Code:
if (a+b > c) {
  b = c-a;
}
a += b;
Thank you this was what I was looking for. Much appreciated. Sorry about the confusion you all, thank you for coming by to help.
 
Top