Getting -1.025547E-07 insead of Zero, why?

Hello guys, I’m using a raycast to detect the walls Y Normal, to check if its a “flat” wall or not. It was always working, until recently, I’ve created a stage in blender, and one of those walls is getting me this strange value: -1.025547E-07 instead of zero, when it cleary should’ve been 0. so, because of this, my conditions are not happening.

if (wallHit.normal.y == 0)
{ etc... }

I know that these E-07 values are scientific notations, and very close to zero ( in blender, it says that the normal is zero… ), There’s a way to aproximate this value in unity, so i can recieve zero instead of this very small number?

It’s called ‘float imprecision’. The way computers handle decimal numbers isn’t ideal, and what often happens, is that it displays ‘the best float possible’. You can do either of 2 things in this case: round it or add a bigger margin.

Round it to 0.1 place:

if((Mathf.Round(wallHit.normal.y * 10) / 10 == 0) //;

Margin:

if(wallHit.normal.y < 0.1 || wallHit.normal.y > -0.1) //;
// or a more computationally intensive version
if(Mathf.Abs(wallHit.normaly.y) < 0.1) //;

PS: I usually choose the first margin method :wink:

–David

There is imprecision in floating point number calculations. Your value is: 0.0000001025547…very nearly zero, and the kind of thing you would see from the imprecision of floating point number calcualtions. The solution is to avoid comparing floating point numbers directly. One way is to use Mathf.Approximately() to compare two numbers.