|
1 Dec 2002, 22:10
|
#1
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
C/C++: sine/cosine in degrees
Is there a function with which I can calculate a cos/sin in degrees instead of radians?
If not, can anyone tell me in which header file PI is defined... it doesn't seem to be in <math.h>
TY
__________________
"Yay"
|
|
|
1 Dec 2002, 22:33
|
#2
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
can't you just use the deg2rad and rad2deg functions?
not 100% if they are in any of the headers, i seem to remember having to write them myself when learning what functions did but they're very simple anyway
|
|
|
1 Dec 2002, 22:36
|
#3
|
Registered User
Join Date: Oct 2000
Posts: 4,944
|
oh and i don't think Pi is defined in any of the standard headers, we always had to define it at the start
|
|
|
1 Dec 2002, 23:06
|
#4
|
beware of the winky
Join Date: Feb 2002
Posts: 17
|
#define PI 3.14159.......
int degrees = 30;
sin((degrees * PI)/180);
__________________
<SnAzBaZ> how long ago was margaret thatcher in power ?
<Raistlin> like 82 she came into power
<Raistlin> shes ****ing sexie
<Raistlin> and I bet shes great in bed
<Raistlin> PHHHOOOAAAWWWWW I WANT HER!!!
|
|
|
1 Dec 2002, 23:31
|
#5
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
M_PI is defined in math.h under linux gcc (at least, i think it's called M_PI).
What I've allways wanted to know is how do you do inverse sin/cos/tan in C/C++? I've not seen a function that does it. Presumably its a bit of simple maths, but I havn't a clue how you do it...
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
1 Dec 2002, 23:51
|
#6
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
Code:
[email protected] [10:54pm] '~'
> $ cat megla.c
#include <math.h>
#include <stdio.h>
int main() {
double angle=M_PI/2,sine;
sine = sin(angle);
printf("sine of %f is %f\n",angle,sine);
printf("asin(%f) is %f\n",sine,asin(sine));
return 0;
}
[email protected] [10:55pm] '~'
> $ gcc -o megla megla.c -Wall -lm
[email protected] [10:55pm] '~'
> $ ./megla
sine of 1.570796 is 1.000000
asin(1.000000) is 1.570796
__________________
#linux : Home of Genius
<idimmu> ok i was chained to a desk with this oriental dude
|
|
|
1 Dec 2002, 23:59
|
#7
|
Born Sinful
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
|
You the daddeh.
I really need to dredge up some of that maths 'stuff' I learnt at AS. Oh well, at least I got the Pi constant right.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
|
|
|
2 Dec 2002, 01:26
|
#8
|
Gubbish
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
|
Quote:
Originally posted by meglamaniac
M_PI is defined in math.h under linux gcc (at least, i think it's called M_PI).
What I've allways wanted to know is how do you do inverse sin/cos/tan in C/C++? I've not seen a function that does it. Presumably its a bit of simple maths, but I havn't a clue how you do it...
|
You have atan, which you can derive asin and acos from
__________________
Gubble gubble gubble gubble
|
|
|
2 Dec 2002, 07:29
|
#9
|
Rawr rawr
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
|
I do remember using PI once.... I can't remember if it was under Borland or VC++ tho (or was that in Java, hmmm). deg2rad isn't a standard function either.
BTW, anyone an opinion about an acceptable precision of PI? I'm using it for a rotation matrix and in combination with (float)'s.
__________________
"Yay"
|
|
|
2 Dec 2002, 16:04
|
#10
|
Registered User
Join Date: Jun 2000
Posts: 8,476
|
Quote:
Originally posted by Structural Integrity
BTW, anyone an opinion about an acceptable precision of PI? I'm using it for a rotation matrix and in combination with (float)'s.
|
Since theres no performance bonus whatsoever by using lower precision, youd be as well just using 3.141592653589796 or something. I doubt you'd actually need greater precision than 3.141592 in 90% of cases though.
|
|
|
2 Dec 2002, 20:35
|
#11
|
Registered User
Join Date: Aug 2000
Location: UK
Posts: 228
|
Quote:
Originally posted by Structural Integrity
I do remember using PI once.... I can't remember if it was under Borland or VC++ tho (or was that in Java, hmmm). deg2rad isn't a standard function either.
BTW, anyone an opinion about an acceptable precision of PI? I'm using it for a rotation matrix and in combination with (float)'s.
|
Way back when I dabbled in 3D (about 10 years ago using assembler) I just used a lookup table. 1 byte = 0-127 signed then rotate the right 8 bits off. It gives 1% accuracy which is fine for most things, especially if it's moving about 20-30fps or faster. Dunno how efficiently this would translate to C tho and maths performance is a lot better nowadays.
__________________
<DrNick> hey i've been playing pa for ages and i know lots of people who bathe with 6 busty babes
<breeze|away> i agree about mang tho .. he is our mentor of perviness
R4: 48:25:13, 2E5 HC R5: n00b
R6: 3:10:1 Mangor, The Daddy
R7: 3:3:11 Retired
R8: c43 Finally free
PAX: 3:7:9 The Mentor of Perviness
|
|
|
3 Dec 2002, 00:43
|
#12
|
/dev/zero Retired Mod
Join Date: May 2000
Posts: 415
|
Quote:
Originally posted by Structural Integrity
... deg2rad isn't a standard function either.
|
Code:
#include <math.h>
#include <stdio.h>
#ifndef _RAD_DEG
#define _RAD_DEG
#define DEG2RAD(x) ((x) * M_PI / 180)
#define RAD2DEG(x) ((x) * 180 / M_PI)
#endif
int main() {
double angle=90.0,sine;
sine = sin(DEG2RAD(angle));
printf("sine of %f is %f\n",angle,sine);
printf("asin(%f) is %f\n",sine,RAD2DEG(asin(sine)));
return 0;
}
etc.
__________________
#linux : Home of Genius
<idimmu> ok i was chained to a desk with this oriental dude
|
|
|
3 Dec 2002, 00:59
|
#13
|
Gubbish
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
|
Oh, and a precalculated table is of course much much faster if you don't need 100% accuracy.
__________________
Gubble gubble gubble gubble
|
|
|
3 Dec 2002, 08:47
|
#14
|
∞+♪˛
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
|
I normally just type
#define PI 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825
#define PI2 (2.*PI)
(Or sometimes just cut and paste, if I can remember the name of a program where I used π and could be bothered to open the code. (Usually can't, so just type it.))
Anyway, then you can use sin( θ*(PI/180.)) and cos( θ*(PI/180.))
Quote:
Originally posted by Nodrog
Since theres no performance bonus whatsoever by using lower precision, youd be as well just using 3.141592653589796 or something. I doubt you'd actually need greater precision than 3.141592 in 90% of cases though.
|
I suppose you meant 3.141592653589793...
Quote:
Originally posted by Structural Integrity
I do remember using PI once.... I can't remember if it was under Borland or VC++ tho (or was that in Java, hmmm). deg2rad isn't a standard function either.
BTW, anyone an opinion about an acceptable precision of PI? I'm using it for a rotation matrix and in combination with (float)'s.
|
I think 90 decimal places is usually sufficient for most purposes.
Quaternions can be useful for rotation - rather homogenous, and no sin/cos involved. And easy to combine two rotations, without turning them into matrixes first. (Depends on what you want to rotate, and how you want to rotate it, I guess.)
__________________
Structural Integrity for Creator - since he'll probably make PA turn 3D.
Wikipedia forum
Note to self - Don't write Chinese letters with bold and italics... 猫
<!--Last incarnation: Nov 2000-->
Last edited by Cyp; 3 Dec 2002 at 08:54.
|
|
|
|
All times are GMT +1. The time now is 23:54.
| |