User Tools

Site Tools

blog:2024-02-21_fast_accurate_atan_arctan_approximation_algorithm



2024-02-21 Fast & accurate atan/arctan approximation algorithm

  • The discussion posts for fast & accurate atan/arctan approximation algorithm
  • Perhaps a more accurate a*x^5 + b*x^3 + c*x will still be fast enough for OP. It is about 4x more accurate on my machine on average and 2x better worst case. It uses an optimal 3-term polynomial as suggested by @Foon and @Matthew Pope
  • #include <math.h>
    #include <stdio.h>
    
    #ifndef M_PI_4
    #define M_PI_4 (3.1415926535897932384626433832795/4.0)
    #endif
    
    double FastArcTan(double x) {
      return M_PI_4*x - x*(fabs(x) - 1)*(0.2447 + 0.0663*fabs(x));
    }
    
    #define A 0.0776509570923569
    #define B -0.287434475393028
    #define C (M_PI_4 - A - B)
    #define FMT "% 16.8f"
    
    double Fast2ArcTan(double x) {
      double xx = x * x;
      return ((A*xx + B)*xx + C)*x;
    }
    
    int main() {
      double mxe1 = 0, mxe2 = 0;
      double err1 = 0, err2 = 0;
      int n = 100;
      for (int i=-n;i<=n; i++) {
        double x = 1.0*i/n;
        double y = atan(x);
        double y_fast1 = FastArcTan(x);
        double y_fast2 = Fast2ArcTan(x);
        printf("%3d x:% .3f y:" FMT "y1:" FMT "y2:" FMT "\n", i, x, y, y_fast1, y_fast2);
        if (fabs(y_fast1 - y) > mxe1 ) mxe1  = fabs(y_fast1 - y);
        if (fabs(y_fast2 - y) > mxe2 ) mxe2  = fabs(y_fast2 - y);
        err1 += (y_fast1 - y)*(y_fast1 - y);
        err2 += (y_fast2 - y)*(y_fast2 - y);
      }
      printf("max error1: " FMT "sum sq1:" FMT "\n", mxe1, err1);
      printf("max error2: " FMT "sum sq2:" FMT "\n", mxe2, err2);
    }
  • Output
  •  ...
     96 x: 0.960 y:      0.76499283y1:      0.76582280y2:      0.76438526
     97 x: 0.970 y:      0.77017091y1:      0.77082844y2:      0.76967407
     98 x: 0.980 y:      0.77529750y1:      0.77575981y2:      0.77493733
     99 x: 0.990 y:      0.78037308y1:      0.78061652y2:      0.78017777
    100 x: 1.000 y:      0.78539816y1:      0.78539816y2:      0.78539816
    max error1:       0.00150847sum sq1:      0.00023062
    max error2:       0.00084283sum sq2:      0.00004826

TAGS

  • 52 person(s) visited this page until now.

Permalink blog/2024-02-21_fast_accurate_atan_arctan_approximation_algorithm.txt · Last modified: 2024/02/21 17:21 by jethro

oeffentlich