There's no way to modify the precision of a floating point type: they are fixed by hardware (and typically language standards). In a different language, you might be able to use a lower-precision type (e.g., in C you could use a float
instead of a double
), but that's not possible in JavaScript, as all numbers are doubles.
What you could do is use a faster approximation to the sine function. Typically, a sin/cos function consists of two steps:
- a "range-reduction" to find an integer n and float y on the interval [-π/4,π/4] such that x=n*π/2+y. This can't be performed using a standard mod (
%
) operation, as π can't be represented exactly by a floating point number.
- a "kernel" function to compute the appropriate sin/cos, typically using a polynomial approximation.
(you can see the V8 source here).
So there are couple of things you can do:
Use arguments that are already reduced to the appropriate interval, or alternatively, use an approximate reduction (i.e. y = x % Math.PI/2
)
Use a faster (and less accurate) approximation to sin/cos: there's lots of stuff about this on the web, e.g. here.