However, there is no answer that finds/proves this programatically (other than the one CoolAJ86 alluded to in his answer that would finish in 28.56 years ;), so here's a slightly more efficient way to do that (to be precise, it's more efficient by about 28.559999999968312 years :), along with a test fiddle:
/*** Checks if adding/subtracting one to/from a number yields the correct result.** @param number The number to test* @return true if you can add/subtract 1, false otherwise.*/var canAddSubtractOneFromNumber = function(number) {var numMinusOne = number - 1;var numPlusOne = number + 1;
return ((number - numMinusOne) === 1) && ((number - numPlusOne) === -1);}
//Find the highest numbervar highestNumber = 3; //Start with an integer 1 or higher
//Get a number higher than the valid integer rangewhile (canAddSubtractOneFromNumber(highestNumber)) {highestNumber *= 2;}
//Find the lowest number you can't add/subtract 1 fromvar numToSubtract = highestNumber / 4;while (numToSubtract >= 1) {while (!canAddSubtractOneFromNumber(highestNumber - numToSubtract)) {highestNumber = highestNumber - numToSubtract;}
numToSubtract /= 2;}
//And there was much rejoicing. Yay.console.log('HighestNumber = ' + highestNumber);
input: 2251799813685246.75 output: 2251799813685246.8 // expected: 2251799813685246.75input: 2251799813685246.25 output: 2251799813685246.2 // expected: 2251799813685246.25input: 2251799813685246.5 output: 2251799813685246.5/**Please note that if you try this yourself and, say, logthese numbers to the console, they will get rounded. JavaScriptrounds if the number of digits exceed 17. The valueis internally held correctly:*/
input: 2251799813685246.25.toString(2)output: "111111111111111111111111111111111111111111111111110.01"input: 2251799813685246.75.toString(2)output: "111111111111111111111111111111111111111111111111110.11"input: 2251799813685246.78.toString(2)output: "111111111111111111111111111111111111111111111111110.11"