473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

Recently, my program need to be run in embeded enviroment, and I cann't
use 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 standard library,
Would you please
help me to optimize my code or give me another suggestion?

Thank you!

Regards Gerald

Dec 9 '05 #1
5 16866
I made a mistake: the 'register' before double is useless, I forget to
delete it.

Dec 9 '05 #2
Gerald wrote:
Recently, my program need to be run in embeded enviroment, and I cann't
use standard library.
I don't think that this is a reasonable argument for not using the
standard library! In particular, I know that there is at least one
standard library targeted also at embedded systems: the Dinkumware
library is available in a version tailored to embedded systems
(although I personally don't see any justification at all in EC++...).
See <http://www.dinkumware.com/>. The prices are definitely in a
range where it comes much cheaper to buy the library than provide a
quality implementation of even a single trigonometric function.
But I need to use arctan(x), so I implement it
like the following:


I haven't implemented these functions myself and thus I might be
entirely wrong about how I think these functions are implemented.
Still, my understanding is that they are implemented very different
from how you tried to implement them. The basic approach is something
like this:

1. Translate the function argument(s) to a minimal basic area (this
is almost certainly not the correct term but I don't know the
correct term). My maths is rather rusty but I think for arctan(x)
the basic area is the range [0, 1]: all other values can be
derived from an appropriate argument:
- x < 0: arctan(x) = -arctan(-x)
- 1 < x: arctan(1/x) = pi/2 - arctan(x)
(actually, you already do this part...)
2. For the values in basic area you look up the subrange your value
is located in. Each subrange is associated with an approximation
function which is just a suitably parameterized polynomial of a
relatively small degree which can be evaluated reasonably fast.
The real tricky thing is to determine suitable subranges and the
parameters for their approximating polynomials. I have no idea
at all how this done.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Dec 9 '05 #3

Dietmar Kuehl wrote:
I haven't implemented these functions myself and thus I might be
entirely wrong about how I think these functions are implemented.
Still, my understanding is that they are implemented very different
from how you tried to implement them. The basic approach is something
like this:

1. Translate the function argument(s) to a minimal basic area (this
is almost certainly not the correct term but I don't know the
correct term). My maths is rather rusty but I think for arctan(x)
the basic area is the range [0, 1]: all other values can be
derived from an appropriate argument:
- x < 0: arctan(x) = -arctan(-x)
- 1 < x: arctan(1/x) = pi/2 - arctan(x)
(actually, you already do this part...)
2. For the values in basic area you look up the subrange your value
is located in. Each subrange is associated with an approximation
function which is just a suitably parameterized polynomial of a
relatively small degree which can be evaluated reasonably fast.
The real tricky thing is to determine suitable subranges and the
parameters for their approximating polynomials. I have no idea
at all how this done.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence


For good accuracy use cubic splines. For optimal speed use a bigger
caching table and linear progression between the points. (A cubic
spline has 4 parameters in each range so using the linear version you
can store in theory 5 times as many points).

Dec 9 '05 #4
Of course you don't have to store the cubic spline parameters as they
can be worked out
from the cached value, but that would give you even slower performance.

Dec 9 '05 #5
Dietmar Kuehl wrote:
Gerald wrote:
Recently, my program need to be run in embeded enviroment, and I cann't
use standard library.
But I need to use arctan(x) The basic approach is something like this:

1. Translate the function argument(s) to a minimal basic area (this
is almost certainly not the correct term but I don't know the
correct term). My maths is rather rusty but I think for arctan(x)
the basic area is the range [0, 1]: all other values can be
derived from an appropriate argument:
- x < 0: arctan(x) = -arctan(-x)
- 1 < x: arctan(1/x) = pi/2 - arctan(x)
(actually, you already do this part...)


There are two kinds of reductions. The first one is to keep the tables
finite
(This is essential for e.g. sin(x), as x may be any value) and the
second is
just to reduce the table size. The latter is something to worry about
later.
(It's a classic time/space tradeoff you'd need to profile).
2. For the values in basic area you look up the subrange your value
is located in. Each subrange is associated with an approximation
function which is just a suitably parameterized polynomial of a
relatively small degree which can be evaluated reasonably fast.
The real tricky thing is to determine suitable subranges and the
parameters for their approximating polynomials. I have no idea
at all how this done.


Polynomials are very efficient in finding the first few digits, but may
be terrible
in finding the next. E.g. when you already have arctan(x)=0.23....
you'd need
to look in table 23 to get the next coefficients, which implies 100
tables.
That's nog good for embedded systems. (Of course, real systems probably
use nibbles not digits, so you'd need 256 tables after the first two
nibbles)

However, it seems a real arctan algorithm is a lot more complex:
http://www.electronics.dit.ie/staff/...arch/ecs99.pdf

I wouldn't invent that myselves. Implementing the basic CORDIC seems
easy, though.

HTH,
Michiel Salters

Dec 9 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Shi Mu | last post by:
any python module to calculate sin, cos, arctan?
15
by: puzzlecracker | last post by:
does anyone know how to implement this function efficiently?
3
by: Ohad Young | last post by:
Hi, I have an interface with an event. I'd like to explicitly implement the interface by a certain class. However, I received the following error: "An explicit interface implementation of an...
3
by: Sean | last post by:
Hi all, I wonder what are the possible ways to implement a Map in C++? Among these methods, which is the best one and why? Thank you very much in advance. Sean
0
by: Gerald | last post by:
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...
4
by: astri | last post by:
i`m doing thesis about comparing calculation of arctan by polynomial and CORDIC. I`ve read a lot of journal and books about CORDIC and this is what i understand. 1. make x and y 2. make...
12
by: astri | last post by:
i`m doing my thesis comparing CORDIC with polynomial in counting arctan with fixed point. I`m using Q15 format now. I`m using this site CORDIC arctan as a referenced when making with floating...
21
by: Owen Zhang | last post by:
What is the best way to implement "tail -f" in C or C++ and higher performance compared to either unix shell command "tail -f" or perl File::Tail ? Any suggestion appreciated. Thanks.
3
by: Payne | last post by:
Hello, I'm having trouble figuring out how to best explain my problem but I hope I can make myself clear enough. Anyway, I'm doing an assignment for school and in this one we're supposed to write a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.