cfloating-pointprecisionieee-754single-precision

What is the approximate resolution of a single precision floating point number when its around zero


I am storing many longitudes and latitudes as doubles, I am wondering if I can get away with storing them as floats.

To answer this question, I need to know the approximate resolution of a single precision floating point number when the stored values are longitudes / latitudes (-180 to +180).


Solution

  • Your question may have several interpretations.

    If it is just for angles and for storage on a disk or on a device I would suggest you to store your values using a totally different technique: Store as 32 bit integer.

    int encodedAngle = (int)(value * (0x7FFFFFFF / 180.0));
    

    To recover it, do the contrary.

    double angle = (encodedAngle / (0x7FFFFFFF / 180.0));
    

    In this way you have full 31 bit resolution for 180 degrees and 1 bit for the sign.

    You can use this way also to keep your values in ram, the cost of this conversion is higher compared to work directly with doubles, but if you want to keep your memory low but resolution high this can work quite well. The cost is not so high, just a conversion to/from integer from/to double and a multiplication, modern processors will do it in a very little amount of time, and since the accessed memory is less, if the list contains a lot of values, your code will be more friendly with processor cache.

    Your resolution will be 180 / ((2^31) - 1) = 8.38190318 × 10^-8 degrees, not bad.