VECTOR3 FLOAT

I using

 bones[i].localPosition = new Vector3(4.521119E-28F, -3.447808E-22F, -0.2580215F);

And Unity return (0.0, 0.0, -0.3)
Is it possible to improve this accuracy?

There a some librarys for vectors using doubles

If you are just doing Debug.Log( v3float) then you will only see the above precision, as it comes out of Vector3.ToString()

If you make your own Vector3 print formatter then you can display whatever level of precision you want.

If you’re looking to compare for equality and failing, you should never compare floats (or doubles) for equality in any case.

Instead use Mathf.Approximately() or subtract and check against your own epsilon.

4 Likes

Followup: today I learned that the == operator is overloaded for Vector3 to be similar to Mathf.Approximately:

That’s … kinda frightening. But I can see it being useful, as long as your notion of “close enough” is less than 1e-5 and you don’t “step through” your goal to the other side!

To explain a bit more: when you ask Unity to print out the value of a Vector, it automatically does some rounding, because usually you don’t care about ALL the digits. It still uses higher-precision internally, behind the scenes.

There is still a maximum precision limit imposed by the fact that the components of a Unity Vector are stored as floats, following the IEEE standard for floating-point numbers. However, I believe the numbers in your example should be represented reasonably accurately within that format.

As Kurt-Dekker says, you generally shouldn’t rely on any two floating-point numbers being exactly equal; use approximate tests instead.

This will print the actual value of the vector3 instead of using the rounded Vector3.ToString(). I’m sure there is also a parameter to feed to Vector3.ToString() to do the same thing, but this way is just faster than googling.

Debug.Log("(" + bones[i].localPosition.x.ToString() + ", " + bones[i].localPosition.y.ToString() + ", " + bones[i].localPosition.z.ToString() + ")");

Debug.Log(transform.position.ToString(“F8”));

This will print upto 8 decimal places.

1 Like