How to implement arctan(x) without using standard library?

Discussion in 'C++' started by Gerald, Dec 9, 2005.

  1. Gerald

    Gerald Guest

    Recently, my program need to be run in the embeded enviroment, and I
    cann't use
    the standard library. But I need to use arctan(x), so I implement it
    like the following:

    inline double pow(double x, size_t n) {
    if (n == 0)
    return 1;
    else if (n % 2 == 0)
    return pow(x * x, n >> 1);
    else
    return x * pow(x * x, n >> 1);
    }

    inline double atan_Gerald(double x) {
    register double temp1 = x >= 0 ? x : -x;
    register double temp2 = temp1 <= 1.0 ? temp1 : (temp1 - 1) / (temp1 +
    1);
    register double sum = temp2;

    for (register size_t i = 1; i != 6; ++i)
    sum += (i % 2 ? -1 : 1) * pow(temp2, (i << 1) + 1) / ((i << 1) + 1);

    if (temp1 > 1.0) sum += 0.785398;
    return x >= 0 ? sum : -sum;
    }

    But I found it doesn't work efficiently like atan in the standard
    library. Would you please
    help me to optimize my code or give me another suggestion?

    Thank you!

    Regards Gerald
     
    Gerald, Dec 9, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens

    arctan of complex number

    Marc Schellens, Nov 26, 2003, in forum: C++
    Replies:
    8
    Views:
    6,200
    Gianni Mariani
    Nov 27, 2003
  2. Shi Mu
    Replies:
    8
    Views:
    19,568
    Robert Kern
    Nov 8, 2005
  3. Simon Brunning
    Replies:
    0
    Views:
    421
    Simon Brunning
    Nov 8, 2005
  4. Gerald
    Replies:
    5
    Views:
    488
  5. Replies:
    3
    Views:
    2,491
Loading...

Share This Page