1

Given a backend that sends numbers with either 4 or 6 digits after the decimal point, will these numbers always be exactly converted to JavaScripts number type (during JSON deserialisation)? Which numbers will cause problems?

Hint: assume the backend is precise, no rounding errors etc. I need to understand at which point problems can occur on the frontend though.

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270
  • why would they change, they are decimal values not strings that will be formatted – midugh May 14 '21 at 17:05
  • @R.LM Couldn't there be some rounding errors while converting them to JavaScripts number type (or later converting them from number type to string) – stefan.at.kotlin May 14 '21 at 17:06
  • number is just a name that allows users not to have to worry about the type of number used, but on the system a value won't be change magically unless you ask it to change – midugh May 14 '21 at 17:08
  • I think you're just asking https://stackoverflow.com/questions/588004/is-floating-point-math-broken – jonrsharpe May 14 '21 at 17:09
  • 1
    Frame challenge: do you need them *as numbers*? If you're only going to display them, then you can pass them as strings and never worry about floating point arithmetic. – VLAZ May 14 '21 at 17:09
  • thank you all for your comments. I need to think more about it and likely ask a more specific question. – stefan.at.kotlin May 14 '21 at 17:11
  • 1
    @VLAZ I think this is a fair question. Is there a possibility of getting a different number when a decimal with 6 places is parsed? This is not the same as 0.1+0.2 OR other math operations. – adiga May 14 '21 at 17:16
  • 2
    JS (double-precision floating point) numbers have around 16 decimal digits of accuracy. How many of them are before or after the decimal point does not matter, you need to count both sides. So as long as you have <=10 digits before and <= 6 digits after the point, you'll be fine. – Bergi May 14 '21 at 17:18
  • @Bergi Thanks, that goes directly in the direction I need to understand. Can you provide more details? E.g. which number would just still work and which one wouldn't anymore? – stefan.at.kotlin May 14 '21 at 17:20
  • 2
    @Bergi I think you should post this as an answer – adiga May 14 '21 at 17:22
  • @stefan.at.wpf https://en.wikipedia.org/wiki/IEEE_754#Character_representation – Bergi May 14 '21 at 17:24
  • @Bergi Thanks, will have a look at that. As suggested by adiga, please consider posting as answer. Obviously you are into this topic :-) – stefan.at.kotlin May 14 '21 at 17:29

1 Answers1

0

Bergi's comment:

JS (double-precision floating point) numbers have around 16 decimal digits of accuracy. How many of them are before or after the decimal point does not matter, you need to count both sides. So as long as you have <=10 digits before and <= 6 digits after the point, you'll be fine. – Bergi

However, as Eric Postpischil remarked in an answer to a related question, saying that a double or float format has "some number of decimal digits is sloppy terminology". Because double and float typically correspond to numbers in binary, not decimal (more specifically, IEEE 754 binary64 and binary32, respectively), and not all decimal numbers can be converted to binary numbers without loss.

Peter O.
  • 32,158
  • 14
  • 82
  • 96