Trigonometry functions in C++

Started by
7 comments, last by jarod83 21 years, 9 months ago
I need a litle help in c++... when I use the tan(), cos(), sin() and other trigonometry functions(math.h) in Visual C++ I get the result in radians. Is it possible to have the result in degree? If so, please give me some examples. thx jarod83 ----------------------------------------------------------- Einsterin: "Never stop questioning"
Advertisement
The standard trig functions always return values in radians. So if you want to convert them, simply: result * ( 180 / pi )
or do this:
#define PIOVER180  0.0174532925199432957692369076848861fcos(angle*PIOVER180);sin(angle*PIOVER180); 


saves you a divition

Here''s a cool code snippet to calculate PI:

PI = 4.0*atan(1.0);

I usually calculate this once as a global const, and use the calculated value rather than use whatever value is located in a standard header file. Not sure why...just personal preference, I guess.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Which takes me to a related question:
Is it possible to somehow use the internal pi of the fpu? From my assembly course (write atan in assembler - blargh) I know that the Intel FPU has a 80 Bit (however they got to that number) defined PI. Which the FPU could use whenever PI is involved.
Question is how to make the C compiler use this PI.

---------------------------
I may be getting older, but I refuse to grow up
I may be getting older, but I refuse to grow up
There is a simple way of getting the PI from the fpu of the processor to a float value.
You can use this code:


float PI;
_asm
{
fldpi //push standard pi on the fpu register stack
fst PI //store st0 to (float)PI
}


and now PI will contain the float (32bits) version of the number pi. It should also work with doubles.
There are also some other cool numbers prebuilt in the fpu, like:
fld1 -> 1
fldl2e -> base−2 logarithm of e
fldl2t -> base−2 log of 10
fldlg2 -> base−10 log of 2
fldln2 -> base−e log of 2
fldz -> zero
You can also refer to the nasm-reference documentaion on the nasm webpage

Have fun!

===========================
UNKNOWN caused an invalid page fault in module unknown at 0000:bff80eb6
: win98 with multiple sclerose

[edited by - SwSh on June 27, 2002 6:23:47 PM]
quote:Original post by grhodes_at_work
Here''s a cool code snippet to calculate PI:

PI = 4.0*atan(1.0);

I usually calculate this once as a global const, and use the calculated value rather than use whatever value is located in a standard header file. Not sure why...just personal preference, I guess.

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.


Not to mention this works better for architecture-specific values of pi. I hear the G4 calculates it as exactly 3.0, for a more straightforward user experience.
kwizatz,

if the "pi" is a constant, then 180/pi is a constant expression and can be evaluated at compile time.
quote:Original post by Anonymous Poster
if the "pi" is a constant, then 180/pi is a constant expression and can be evaluated at compile time.
True, but if you divide your angle (in degrees) with the constant 180/pi you will use a division anyway. But if you multiply your angle (in degrees) with the constant pi/180 you will not use a division, alas it would get a bit faster.

[edited by - dalleboy on June 28, 2002 4:45:34 AM]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

This topic is closed to new replies.

Advertisement