0

So, I know that some programming languages have maximum numbers (integers). I am trying to find a combat this with this problem. This is more of an algorithm based question and a storage question. So basically, I am trying to store information to a database. But, I am trying to find a way to test for the worst possible case (reach a number so large its no longer supported) and find a solution to it. So, assume I have a function called functionInTime that looks something like this:

functionInTime(){
    currenttime = getCurrentTime();
    foo(); // Random void function
    endtime = getCurrentTime();
    return (endtime - currenttime);
}

This function should essentially just check how long it takes to run the function foo. Additionally, assume there is a SUM function that looks similar to:

SUM(arrayOfNumbers){
    var S = 0;
    for(int i = 0; i < arrayOfNumbers.length; i++){
        S = S + arrayOfNumbers[i];
    }
    return S;
}

I also there is a function storeToDB, which looks something like this:

storeToDB(time){ // time is an INTEGER
    dbInstance = new DBINSTANCE();
    dbInstance.connectTo("wherever");
    timesRun = dbInstance.get("times_run"); // return INTEGER
    listOfTimes = dbInstance.get("times_list"); // return ARRAY
    dbInstance.set("times_run", timesRun+1);
    dbInstance.set("times_list", listOfTimes.addToArray(time));
    dbInstance.close();
}

However, this is where problems start to stand out to me in terms of efficiency and lead me to believe that this is a terrible algorithm. Lastly assume I have a function called computeAverage, that is simply:

computeAverage(){
    dbInstance = new DBINSTANCE();
    dbInstance.connectTo("wherever");
    timesRun = dbInstance.get("times_run"); // return INTEGER
    listOfTimes = dbInstance.get("times_list"); // return ARRAY
    return SUM(listOfTimes)/timesRun;
}

Since the end goal is to report the computed average each time, I think the above method should work. However, what I am trying to test is what happens if the value returned for the variable timesRun is so large that the language cannot support such a number. Parsing an array that long would take forever, but what if the size of an array is a number the language doesn't support? Removing or resetting the variables timesRun or listOfTimes would distort the data and throw off any other measurements that use this.

::: {"times_run": N} // N = number not supported by language
functionTime();
printThisOut("The overage average is "+computeAverage()+" ms per run...");
::: ERROR (times_run is not a supported number)

How can I infinitely add data in a way that is both efficient and is resistant to a maximum number overflow?

  • "what if the size of an array is a number the language doesn't support" - what do you mean by this? Every integer from 0 to the maximum possible integer (they allow) is supported in every language I know of. To deal with a number that is too large (or negative) (which would presumably be input as a string or something along those lines) you just put a check for that somewhere. – Bernhard Barker Apr 20 '17 at 16:47
  • So I think in JavaScript, the largest possible integer is N = (2^53)-1. If the user tries to increment it, I believe it changes to a negative number. I was asking about what happens when you try to add another element to an array of size N. –  Apr 20 '17 at 16:59
  • If you want to cater for having more than [4 billion](http://stackoverflow.com/questions/6154989/maximum-size-of-an-array-in-javascript) elements in an array (which takes like 16 gig of memory if you're storing 4-byte ints, by the way), you can presumably just check the size before inserting. If you're wondering what actually happens (in JavaScript) if you try to insert it, you should probably rewrite your question to more clearly focus on that, and replace all your tags with just the JavaScript tag. My guess is it will just give you an error of some sort. – Bernhard Barker Apr 20 '17 at 17:12
  • Javascript uses double precision floating point for all numbers, so the "largest possible integer" is really quite large indeed. However, there is a smallest positive unrepresentable integer, which is Math.pow(2,53) + 1. If you try to add one to Math.pow(2,53), you will get the same Math.pow(2,53), but adding 2 (or 1.1) will give you Math.pow(2,53) + 2. The maximum size of an array is limited by available memory or Math.pow(2,32), whichever gets exceeded first. – rici Apr 20 '17 at 17:26

0 Answers0