Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fast approx. atan2 vs Math.atan2 vs fast accurate atan2
(version: 1)
Comparing performance of:
Math.atan2 vs fast approx. atan2 vs accurate approx atan2
Created:
6 months ago
by:
Guest
Jump to the latest result
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));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.atan2
fast approx. atan2
accurate approx atan2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
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/OS:
Safari 26 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
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
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 6 months ago):
This benchmark compares three different approaches to calculating the angle (in radians) from the x and y coordinates to the origin in a Cartesian coordinate system. The methods being tested are: 1. **Math.atan2**: This is a built-in JavaScript function that returns the arctangent of the quotient of its two arguments (y, x), which represents the angle relative to the positive x-axis. 2. **fastAtan2**: This is a custom implementation that approximates the behavior of Math.atan2 but is designed to be faster. It uses a series of mathematical transformations and conditions to calculate the angle based on the signs and magnitudes of x and y. 3. **accurateFastAtan2**: This method also approximates atan2 but aims for increased precision. It adopts a different approach by first determining if the input coordinates (x, y) require a swap based on their absolute values. It then performs polynomial approximation using specific coefficients to refine the output. ### Pros and Cons of Each Approach #### 1. Math.atan2 - **Pros**: - Built-in and thus well-optimized for general use across all JavaScript environments. - Provides accurate results across all quadrants. - **Cons**: - May be slower compared to the custom implementations because it likely includes additional checks and handling inside the native implementation. #### 2. fastAtan2 - **Pros**: - Aims for performance, making it faster than Math.atan2 for many inputs due to a reduced number of operations. - Useful in performance-critical applications, such as gaming or real-time graphics, where the exact angle isn't as crucial. - **Cons**: - Adequate accuracy but might fall short in edge cases or extreme values, particularly when inputs are very close to zero or far apart. #### 3. accurateFastAtan2 - **Pros**: - Strikes a balance between speed and accuracy. It generally yields results that are closer to the actual arctangent values compared to fastAtan2. - The polynomial approximation allows for a reasonable accuracy even for values that might challenge simpler algorithms. - **Cons**: - Slightly slower than fastAtan2 due to the overhead of additional mathematical operations and checks. ### Other Considerations - The benchmark utilizes `eval('')` to create a scoped environment for the performance testing, which allows the use of the custom functions without external interference. - The benchmarking results indicate the number of executions per second, providing a quantitative comparison of how each method performs in the tested environment. ### Alternative Approaches - **Precision Libraries**: Libraries such as math.js or numeric.js can be used for more complex mathematical operations, offering enhanced reliability and additional features. - **WebAssembly**: For applications where extreme performance and speed are required, one might consider implementing atan2 calculations in a language like C or Rust, compiling to WebAssembly (Wasm) for better execution speed. - **Optimized Algorithms**: Exploring other mathematical optimizations or approximation techniques, such as Cordic algorithms, which can provide faster convergence for certain types of trigonometric calculations. ### Summary The benchmark presents a clear comparison of three approaches to calculating atan2, highlighting the trade-off between performance and precision. Each method has its target use case, and the choice depends on the requirements of the application—whether speed or accuracy is the priority.
Related benchmarks:
trigata
trigata
trigata
trigata
Fast approx. atan2 vs Math.atan2
Fast approx. atan2 vs Math.atan2 vs cached atan2
Fast approx. atan2 vs Math.atan2 vs cached atan2 (v8 optimization buster + local scope)
Fast atan2 vs Math.atan2
Fast approx. atan2 vs Math.atan2 vs cached atan2 (v8 optimization buster + local scope) 2
Comments
Confirm delete:
Do you really want to delete benchmark?