User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 1 Dec 2002, 22:10   #1
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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"
Structural Integrity is offline   Reply With Quote
Unread 1 Dec 2002, 22:33   #2
Ragnarak
Registered User
 
Ragnarak's Avatar
 
Join Date: Oct 2000
Posts: 4,944
Ragnarak is a splendid one to beholdRagnarak is a splendid one to beholdRagnarak is a splendid one to beholdRagnarak is a splendid one to beholdRagnarak is a splendid one to beholdRagnarak is a splendid one to beholdRagnarak is a splendid one to behold
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
Ragnarak is offline   Reply With Quote
Unread 1 Dec 2002, 22:36   #3
Ragnarak
Registered User
 
Ragnarak's Avatar
 
Join Date: Oct 2000
Posts: 4,944
Ragnarak is a splendid one to beholdRagnarak is a splendid one to beholdRagnarak is a splendid one to beholdRagnarak is a splendid one to beholdRagnarak is a splendid one to beholdRagnarak is a splendid one to beholdRagnarak is a splendid one to behold
oh and i don't think Pi is defined in any of the standard headers, we always had to define it at the start
Ragnarak is offline   Reply With Quote
Unread 1 Dec 2002, 23:06   #4
SnAzBaZ
beware of the winky
 
Join Date: Feb 2002
Posts: 17
SnAzBaZ is an unknown quantity at this point
#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!!!
SnAzBaZ is offline   Reply With Quote
Unread 1 Dec 2002, 23:31   #5
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
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.
meglamaniac is offline   Reply With Quote
Unread 1 Dec 2002, 23:51   #6
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
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
MT is offline   Reply With Quote
Unread 1 Dec 2002, 23:59   #7
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
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.
meglamaniac is offline   Reply With Quote
Unread 2 Dec 2002, 01:26   #8
W
Gubbish
 
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
W is a jewel in the roughW is a jewel in the roughW is a jewel in the rough
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
W is offline   Reply With Quote
Unread 2 Dec 2002, 07:29   #9
Structural Integrity
Rawr rawr
 
Structural Integrity's Avatar
 
Join Date: Dec 2000
Location: Upside down
Posts: 5,300
Structural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriendStructural Integrity needs a job and a girlfriend
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"
Structural Integrity is offline   Reply With Quote
Unread 2 Dec 2002, 16:04   #10
Nodrog
Registered User
 
Join Date: Jun 2000
Posts: 8,476
Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.Nodrog has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
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.
Nodrog is offline   Reply With Quote
Unread 2 Dec 2002, 20:35   #11
mikay
Registered User
 
Join Date: Aug 2000
Location: UK
Posts: 228
mikay is an unknown quantity at this point
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
mikay is offline   Reply With Quote
Unread 3 Dec 2002, 00:43   #12
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
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
MT is offline   Reply With Quote
Unread 3 Dec 2002, 00:59   #13
W
Gubbish
 
Join Date: Sep 2000
Location: #FoW
Posts: 2,323
W is a jewel in the roughW is a jewel in the roughW is a jewel in the rough
Oh, and a precalculated table is of course much much faster if you don't need 100% accuracy.
__________________
Gubble gubble gubble gubble
W is offline   Reply With Quote
Unread 3 Dec 2002, 08:47   #14
Cyp
∞+♪˛
 
Join Date: Nov 2000
Location: :uo!te]oŻ|
Posts: 428
Cyp is an unknown quantity at this point
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 &pi; and could be bothered to open the code. (Usually can't, so just type it.))

Anyway, then you can use sin(&theta;*(PI/180.)) and cos(&theta;*(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.
Cyp is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 23:54.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018