How can I calculate arc tangent (atan) without cmath?

Page 1 of 1

3 Replies - 1785 Views - Last Post: 26 September 2011 - 08:57 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=248604&amp;s=f2732bd8a048c95ec11597b34745149b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 hulla

• Writing Lines

Reputation: 49
• Posts: 733
• Joined: 05-March 11

How can I calculate arc tangent (atan) without cmath?

Posted 26 September 2011 - 04:03 AM

I would like to know how to calculate arc tangent in C++ without any cmath functions. Can you teach me how? I haven't done trigonometry before and cannot seem to understand anything people say about it.
Here is my current code . . .
```#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout.precision(12);
cout << "Computing the value of PI . . ." << endl;
long double pi;
pi = 4*(4*(/*arctan(1/5)*/)-(/*arctan(1/239)*/))
cout << pi << endl;
return 0;
}

```

Thank you and pat yourself on your back for reading this topic.

This post has been edited by hulla: 26 September 2011 - 04:05 AM

Is This A Good Question/Topic? 0

Replies To: How can I calculate arc tangent (atan) without cmath?

#2 nuclearfroggy

Reputation: 82
• Posts: 167
• Joined: 04-August 08

Re: How can I calculate arc tangent (atan) without cmath?

Posted 26 September 2011 - 05:28 AM

One method for calculating trigonometric functions is called the Taylor Series - I don't really understand how it works, but you basically keep adding terms and you get closer and closer to the true value. Arctan follows the rule x - x^3 / 3 + x^5 / 5 - x^7 / 7 + ... for arctan(x) and basically can go on forever, although you would stop when you've got the precision you want.

Not really sure how much this helps. Considering you're only using a couple of arctan values, wouldn't it be easier just to calculate them before and use them as constants?

#3 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

Re: How can I calculate arc tangent (atan) without cmath?

Posted 26 September 2011 - 08:41 AM

For a start, all trigonometry implementations, even those found in <cmath> are merely approximations. As pointed out above, the only way to calculate their value is through various infinite series, and the precision stops at some point.

Note that the <cmath> atan function (in most implementations) actually uses a precompiled table of values. This is much, much faster than any accurate approximating function is. Because trigonometry is often used in performance-critical graphics programming, these functions must be as fast as they possibly can be.

#4 hulla

• Writing Lines

Reputation: 49
• Posts: 733
• Joined: 05-March 11

Re: How can I calculate arc tangent (atan) without cmath?

Posted 26 September 2011 - 08:57 AM

Thanks for the suggestions. I need to sleep now so I'll try the Taylor Series tomorrow.