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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

SerializedField float in inspector automatically rounds (Solved)

Discussion in 'Scripting' started by lord_of_shred, May 28, 2020.

  1. lord_of_shred

    lord_of_shred

    Joined:
    Jun 22, 2017
    Posts:
    3
    Hi everyone,

    I'm currently working on a project that needs to handle GPS latitude and longitude values.
    I have an object with a script attached that should hold the latitude/longitude values assigned to this object.

    Code (csharp):
    1. [SerializeField]
    2. [Tooltip("Set the longitude for the center of the object")]
    3. private double _poiCenterLongitude = 0.0f;
    Now if I enter a value like this: 48.37201 in the inspector it get's automatically rounded to 48.372. If I then output the value via

    Code (csharp):
    1. Debug.Log(_poiCenterLongitude);
    I get the rounded value as well. Is there any way to stop the inspector from messing with these values? Except changing the type to double? I think float should be enough (shouldn't take values larger than this, also Input.location.lastData.longitude is a float as well).

    Thanks in advance. I tried searching for a solution but didn't find anything useful.

    -lord_of_shred-
     
    Last edited: May 28, 2020
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,078
    The problem here is not unity but the way floating point numbers are stored.
    I guess the number you mentioned is not representable by a float. I didn't do math to proof this, but did some tests with that number myself.
    So, I guess you will need doubles if you require such a precision.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Unfortunately math doesn't care what anybody thinks. With a standard 32-bit
    float
    , you are actually only guaranteed about six decimal digits. Reviewing the details on wikipedia,

    https://en.wikipedia.org/wiki/Single-precision_floating-point_format

    I quote, "If a decimal string with at most 6 significant digits is converted to IEEE 754 single-precision representation, and then converted back to a decimal string with the same number of digits, the final result should match the original string."

    Your example of
    48.37201
    is actually 7 significant digits.

    Why not just store the coordinates in a string?
     
    lord_of_shred likes this.
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    IDK.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class KeyCodeTest : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     private double test;
    7.  
    8.     private void Start()
    9.     {
    10.         Debug.Log(test);
    11.     }
    12. }
    13.  
    answer.PNG

    And after hitting play.
    answer1.PNG

    2019.3.14f1
    Did I misunderstand something? (Don't mind the class name :D)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    We're talking about
    float
    types, which are 4-byte. In the original post, @lord_of_shred indicated he was not interested in
    double
    , that's all.
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    10,004
    Ah, I'm having code-fixation. :D I haven't seen the tailing question, only the code. Sorry, I'll see myself out.
     
    Kurt-Dekker likes this.
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    S'all good my friend... as a proper Ninja, you're welcome to hang out and nobody will notice you in the shadows anyway, right??
     
    Lurking-Ninja likes this.
  8. Cannist

    Cannist

    Joined:
    Mar 31, 2020
    Posts:
    64
    I think I'd just use an
    int
    to represent a fixed point longitude/latitude value with 6 decimals accuracy, so
    48.37201 would be stored as 48372010. You could even go up to 7 decimals if you wanted.
    There is probably a way to use a custom editor to still show the decimal point in the inspector, but I'd probably just make it plain in the variable names and call them e.g.
    latitudeMicros
    .
     
  9. lord_of_shred

    lord_of_shred

    Joined:
    Jun 22, 2017
    Posts:
    3
    Thanks for all your replies, I'll set this to (Solved).
    I went with doubles to avoid further troubles with it.

    You're right, math doesn't care ;-). I had thought about the IEEE 754 "restrictions" but dismissed it, since Unity seems to work with float for the GPS readout.