Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

OK, I'm new to perl and I'm trying to use the sin thingy in a script (to do my geometry homework with :) When I use it, it comes out to a different number than what my calculator comes out with. Any ideas on whats wrong with me/it?

Replies are listed 'Best First'.
Re: whats up with the sin stuff?
by repson (Chaplain) on Apr 18, 2001 at 19:23 UTC
    Quite simple, perl (and most other programming languages) take angles for trig functions in radians, while calculators take them in degrees, radians or gradians (usually). Most people prefer degrees, but radians are nicer to mathamaticians and other similar people.

    The best thing for you to do is

    use Math::Trig; $num = sin( deg2rad( 45 ) );
    I think Math::Trig is part of the standard perl distribution, so read the documentation and go for it.

    Update: I know about the math of radians as jepri explained but after the amount of it i've done at school recently I didn't feel like providing an explanation.

      Or you could just divide or multiply by pi/180 (depending on whether you are converting to or from).

      It helps to know that there are 2*pi radians in circle. So the math goes like this:

      (radiians)2*pi=360(degrees) pi=180 1 radiian=180/pi degrees turn it around 1 degree=pi/180 radiians

      I tend to work in radiians, since it's easier for a few things.

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.

Re: whats up with the sin stuff?
by Anonymous Monk on Apr 18, 2001 at 19:21 UTC
    sin expects the argument to be expressed in radians. If its geometry homework you're probably trying to work in degrees.