2

If you create the following function, and execute, you will get a return of: 11111111111111112. If you do much more, you will get exponential numbers. I just want to take long strings of numbers and place them into an array (the follow-up would be a split on the function). Is there anyway around this weirdness?

function sum2one(num) {
  var numtostring = num.toString();
  console.log(numtostring);
}

sum2one(11111111111111111);
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
Ryan Turner
  • 99
  • 1
  • 6
  • 1
    blame the IEEE, and just store strings... – dandavis Jan 23 '17 at 20:54
  • I think this is just a matter of the nature of the one Number type in javascript? For really big numbers I think scientific notation is probably a better way to go about it? @dandavis, are you sure storing strings as opposed to an actual number is a safe way to go (what happens if you need to perform arithmetic operations with those stored strings)? – Tim Consolazio Jan 23 '17 at 20:55
  • http://stackoverflow.com/questions/34299407/javascript-floating-point-precision-issue – Stephen Thomas Jan 23 '17 at 20:55
  • or check a big number implementation (it might be slow, but you will be able to perform calculation on them and get consistent string representation) – Balázs Édes Jan 23 '17 at 20:56
  • [Related](http://stackoverflow.com/questions/588004/is-floating-point-math-broken), though at the other end of the spectrum. – Ilja Everilä Jan 23 '17 at 21:01
  • It's not the `toString` function that does anything weird. It's the parser that converts your literal to a representable number. Simply try `console.log(11111111111111111)` or `console.log(11111111111111111 + 0)`. – Bergi Jan 23 '17 at 21:07
  • Thank you, all. Makes sense. – Ryan Turner Jan 23 '17 at 21:08
  • _"I just want to take long strings of numbers and place them into an array"_ How are the numbers generated? – guest271314 Jan 23 '17 at 21:11
  • http://stackoverflow.com/questions/40867648/why-does-this-number-get-increased-by-one – Josh Lee Jan 23 '17 at 22:17
  • See also [Converting large numbers from binary to decimal and back in JavaScript](http://stackoverflow.com/q/39334494/) – guest271314 Jan 23 '17 at 22:46

3 Answers3

1

Javascript numbers are always 64-bit floating point numbers. That is why. That basically leads to the fact that for every number there is no exact integer representation.

So, in fact, toString() doesn't do the weird things. It is the actual format how the numbers are stored/represented/implemented in the javascript language.

What!? Is floating point math broken?! Not really, see Is floating point math broken?

Community
  • 1
  • 1
teroi
  • 1,087
  • 10
  • 19
0

Javascript uses 64-bit floating point numbers.
I don't think it's possible to store precise numbers in javascript.

There are alternatives, bigint libraries can help. But you should just store as a string.

jdmdevdotnet
  • 1
  • 2
  • 19
  • 50
0

I just want to take long strings of numbers and place them into an array

You can use Array.prototype.fill().

function sum2one(num, len, from, to, arr) {
  return  (arr 
          ? len > arr.length 
            ? (arr = [...arr, ...Array(len - arr.length)]) 
            : arr 
          : Array(len))
          .fill(num, from || 0, to || len)
}

let arr = sum2one(1, 17);

console.log(arr);

// set index 13

arr[13] = 2;

console.log(arr);

// fill indexes 14 through 21

arr = sum2one(3, 21, 14, 21, arr);

console.log(arr);
guest271314
  • 1
  • 15
  • 104
  • 177