Re:Essential facts about floating point calculations
by mortoray on Monday July 06, 2015 @05:17AM
(#9837785)
I do mention if you simply increase the length of the formatted string you can do round-trip. It should be noted however (as I do) that the decimal number is still not equivalent to the actual floating point number, only that when converted back it is the same. This point could, in theory, be an issue if your serialized form crosses systems that use different floating point systems.
Re:Essential facts about floating point calculations
by brucedawson on Monday July 06, 2015 @06:01PM
(#9842465)
If you are trying to get exact compatibility between systems with different floating-point systems then serializing to text is the least of your problems.
Re:Essential facts about floating point calculations
by panic on Monday July 06, 2015 @06:09AM
(#9837976)
It should be noted however (as I do) that the decimal number is still not equivalent to the actual floating point number, only that when converted back it is the same.
Since 2 divides evenly into 10, every binary number has an exact decimal representation, and you can produce a decimal string which is equivalent to any floating point number. You may need quite a few digits, though.
Re:Essential facts about floating point calculations
by yoha on Monday July 06, 2015 @05:14AM
(#9837779)
> It is possible to get the same number back though, even when using decimal. If the truncation length is simply extended long enough the parsing will yield the same number back.
But I agree that actual code would have been clearer.
https://randomascii.wordpress.com/2012/03/08/float-precision...
Specifically, printf(“%1.8e”, f); ensures that a float will round-trip to decimal and back