Perl Monk, Perl Meditation PerlMonks

### How can I get sine, cosine, and tangent to return values in degrees?

by dooberwah (Pilgrim)
 on Dec 21, 2002 at 21:24 UTC Need Help??
Contributed by dooberwah on Dec 21, 2002 at 21:24 UTC
Q&A  > math

 Answer: How can I get sine, cosine, and tangent to return values in degrees?contributed by tachyon The perl sin and cos functions take arguments in the form of radians (rather than degrees). 180 degrees = Pi radians so both radians and degrees are measures of angle. You question has no answer per se as sin and cos do not return values that can be expressed as any measure of angle like radians or degrees. Hopefully this code covers what you want. The asin, acos and atan functions return radians and are the inverse functions of sin, cos and tan respectively: ```my \$pi = 3.14159265358979; sub deg_to_rad { (\$_[0]/180) * \$pi } sub rad_to_deg { (\$_[0]/\$pi) * 180 } sub asin { atan2(\$_[0], sqrt(1 - \$_[0] * \$_[0])) } sub acos { atan2( sqrt(1 - \$_[0] * \$_[0]), \$_[0] ) } sub tan { sin(\$_[0]) / cos(\$_[0]) } sub atan { atan2(\$_[0],1) }; print 'sin 30 degrees is ', sin(deg_to_rad(30)), "\n"; print 'inverse sin 0.5 is ', rad_to_deg(asin(0.5)), ' degrees'; [download]``` Answer: How can I get sine, cosine, and tangent to return values in degrees?contributed by tachyon Just uploaded Math::Trig::Degree which will do both degrees and radians. Answer: How can I get sine, cosine, and tangent to return values in degrees?contributed by barrachois Rather than have a line like this ``` my \$pi = 3.14159265358979; [download]``` which requires you to choose how many digits of pi you (or your operating system) want, and even may require (for some, at least) that you look up the value of pi, I would suggest that a line like this instead will do the right thing. ``` my \$pi = 4*atan2(1,1); [download]```

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Please read these before you post! —
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?
• See Writeup Formatting Tips and other pages linked from there for more info.

Create A New User
Chatterbox?
 [Discipulus]: planetscape welcome back! (or is well comeback?) [Discipulus]: fore every 2 good old monks that come back, we will accept 1 clinical [planetscape]: Might be "well, come back!" Discipulus ;-) [stonecolddevin]: that's a 2.0 k/d ratio, i'll take it [choroba]: #cbstream still down? [erix]: yeah, annoying [choroba]: last ambrus: 1 week ago :-(

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-06-22 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (530 votes). Check out past polls.