2

In this API (https://docs.gemini.com/rest-api/#current-order-book), it says:

The quantities and prices returned are returned as strings rather than numbers. The numbers returned are exact, not rounded, and it can be dangerous to treat them as floating point numbers.

Why would it be "dangerous" to treat the numeric strings as floats? Is it just the loss of precision? If it's the loss of precision, technically it's more "dangerous" to return floats in the first place. This quote seems to imply that the act of parsing floats might be "dangerous". Why might that be so?

Leo Jiang
  • 24,497
  • 49
  • 154
  • 284

1 Answers1

2

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

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256