Skip to main content
GameDev.net gamedev.net
🔒 Locked

arcsin and arccos in c++

Started by brassfish89 May 30, 2003 at 8:29 PM 9 replies 52.3k views
Original Post
brassfish89
brassfish89
does anyone know what the equavilent of the trig functions arcsin and arccos are in c++. i think it might be asin & acos in math.h but they dont seem to work. this is how i use them:
result=acos(number)*(PI/180);
multiplying the result by (PI/180) converts it to degrees. thx brassfish
doh, nuts. Mmmm... donuts My website
Thunder_Hawk
Thunder_Hawk
It should be 180/PI ... think about it for a second and then smack your head.

______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
Doc
Doc
Try:

#include <cmath>
...
radians = std::acos(number);


The "cmath" is technically the right way to do it in C++, but using math.h and without the "std::" bit would probably work aswell.

As for the radian to degree bit, the previous posters are right.


The following statement is true. The previous statement is false.
brassfish89
brassfish89
!!! i cant believe i did that!!! i converted right using sin and cos but for some reason i thought i should multiply by the recipricol using arcsin and arccos!
thx brassfish



doh, nuts. Mmmm... donuts
My website
brassfish89
brassfish89
its not working! i put in 1 and it game me 0.000304633. here''s my code:

#include <iostream>
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926535897932384626433832795

int main(int argc, char *argv[])
{
while(1)
{
int input=0;
double number=0.0f;
double answer=0.0f;
cout<<"What would you like to calculate?\n"
"1. sine\n"
"2. cosine\n"
"3. tangent\n"
"4. arcsine\n"
"5. arccosine\n"
"6. arctangent\n"
"7. square root\n"
"8. exit\n";
cin>>input;
if(input==8)
break;
cout<<"What number do you want to perform the operation on? ";
cin>>number;
bool radians=0;
if(input>0&&input<4)
{
cout<<"In radians (0-no,1-yes):";
cin>>radians;
}
if(!radians)
number*=(PI/180);
switch(input)
{
case 1:
answer=sin(number);
break;
case 2:
answer=cos(number);
break;
case 3:
answer=tan(number);
break;
case 4:
answer=asin(number)*(PI/180);
break;
case 5:
answer=acos(number)*(PI/180);
break;
case 6:
answer=atan(number)*(PI/180);
break;
case 7:
answer=sqrt(number);
break;
default:
system("cls");
continue;
}
cout<<"Answer: "<<answer<<endl;
cin.get();
cin.get();
system("cls");
}
cin.get();
return 0;
}





doh, nuts. Mmmm... donuts
My website
Zipster
Zipster
The catch is that you don't perform the degree-to-radian operation on the inputed value if it's an inverse trigometric function. The input for those functions are mere ratios.

[edited by - Zipster on May 30, 2003 12:49:02 AM]
Extrarius
Extrarius
    bool radians=0;
if(input>0&&input<4)
{
cout<<"In radians (0-no,1-yes):";
cin>>radians;
}
if(!radians)
number*=(PI/180);
will run the conversion every time for options >4. You shouldnt do that, so to fix it you need to change the first line above to "bool radians = true;" or put the second if statement inside the first.

[edited by - Extrarius on May 31, 2003 6:37:26 PM]
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
brassfish89
brassfish89
ok, i changed that bit of code that says:

if(!radians)
number*=(PI/180);

to:

if(!radians&&input>0&&input<4)
number*=(PI/180);

but it gives me 0.0274156 when i input 1.



doh, nuts. Mmmm... donuts
My website
Extrarius
Extrarius
arcsin(1)*PI/180 is ~0.027415567780804 so that answer is correct. You are probably expecting the answer to be 90, in which case the formula you want is arcsin(x)*180/PI as people have said already many times.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
brassfish89
brassfish89
ok, i changed it and it works fine now.
thx brassfish



doh, nuts. Mmmm... donuts
My website

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.