#### Archived

This topic is now archived and is closed to further replies.

# Trigonometry functions in C++

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

## Recommended Posts

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"

##### Share on other sites
The standard trig functions always return values in radians. So if you want to convert them, simply: result * ( 180 / pi )

##### Share on other sites
or do this:
#define PIOVER180  0.0174532925199432957692369076848861fcos(angle*PIOVER180);sin(angle*PIOVER180);

saves you a divition

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
kwizatz,

if the "pi" is a constant, then 180/pi is a constant expression and can be evaluated at compile time.

##### Share on other sites
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]

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013481
×