Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westc/d8949dded5bf21658e2ffaf62350469c to your computer and use it in GitHub Desktop.
Save westc/d8949dded5bf21658e2ffaf62350469c to your computer and use it in GitHub Desktop.
Calculates the minimum and the maximum safe integer values which can vary depending on the environment where the code is run. This can be used to have constants similar to JavaScript's Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER.
<?php
// Similar to JavaScript's Number.MAX_SAFE_INTEGER
define("MAX_SAFE_INTEGER", (function() {
for ($e = 1; pow(2, $e) - ($MAX_SAFE_INTEGER = (pow(2, $e - 1) - 1) * 2 + 1) == 1; $e++) {
if ($e > 999) {
throw new Exception("Maximum integer couldn't be found.");
}
}
return $MAX_SAFE_INTEGER;
})());
// Similar to JavaScript's Number.MIN_SAFE_INTEGER
define("MIN_SAFE_INTEGER", -MAX_SAFE_INTEGER - 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment