Created
June 28, 2017 08:47
-
-
Save Bradshaw/d3d9349d097692cfd55161e5d571b499 to your computer and use it in GitHub Desktop.
Better version of Math.sin for javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var old_sin = Math.sin; | |
Math.sin = function(x) { | |
if (typeof x == "number" && isFinite(x)){ | |
return old_sin(x); | |
} | |
var str = "" + x; | |
if (str.length>0){ | |
str = str.repeat(Math.ceil(Math.max(1,18/str.length))); | |
} | |
var sinstr = "|\n"; | |
for (var line = 0; line<4; line++){ | |
sinstr+= "| "; | |
for (var pos=0; pos < str.length; pos++){ | |
var y = old_sin((pos/4+Math.PI-0.3)*Math.PI); | |
y = Math.floor((y+1)*2); | |
if (line==y){ | |
sinstr+=str[pos]; | |
} else { | |
sinstr+=" "; | |
} | |
} | |
sinstr+="\n"; | |
} | |
sinstr+="|\n"; | |
sinstr+="+"+("-").repeat(str.length+4); | |
return sinstr; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function test_sin (str, val) { | |
console.log("Math.sin("+str+")"); | |
console.log(Math.sin(val)); | |
console.log(""); | |
} | |
test_sin("Math.PI",Math.PI); | |
// Math.sin(Math.PI) | |
// 1.2246467991473532e-16 | |
test_sin("0",0); | |
// Math.sin(0) | |
// 0 | |
test_sin("15",15); | |
// Math.sin(15) | |
// 0.6502878401571168 | |
var x = undefined; | |
console.log("var x = undefined"); | |
test_sin("x",x); | |
// var x = undefined | |
// Math.sin(x) | |
// | | |
// | de nd | |
// | n f u e d | |
// | u i d f e | |
// | ne in | |
// | | |
// +---------------------- | |
test_sin("1/0",1/0); | |
// Math.sin(1/0) | |
// | | |
// | fi fi fi | |
// | n n n n n n | |
// | I i I i I i | |
// | ty ty ty | |
// | | |
// +---------------------------- | |
test_sin("NaN",NaN); | |
// Math.sin(NaN) | |
// | | |
// | NN aN | |
// | a a N N N | |
// | N N N a a | |
// | Na NN | |
// | | |
// +---------------------- | |
test_sin("\"Hello, World! \"","Hello, World! "); | |
// Math.sin("Hello, World! ") | |
// | | |
// | ll ld o, ! | |
// | e o r ! l d | |
// | H , o l W l | |
// | W He or | |
// | | |
// +-------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment