0

This excellent, highly recommended javascript tutorial, by Ilya Kantor, shares the following:

// Hello! I'm a self-increasing number!
console.log( 9999999999999999 ); // shows 10000000000000000

I don't get why 9,999,999,999,999,999‬ is turned into 10,000,000,000,000,000. If we have 53 bits to store a number (out of the 64), given that representing the number above requires more bits than available (precisely 54 ?), why not return all the 53 bits we have oned, meaning we are representing the maximum number we possibly can (9,007,199,254,740,991) ?

The post here mentions 52 bits to store a number out of the 64, but I changed it to 53 after checking Number.MAX_SAFE_INTEGER and putting it into Window's calculator in programming mode - it gives 53 bits oned.

enter image description here

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
Veverke
  • 9,208
  • 4
  • 51
  • 95
  • because `10,000,000,000,000,000` is closer to `9,999,999,999,999,99`? I mean, why you want more error (i.e. `9,007,199,254,740,991`)? – apple apple Mar 31 '20 at 14:33
  • 1
    That's not how the IEEE754 algorithm work. A number is not supposed to be rounded to the max integer. It is supposed to be rounded to the nearest representable number. – slebetman Mar 31 '20 at 14:34
  • btw, you can use bigint to store that like `999999999999999n` – apple apple Mar 31 '20 at 14:35
  • @slebetman: although the question was marked as duplicate (was indeed asked, but with an additional "flavor" missing in the duplicates: why not show the MAX_SAFE_INTEGER - which you answer. Thanks – Veverke Mar 31 '20 at 14:37
  • @appleapple: if that is a sum of money, you certainly do not want a number greater than the real one in question. – Veverke Mar 31 '20 at 14:40
  • The "already has answers here" answers are *excellent.* Go now and read them. – Mike Robinson Mar 31 '20 at 14:49
  • @Veverke well, if that is a sum of money, you certainly do not want a number *smaller* than what you should have – apple apple Mar 31 '20 at 15:02

0 Answers0