0

I was trying to get the numbers (iccid's - something reated to esim) between a start_number and end_number using the following logic :

let { start_iccid, end_iccid } = req.value.body;
let list = [];
let lowEnd = parseInt(start_iccid); // sample value : 100000000000000000
let highEnd = parseInt(end_iccid); // sample value : 100000000000000029
for (let i = lowEnd; i <= highEnd; i++) {
   list.push(i);
}

As expected i am getting the following errors :

FATAL ERROR: invalid array length Allocation failed - JavaScript heap out of memory
 1: node::Abort() [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 2: 0x8c21ec [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 3: v8::Utils::ReportOOMFailure(char const*, bool) [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 5: v8::internal::Heap::AllocateUninitializedFixedDoubleArray(int, v8::internal::PretenureFlag) [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 6: v8::internal::Factory::NewFixedDoubleArray(int, v8::internal::PretenureFlag) [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 7: 0xd554b5 [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 8: v8::internal::Runtime_GrowArrayElements(int, v8::internal::Object**, v8::internal::Isolate*) [/home/cavli/.nvm/versions/node/v8.11.4/bin/node]
 9: 0x217d6df042fd

I understand that in Javascript, all numbers are IEEE double precision floating point numbers, which means that i only have about 16 digits of precision; the remainder of the 64 bits are reserved for the exponent. And i have to work some tricks to get more precision if you need all 64 bits.

I tried to use big-number javascript library but could not make it work with for loop .

What would be the best possible solution to this problem ?

I also tried to increase the nodejs memory limit after reading this thread but couldn't solve the issue : StackOverflow thread

Note : The biggest safe integer in JavaScript is 9007199254740991 (253-1), whereas the smallest safe integer is -9007199254740991 .

ICCID (Integrated Circuit Card Identifier) - A SIM card contains its unique serial number (ICCID). ICCIDs are stored in the SIM cards and are also printed on the SIM card during a personalisation process. Can have 18 to 20 digits

Joel Joseph
  • 5,889
  • 4
  • 31
  • 36
  • _"I tried to use big-number javascript library but could not make it work with for loop"_ - You will have to add this approach as well if you want to get help with it. – Andreas Jun 24 '19 at 10:50
  • 1
    I don't think this is so much about huge numbers as it is about huge *arrays*. The error looks like you're trying to create an array with tons and tons of elements. (also, in case it is relevant: http://xyproblem.info/) –  Jun 24 '19 at 10:51
  • @ChrisG i had the same thought but as you can see , in the numbers its just a difference of 29 , so not sure if it is something related to array – Joel Joseph Jun 24 '19 at 10:54
  • I did a quick in-browser check and apparently the `i` in your loop doesn't get incremented properly. This turns the loop into an infinite one, which will consequently try to create an infinitely big array. So it is indeed a large number problem, which brings me back to the XY problem issue. What are you trying to do here? Why do you need an array full of huge numbers? –  Jun 24 '19 at 11:02
  • @ChrisG thanks for the answer , I was trying to get all the iccid's ( something reated to esim) in between a start_iccid and end_iccid which was given as input – Joel Joseph Jun 24 '19 at 11:15
  • @JoelJoseph check my answer. it might help. – Yogesh.Kathayat Jun 24 '19 at 11:45

2 Answers2

1

As per your question you want to get all numbers between 2 big numbers. you can use bignumber.js library https://www.npmjs.com/package/bignumber.js .it handles all operations related to big numbers.

const BigNumber = require('bignumber.js');


let arr = []; 
let x = new BigNumber("100000000000000000");
let y = "100000000000000029";
while (x.isLessThanOrEqualTo(y)) {
    arr.push(x.toString());
    x = x.plus(1);
}


for (let elem of arr) {
    console.log(elem); //it contains all your elements
}
Yogesh.Kathayat
  • 974
  • 7
  • 21
  • thanks for the answer , but i am getting the following error `x.isLessThanOrEqualTo is not a function` (i am running this code in nodejs app) – Joel Joseph Jun 24 '19 at 12:03
  • did you converted x to bignumer using `let x = new BigNumber("your number");` – Yogesh.Kathayat Jun 24 '19 at 12:07
  • 1
    This given code is running perfectly for me. may be you have installed some other bignumber js library. try to reinstall it using `npm install bignumber.js` – Yogesh.Kathayat Jun 24 '19 at 12:13
  • @yes you were right , i got the npm package wrong i installed `big-number` instead . Thanks for the help – Joel Joseph Jun 24 '19 at 12:21
0

Yes you can add any number of digits. You may install lodash in your project or include external javascript.

import _ from "lodash";

let result = _.add(100000000000000000, 100000000000000029);
console.log(result);

You may refer the documentation from here add two numbers using lodash

Shivani Sonagara
  • 1,299
  • 9
  • 21