Viewing a single comment thread. View all comments

HaikuBotStalksMe t1_itqy3ug wrote

This kind of stuff is why I program safety measures in my game. Like "if x == 0, do blah. If x == 2, do blah2, if x==3 do blah 3. If x > 3, x= 3 continue, if x < 0, x = 0, continue"

10

relefos t1_itrpu1x wrote

I just wanted to leave a fun fact ~ this is not underflow, it's actually still just overflow. Underflow is when a number is too small to be properly stored as a float

Overflow happens when a number flows out of a certain range, so if you can store a number from 0 up to 255, and you try to add 1 to 255, you overflow. The same is true if you subtract 1 from 0 ~ it's still overflow

Here's this resource: https://www.educative.io/answers/what-are-overflow-and-underflow

u/alegonz if you're curious!

15

HaikuBotStalksMe t1_itseyy7 wrote

That's a fair point. But like usually overflow or underflows in games happen because you didn't bother setting up a safe upper or lower bound.

Like in 8 bits, you're gonna get up to 255. As such, I wouldn't let someone have more than like 200 points, and no single action will allow them to gain more than 50 points. And each time they gain or lose, I'm gonna make sure that if the value will be negative, then they can only go to 0 ("if score < pointslost: score = 0; else score -= pointslost"), and if adding any score goes over 200, then the score is set to 200.

My coworkers complain I overthink/over-safety my code sometimes, but hey, why risk it?

4

relefos t1_itvqq8n wrote

I 100% agree, just clarifying that under flow has nothing to do with setting a lower bound on an int

Going lower than your lowest bound is still just overflow

Underflow is specifically when you try to represent a floating point number smaller than your float can possibly represent. ie 0.0000000001 instead of 0.00001

Just a semantics misunderstanding thing I see a lot bc it honestly is unnecessarily confusing 😂

2

brimston3- t1_itrypm8 wrote

If -1 would overflow then the type is unsigned and the if x &lt; 0 check is too late; unsigned < 0 is never true. You must check the magnitude of the subtrahend against the minuend before the operation.

3