Search Unity

How to remove the inaccuracy when subtracting vectors (positions)?

Discussion in 'Scripting' started by Lesnikus, Mar 24, 2018.

  1. Lesnikus

    Lesnikus

    Joined:
    Aug 29, 2015
    Posts:
    47
    Wrong result when calculating float.
    I need to move several objects to the same distance. But if i repeat this operation many times, they are not in the places where they were supposed to be. With the help of the console I derived the values and it turned out that the result of subtraction is not correct. For example:

    This is the original position of the object:
    ObjectPosBefore = (0.0000000, -1.0000000, -90.6025600).

    The vector to which I should shift the object:
    MoveVector = (0.0000000, 0.0000000, 10.0610500).

    Result:
    ObjectPosAfter = (0.0000000, -1.0000000, -100.6636000).

    I have to subtract the MoveVector from ObjectPosBefore and get ObjectPosAfter. Now I will calculate manually: -90.6025600 - 10.0610500 = -100.6636100. This is the result of Unity: -100.6636000. Where are the 0.0000100? This happens all the time!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    There's only so much precision that a float can hold. That's just how it is, and not a Unity thing.
     
  3. Lesnikus

    Lesnikus

    Joined:
    Aug 29, 2015
    Posts:
    47
    How can I bypass this restriction? Maybe i can somehow round the fifth decimal number, what would the result be exact? How to move objects so that after 50-100 iterations they are in exactly the place designated for them?
    I checked when the object is standing still, it is static. The error occurs only when subtracting.

    The larger the integer, the less accurate the result. For example: -663,9017000 - 12,2105800, the result was even less accurate = -676.1122000, which is inaccurate at -0.0000800. How to be?!!
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    There's no real way to bypass it.

    If you are moving between points, though, you could have both ends setup as Vector3 positions and go towards one or the other.

    What is it you're trying to do, out of curiosity?
     
  5. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
    Floating Point Arithmetic

    You absolutely require 0.0000100 precision?
    Like @methos5k above, I can't imagine any scenario where that would be required.

    In a real-world scale, we're talking about nano-meters here...
    Are you perchance on the frustrating end of a boolean logic == float comparison?
    Yeah... those are always off. You need to establish a tolerance value.

    Code (CSharp):
    1.     if (Math.Abs(x - y) < 0.0001f )
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I think that is 100 micro meters ;) But nevertheless. heh
     
    FuguFirecracker likes this.
  7. Lesnikus

    Lesnikus

    Joined:
    Aug 29, 2015
    Posts:
    47
    I'm trying to realize an infinite world and since at a great distance from the center of the world coordinates, all objects are shaking, periodically I move all objects in the scene back to zero coordinates. I use this script as a basis: http://wiki.unity3d.com/index.php?title=Floating_Origin
     
    Last edited: Mar 24, 2018
  8. FuguFirecracker

    FuguFirecracker

    Joined:
    Sep 20, 2011
    Posts:
    419
  9. Lesnikus

    Lesnikus

    Joined:
    Aug 29, 2015
    Posts:
    47
    I find the reason! This is due to the fact that I subtracting the position of the player, rather than a static value! Problem solved