What are the risks of parsing numeric strings into floats?
Mapping infinite to finite
Common floating point types have a finite member set/size. Say the float
took up 32-bits. That leads to about 232 different float
values. A string has limitless combinations. Something has to give, regardless if the target float
is encoded using binary, decimal or stone knives and bears skins 1:40.
Risk: Assuming all values are encode-able.
Decimal to binary
Typical floats employ a binary notation/fraction while a string is often written using decimal digits.
"1.234"
has no exact equivalent value as a float
. Instead the parsing yields a nearby float
. The float
itself is exact, but not the same exact value as 1.234.
Risk: Assuming conversion was exact equivalent.
Range
Even if imprecision is tolerated, float
has a range, perhaps +/-1038, +/-10308 or the like. 10000! is not expected to be representable.
Value can only get so small too, before they are converted to 0.0.
Risk: Overflow/underflow.
The real danger is lack of understanding of how floating point differs in various cases from real math and how to cope. With these, subtleties can lead to infinite loops, lost money, crashed code, or inconsistencies across platforms.
In math there is the real number_line. With float
, the line is "dotted". Zoomed in example example 2