by jacobolus · 5y ago · 0 comments
tanpi_2 = function tanpi_2(x) {
var y = (1 - x*x);
return x * (((-0.000221184 * y + 0.0024971104) * y - 0.02301937096) * y
+ 0.3182994604 + 1.2732402998 / y);
}
(valid for -1 <= x <= 1)https://observablehq.com/@jrus/fasttan with error: https://www.desmos.com/calculator/hmncdd6fuj
But even better is to avoid trigonometry and angle measures as much as possible. Almost everything can be done better (faster, with fewer numerical problems) with vector methods; if you want a 1-float representation of an angle, use the stereographic projection:
stereo = (x, y) => y/(x + Math.hypot(x, y));
stereo_to_xy = (s) => {
var q = 1/(1 + s*s);
return !q ? [-1, 0] : [(1 - s*s)/q, 2*s/q]; }I was just learning about stereographic projection earlier. Isn't it odd how when you know something you notice it appearing in places.
Can you give an example of an operation that could be performed better using stereographic projection rather than angles?
The places where you might want to convert to a 1-number representation are when you have a lot of numbers you want to store or transmit somewhere. Using the stereographic projection (half-angle tangent) instead of angle measure works better with the floating point number format and uses only rational arithmetic instead of transcendental functions.