so I have some code in the update function that will be used for an accelerate/decelerate function. but when I run it, the math isn’t accurate, here is my code:
wDown gets turned true/false when w is pressed, and lifted, and veloFB is set to 0. when I run this, and stop pressing w, it either goes to around -2 with some random decimals, or it goes to a really high/low number, like -2e349994939234, why is this happening?
Memory has a finite amount of space… in the case of a “float” or “single” it’s 32-bits. In the case of a “double” it’s 64-bits.
Basically it has 32/64 digits of 1 or 0 to represent the number in question.
How it does this, in the case of floats/doubles, is basically what you may be familiar with as “scientific notation”. Where you write the number as its significant value * 10 raised to some power. Just in floats it’s * 2 raised to some power (cause binary).
It ends up looking like this in memory:
This means that you only have a finite amount of sig value range. If your value does not fit in it… well you lose information.
If you do arithmetic with 2 values that in different sig value ranges, it’s going to work in the largest sig value range (meaning really large numbers + really tiny numbers appear as no addition whatsoever).
Also you end up in situations where what looks like very simple decimal values are actually infinite repeating values in binary. Just like how 1/3 is 0.333333333… in decimal, well the simple value 1/10 is 0.00011011011011011011… repeating in binary. And since you only get a limited sig range you’re going to end up with all that infinite data chopped off and you end up with a value just shy of the real value.
It’s not going to -2. It’s probably going to something like -2e-<some large number>. The e is exponential notation, so that expression expands out to -2 * 10^-<some large number>, or -0.000000...0002. It’s actually extremely close to zero, but slightly less due to floating point imprecision. There’s nothing to fix, the math is not wrong.
When working with floats you need to factor the imprecision into your logic. Decide whether you actually need that precision (you probably don’t). If you do, then don’t use floating point numbers.
Use integers and convert it to float in the precision range when you apply it.
Or just ignore it, it’s not wrong, just ugly.
Also this } else if(wDown == false && veloFB > 0) should be this } else if(wDown == false && veloFB >= 0.2f)
Or add an if (veloFB < 0) veloFB = 0; inside after subtracting .2f
Because if you end up with .1f, the if statement will still be true and you will subtract .2f, so you will end up with -.1f
To get the final value as a float, you can simply multiply the value by 0.2f (or divide by 5f) whenever you use it somewhere. However if you don’t really care about the tiny errors, you could simply get rid of your range if statements and always increase / decrease the value whenever the button is pressed and just clamp the result between your desired values:
btw: You haven’t really said what your actual issue was. There is no really “large” value. What you probably got was an extremely small number, very close to 0. Probably something like 2.123456789e-50. That’s not a very large number as this is a number with 50 zeros before the decimal point. So my example number would actually be 0.00000000000000000000000000000000000000000000000002123456789
That’s because your logic depends on equality. A number like 2e-45 (which is about what you get from this kind of imprecision) is smaller than the diameter of an electron so that will literally never produce detectable movement. However, “the diameter of an electron greater than zero” is still greater than zero, so this:
will result in veloFB being -0.2f, which is your backwards movement. If your goal is “slow down by 0.2f until you reach 0 velocity”, use something like MoveTowards instead of plain subtraction:
Great explanation, thanks a lot. Your solution helped me. In general, mathematics occupies a very large place in my life, because I have loved it since childhood. Now I am studying at the university and continue to study mathematics professionally. Sometimes I go to the site https://plainmath.net/ which helps me with my math assignments. A very useful resource, I advise.