Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by tachyon (Chancellor)
on Dec 22, 2002 at 03:44 UTC ( [id://221713]=note: print w/replies, xml ) Need Help??


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

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';
  • Comment on Re: How can I get sine, cosine, and tangent to return values in degrees?
  • Download Code

Replies are listed 'Best First'.
Re: Answer: How can I get sine, cosine, and tangent to return values in degrees?
by Belgarion (Chaplain) on Apr 29, 2004 at 17:42 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://221713]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-18 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found