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 KroniK'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 :)

–

That's just scientific notation.

–

1 Answer

1

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.