• Advertisement
Sign in to follow this  

Using cosine, sine, tangent etc. in C++

This topic is 1653 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi!

 

I don't need to go into detail of what I'm doing to get an answer on this question. All I'm wondering is what function in the cmath.h header that corresponds to cos-1, sin-1, tan-1 on a calculator.

For example: if I want to calculate cos-1(200/100) to get the degree of an angle, which function do I use since functions cos(), sin(), tan() in C++ corresponds to cos, sin, tan on a calculator and not cos-1, sin-1, tan-1.

 

Hopefully, that was not a load of incohesive dribble, but let me know if you want me to explain some more if it was :)

Share this post


Link to post
Share on other sites
Advertisement
It should also be noted that using "cmath.h" is in general inadvisable when working with C++. C++ has the "cmath" header. Not only does it respect namespaces, it also has several useful overloads for types other than just doubles.

Share this post


Link to post
Share on other sites

Seriously it took 5 seconds to do a Yahoo search  using keywords "c++ arc cosine"  to find this info.

 

Everyone doesn't study math in english, I didn't know that "cos-1" was "arc cosine" in english.

Edited by The Magical Pot

Share this post


Link to post
Share on other sites

 

Seriously it took 5 seconds to do a Yahoo search  using keywords "c++ arc cosine"  to find this info.

 

Everyone doesn't study math in english, I didn't know that "cos-1" was "arc cosine" in english.

 

 

No, you don't get to play the foreigner card this time: Google finds relevant information using "C++ inverse cosine", "C++ inverse cos", "C++ cos^-1" and even "C++ cos-1".

Share this post


Link to post
Share on other sites

Not to mention you're taught that in math class...

 

That said:


EDIT: Oh, and pi is 3.14159... which is M_PI I think in math.h / . You can also use

M_PI is not standard, so no, you can't assume it's there (indeed, in my system headers it's not present, at least not when I enable C99 mode). There's no option but to define pi manually.

 

Also huh, selective quote will eat anything between < and >...

Share this post


Link to post
Share on other sites

 

 

Seriously it took 5 seconds to do a Yahoo search  using keywords "c++ arc cosine"  to find this info.

 

Everyone doesn't study math in english, I didn't know that "cos-1" was "arc cosine" in english.

 

 

No, you don't get to play the foreigner card this time: Google finds relevant information using "C++ inverse cosine", "C++ inverse cos", "C++ cos^-1" and even "C++ cos-1".

 

You don't even need to add any of the inverse variants, a plain reference page for sin or cos will often refer to the inverse functions, or, since he mentioned the relevant header, list the functions in said header to look through.

Share this post


Link to post
Share on other sites

PEDANT ALERT:

 

The cosine function has no inverse! arccos(x) [a.k.a. acos(x), cos-1(x)] is the inverse of the restriction of the cosine function to a suitable range (in this case, [0, pi]). EDIT: You won't normally see an analysis textbook use cos-1 for that reason, they usually use arccos.

 

For a function to have an inverse it must be either strictly increasing or decreasing so there is a 1-1 mapping from the domain to the range.

 

Similarly sqrt(x) is not the inverse of f(x) = x2, since it only returns the positive square root.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

ULTRA-PEDANT ALERT:

 

There is something called the inverse cosine, which is a multivalued function. The C++ function std::acos returns the principal value of this multivalued function (in C++98 this is only defined for real values between -1 and 1, but in C++11 there is a version that works for any complex number).

 

Similarly the square root is a multivalued function that is the inverse of the function that maps x to x2, and std::sqrt returns the principal value.

Edited by Álvaro

Share this post


Link to post
Share on other sites

M_PI is not standard, so no, you can't assume it's there (indeed, in my system headers it's not present, at least not when I enable C99 mode). There's no option but to define pi manually.

Or to use a library that defines it. Ex: boost has a large number of mathematical constants defined.

Share this post


Link to post
Share on other sites

 

M_PI is not standard, so no, you can't assume it's there (indeed, in my system headers it's not present, at least not when I enable C99 mode). There's no option but to define pi manually.

Or to use a library that defines it. Ex: boost has a large number of mathematical constants defined.

 

 

 

As an aside, and certainly not a question for the beginners forum, but...

 

How are the boost math libraries in this situation?  Are they performant enough for real-time game usage?

 

Also, why the hell isn't performant a word?  It really should be.

Share this post


Link to post
Share on other sites

I asked a very simple question and I got the answer that I needed 5 minutes after posting. I know that you shouldn't ask stuff that you can easily find on google or whatever, but for some reason i did and someone was nice enough to answer my question. I'll gladly take delete this post if anyone says that I have to because I already got my answer.

This conversation is pointless.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement