Search code examples
c#.netfloating-pointprecisionfloating-point-conversion

Can float be round tripped via double without losing precision?


If I have a C# float, can I convert it to double without losing any precision?

If that double were converted back to float, would it have exactly the same value?


Solution

  • Yes. IEEE754 floating point (which is what C# must use) guarantees this:

    1. Converting a float to a double preserves exactly the same value

    2. Converting that double back to a float recovers exactly that original float.

    The set of doubles is a superset of floats.

    Note that this also applies to NaN, +Infinity, and -Infinity. The signedness of zero is also preserved.