3 Replies - 1105 Views - Last Post: 26 September 2011 - 08:57 AM Rate Topic: -----

#1 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • 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?
Was This Post Helpful? 2
  • +
  • -

#3 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • 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.
Was This Post Helpful? 3
  • +
  • -

#4 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1