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

G

Gerald

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top