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?