-3

I'm new with JS, and i'm learning by video lectures. In one of the video, it was said that: "

numbers are always stored in a binary format"

and this is the reason that is very hard to represent some fraction that are very easy to represent in base 10 system we are used to.. I know some development languages (c,java,etc), Is there a difference between the way that numbers are stored in JS to the way it stored in java for example?

Charlie Armstrong
  • 2,332
  • 3
  • 13
  • 25
Eitanos30
  • 1,331
  • 11
  • 19
  • 2
    No, no difference, basically everything is stored in binary. – luk2302 Dec 30 '20 at 18:32
  • numbers in JS are IEEE754-2019 float64. That's it. Nothing more, nothing less. There is also `BigInt`, but that's not the default "number" – ASDFGerte Dec 30 '20 at 18:37
  • I will be happy to understand on what people unvote it... – Eitanos30 Dec 30 '20 at 18:46
  • I didn't downvote, but it's borderline for me aswell. There is no indication, that you did any research at all, for a topic, that has been discussed probably millions of times. If you had a better question/answer ratio, i could perhaps just assume, that you did effort, but as-is, i can't. You also tagged `java`. While you mention that language somewhere, it has very little to do with the question. If i came from the `java` tag, that would be a default-downvote from me already. Generally, this kind of mistagging again indicates "no effort was done". – ASDFGerte Dec 30 '20 at 18:58
  • @ASDFGerte, i have mentioned Java since in the video they saying other language. Java is a reference for me. If it is mistake to tag java i will remove it – Eitanos30 Dec 30 '20 at 19:13

1 Answers1

1

The way numbers are stored in JS is not different to other programming languages. The fact that all numbers are stored in base 2 (aka binary) is the reason why certain fractions are hard to store in JS (and other languages). For example, the base 10 number 0.1 is 0.000110011... in binary where 0011 is recurring indefinitely. Since your computer only has a finite number if bits, you can't store 0.1 exactly, you can only approximate it, hence the unprecision.

If you need precision for numbers, most programming languages have data types to store numbers accurately. In javascript you can use the BigInteger type to store numbers more accurately. The default number type used in JS, float, however lacks the precision and runs into the issues described above.

chingucoding
  • 894
  • 7
  • 17
  • Why 0.1 in base 10 is not 0.1? – Eitanos30 Dec 30 '20 at 18:45
  • 1
    @Eitanos30 I restructured the sentence... – luk2302 Dec 30 '20 at 18:53
  • there are number representations which can represent decimal numbers accurately, e.g. BigInts in JS. "other languages" also have different representations than just floating point. – Jonas Wilms Dec 30 '20 at 18:55
  • Thank you @JonasWilms, I added a few words regarding that :) – chingucoding Dec 30 '20 at 18:59
  • @Eitanos30 This is because of the same reason why 10 base 10 is not the same as 10 base 2. `0.1` base 10 is 1 * 10^-1, `0.1` in base 2 (binary) is 1 * 2^-1 though, that is why their value is different. After all, what would be 0.2 in base 10 be in base 2 given that there is no 2 in base 2? – chingucoding Dec 30 '20 at 19:00