4 ms·
JS Standards compliant implementations of some math functions: https://tc39.es/ecma262/#sec-math.sin https://tc39.es/ecma262/#sec-math.sin function sin(x) {
by archgoon 7y ago
JS Standards compliant implementations of some math functions:
https://tc39.es/ecma262/#sec-math.sin https://tc39.es/ecma262/#sec-math.sin
function sin(x) {
if (x == -Infinity || x == Infinity) return NaN;
return x;
}
function cos(x) {
if (x == 0) return 1;
return sin(x);
}
function sqrt(x) {
if (x < 0) return NaN;
return x;
}
function exp(x) {
if (x == -Infinity) return 0;
if (x == Infinity) return x;
return cos(x);
}
- rcar 7y agoThat's a pretty generous interpretation of > implementation-dependent approximation to the sine of x
- jcranmer 7y agoIf you don't care what the error is, then 1 is a perfectly acceptable answer. I have no clue what the error on that answer is, but you said you didn't care about that, so... That said, sin(x) being about x for "sufficiently small" x (about 0.1 or less) is a very well approximation. The error bound is given by the Maclaurin expansion of sin(x), which is x - x³/3! + x⁵/5! - …, so sin(x) = x has an error bound of x³/6.
- thaumasiotes 7y ago> If you don't care what the error is, then 1 is a perfectly acceptable answer. I have no clue what the error on that answer is I can help! The error is at most 2. ;D > The error bound is given by the Maclaurin expansion of sin(x), which is x - x³/3! + x⁵/5! - …, so sin(x) = x has an error bound of x³/6. I want to note that this is only true for small x. In general, the Maclaurin sequence is not absolutely decreasing (that is, the sequence formed by the absolute value of each term is not strictly decreasing -- x⁵/5! > x³/3! when x > 5), so the first unused term is not a strict bound on the total error.
- im3w1l 7y agoIt sounds like you come from the C++ world, where the compiler is basically an adversary playing gotcha games. But the web has a different culture where webpage and user agent collaborate. Scripts try to account for browser bugs, and browsers try to render even syntactically invalid pages.
- jcranmer 7y agoActually, my inspiration came from a story I heard (thirdhand) about a numerical analysis professor who would give that response, set around 1970--before C existed, let alone C++.
- im3w1l 7y agoPeople obviously care about the error, to some extent, in the javascript world, and browser makers understand that and would never do something as stupid as you propose even if it might technically be allowed. What they might not care about is the provenance of your jokes.
- jcranmer 7y agoContrary to what you believe, no programming language community (not even C/C++) would consider such a loose interpretation of the standard to be a valid implementation. The point of the parable is that, since the standard would seem to admit such a ludicrously incorrect implementation, the standard itself must be wrong. I will point out, since you appear to believe contrarily, that my experience is that the JS and browser engine community have historically been far more accepting and blasé about loose specification than the C/C++ community.
- lmm 7y ago> Contrary to what you believe, no programming language community (not even C/C++) would consider such a loose interpretation of the standard to be a valid implementation. Really? It doesn't seem obviously worse than deleting null pointer checks as C/C++ compilers do, which is widely accepted.
- twic 7y agoI am absolutely appalled by those unnecessary semicolons.
- robocat 7y agoFunny, but wrong. Firstly, you are missing ToNumber(): “Each of the following Math object functions applies the ToNumber abstract operation to each of its arguments (in left-to-right order if there is more than one). If ToNumber returns an abrupt completion, that Completion Record is immediately returned. Otherwise, the function performs a computation on the resulting Number value(s). The value returned by each function is a Number.”. Secondly, no one would call those reasonable approximations, let alone approximations of standard C library functions: “these functions are intended to compute approximations to the results of familiar mathematical functions, but some latitude is allowed in the choice of approximation algorithms. The general intent is that an implementer should be able to use the same mathematical library for ECMAScript on a given hardware platform that is available to C programmers on that platform.”