Can anyone explain why this would happen:
public float[ ][ ] nodeHeights
// I populate nodeheights with a world generator but set values to f1/f2 from it below:
float f1 = UI.worldRegion.nodeHeights[n][1];
float f2 = UI.worldRegion.nodeHeights[an][0];
Debug.Log(f1 + " - " + f2 + " = " + (f1 - f2));
Debug Log comes out as:
2.386338 - 2.386338 = 2.384186E-07 <—???
How or why is this possible?
If I set the f1 & f2 myself to 2.386338 it’s totally fine, but pulling from the array I’m getting this weird result and while I’m guessing it’s some maths issue with floats, I can’t get my head around it.
Thanks
Welcome to the world of floating point numbers!
It’s a floating point precision error. Note that the result you’re getting is extremely close to 0. All is (mostly) right with the world.
Try Changing your log statement to this for a little more clarity:
Debug.Log(f1.ToString("F8") + " - " + f2.ToString("F8") + " = " + (f1 - f2).ToString("F8"));
Your right, I did expect it to be but not this much as the result was coming back as 2.38 still, but sending to strings it’s giving: 2.38633800 - 2.38633800 = 0.00000024
It’s not coming back as “2.38”. Note that there’s E-07 appended to it. It’s a scientific notation.
It means the value * 10^-7, which is the same as value * 1/10^7 = value * 1/10000000 = value * 0.0000001.
Thanks Suddoha that explains much and helped me greatly, much appreciated.