Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

VECTOR3 FLOAT

Discussion in 'Scripting' started by rugal77, May 10, 2019.

  1. rugal77

    rugal77

    Joined:
    Aug 9, 2017
    Posts:
    44
    I using
    Code (CSharp):
    1.  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?
     
  2. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    There a some librarys for vectors using doubles
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    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. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Followup: today I learned that the == operator is overloaded for Vector3 to be similar to Mathf.Approximately:

    https://docs.unity3d.com/ScriptReference/Vector3-operator_eq.html

    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!
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    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.
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.

    Code (csharp):
    1. Debug.Log("(" + bones[i].localPosition.x.ToString() + ", " + bones[i].localPosition.y.ToString() + ", " + bones[i].localPosition.z.ToString() + ")");
     
  7. rahul-Mlympix

    rahul-Mlympix

    Joined:
    Oct 5, 2020
    Posts:
    2
    Debug.Log(transform.position.ToString("F8"));

    This will print upto 8 decimal places.
     
    Joe-Censored likes this.