Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Fast approx. atan2 vs Math.atan2 vs fast accurate atan2
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0.1 Safari/605.1.15
Browser:
Safari 26
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
6 months ago
Test name
Executions per second
Math.atan2
13583317.0 Ops/sec
fast approx. atan2
33551266.0 Ops/sec
accurate approx atan2
20342814.0 Ops/sec
Script Preparation code:
const ONEQTR_PI = Math.PI / 4; const THRQTR_PI = (3 * Math.PI) / 4; const HALF_PI = Math.PI / 2; // https://gist.github.com/volkansalma/2972237#file-atan2_approximation-c-L29 const fastAtan2 = (y, x) => { const abs_y = Math.abs(y) + 1e-10; // kludge to prevent 0/0 condition let angle, r; if (x < 0) { r = (x + abs_y) / (abs_y - x); angle = THRQTR_PI; } else { r = (x - abs_y) / (x + abs_y); angle = ONEQTR_PI; } angle += (0.1963 * r * r - 0.9817) * r; return y < 0 ? -angle : angle; }; // https://mazzo.li/posts/vectorized-atan2.html const accurateFastAtan2 = (y, x) => { const swap = Math.abs(x) < Math.abs(y); const denominator = (swap ? y : x) === 0 ? 0.00000001 : swap ? y : x; const atan_input = (swap ? x : y) / denominator; const a1 = 0.99997726; const a3 = -0.33262347; const a5 = 0.19354346; const a7 = -0.11643287; const a9 = 0.05265332; const a11 = -0.0117212; const z_sq = atan_input * atan_input; let res = atan_input * (a1 + z_sq * (a3 + z_sq * (a5 + z_sq * (a7 + z_sq * (a9 + z_sq * a11))))); if (swap) res = Math.sign(atan_input) * HALF_PI - res; if (x < 0.0) res = Math.sign(y) * MATH.PI + res; return res; };
Tests:
Math.atan2
eval(''); var k = [1, .23, 0.12, 929, 8172, 9.2, 21.2].map(e => Math.atan2(e, .5));
fast approx. atan2
eval(''); var k = [1, .23, 0.12, 929, 8172, 9.2, 21.2].map(e => fastAtan2(e, .5));
accurate approx atan2
eval(''); var k = [1, .23, 0.12, 929, 8172, 9.2, 21.2].map(e => accurateFastAtan2(e, .5));