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
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