Description
I have a use case where I need to output a floating point number (either a f32 or a f64) into a generated Rust source file as text, which needs to be parsed by Rust as the original floating point number exactly. I did some searching and found out about the {:.}
format to print a floating point number with "all available precision". This seems to work, but according to the documentation for fmt, this isn't a valid formatting specifier. The documentation states:
There are three possible ways to specify the desired precision:
- An integer .N: (...)
- An integer or name followed by dollar sign .N$: (...)
- An asterisk .*: (...)
Additionally, printing a float with {}
seems to print the same thing as with {:.}
, at least for the floats I've tried, but as far as I can tell the precision of this isn't documented either. The documentation says that "printing floats with println and friends will often discard insignificant digits", which kind of suggests that it won't discard significant digits, but it doesn't actually say that, so I don't know if I can rely on it for a lossless roundtrip.
Is this a documentation issue, or is there no official way to print a float losslessly? (The floats I generate must be usable in const on stable, so I can't use from_bits, but I suppose transmute would work as an alternative)