http://qs321.pair.com?node_id=221674

dooberwah has asked for the wisdom of the Perl Monks concerning the following question: (math)

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

Originally posted as a Categorized Question.

  • Comment on How can I get sine, cosine, and tangent to return values in degrees?

Replies are listed 'Best First'.
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

    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';
Re: How can I get sine, cosine, and tangent to return values in degrees?
by tachyon (Chancellor) on Dec 23, 2002 at 07:43 UTC
Re: How can I get sine, cosine, and tangent to return values in degrees?
by barrachois (Pilgrim) on Dec 30, 2002 at 21:01 UTC
    Rather than have a line like this
    my $pi = 3.14159265358979;
    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);
Re: How can I get sine, cosine, and tangent to return values in degrees?
by tachyon (Chancellor) on Dec 22, 2002 at 03:53 UTC

    Sorry the atan() function is missing a ,1 ie

    sub atan { atan2($_[0],1) };

    Originally posted as a Categorized Answer.

Re: How can I get sine, cosine, and tangent to return values in degrees?
by stefan k (Curate) on Dec 22, 2002 at 16:33 UTC
    This isn't really an answer, it's more adding a wish to the question (and not knowing how to remove the "Answer:" in the subject)...

    I'd really consider it nice and usefull if you could just type something along

    use Math::Trig::Degree;
    and the have the named functions considering their input values as measured in deg (rad, grad resp.).

    Just a thought...

    Originally posted as a Categorized Answer.

      Just uploaded Math::Trig::Degree which will do degrees and radians as you desire. It will take a few hours to appear in the indexing.

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: How can I get sine, cosine, and tangent to return values in degrees?
by Anonymous Monk on Apr 29, 2004 at 17:25 UTC
    Using a calculator, press 2ndF, then sin cosin or tangent(depending on what you're looking for), then the decimal, and round your answer to the nearest degree.

    Originally posted as a Categorized Answer.