Skip to content

Instantly share code, notes, and snippets.

@joa
Last active August 29, 2015 14:17
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 joa/23e8b6e11c1f06c041b8 to your computer and use it in GitHub Desktop.
Save joa/23e8b6e11c1f06c041b8 to your computer and use it in GitHub Desktop.
// http://jsperf.com/double2int
function double2int=function(x){
if(isNaN(x))
return 0;
if(x==Infinity||x>0x7fffffff)
return 0x7fffffff;
if(x==-Infinity||x<-0x80000000)
return -0x80000000;
return x|0;
};
function double2int=function(x){
if(x!==x)
return 0;
if(x>0x7fffffff)
return 0x7fffffff;
if(x<-0x80000000)
return -0x80000000;
return x|0;
};
function double2int=function(x) {
return Math.min(Math.max(x, -0x80000000), 0x7fffffff)|0
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment