- Home /
Subtraction gives completely wrong answers
I'm working on a tutorial (substituting C# for JS) for a camera that zooms out when the player jumps. Changing the variable to restrict the maximum zoom out had no effect, so I tested some stuff in the main camera's script and found an issue in this piece of code in the Update function:
if (camZoom)
{
targetCurPos = player.transform.position.y; // get target's current Y position
playerJumpHeight = targetCurPos - playerScript.groundPos; // get latest jump height
print ("STARTING POS " + playerScript.groundPos);
print ("CURRENT POS " + targetCurPos);
print (playerJumpHeight);
When I stand still, the starting and current positions both alternate between 0.74 and 0.7399999 and when subtracted, the answer alternates between - or + 5.0960464E-08. I have no idea where these answers are coming from. Am I missing something?
It's not "completely wrong", it's only very slightly wrong, and you should always expect that when using floating point.
Sorry, but without $$anonymous$$roni$$anonymous$$'s helpful answer I wouldn't have known that an E means that a number like 5.09etc. is actually a tiny decimal.
E is scientific notation, essentially. So, 5.0960464E-08 is the same as 5.0960464 x 10^-8 . So that means you move the decimal eight places to the left. The same would be true if it was a positive eight, you'd just move it to the right eight places. Glad to help :)
Answer by xKroniK13x · Apr 16, 2013 at 03:26 AM
A lot of times things like this will have weird decimals. 5.0960464E-08 is essentially 0, since it is .000000050960464. If you're aiming for 0 here, it's negligible. I've had lot of crazy decimals like this throughout coding, especially when doing something like manipulating the positions of a player. Unless it is clean cut like 2+2=4, then you may get weird numbers.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
subtracting numbers gives a very wrong answer 0 Answers
Connect matlab and unity 0 Answers