2.11. Doing Trigonometry in Degrees, not RadiansProblemYou want your trigonometry routines to operate in degrees instead of Perl's native radians. SolutionConvert between radians and degrees (2 radians equals 360 degrees). BEGIN { use constant PI => 3.14159265358979; sub deg2rad { my $degrees = shift; return ($degrees / 180) * PI; } sub rad2deg { my $radians = shift; return ($radians / PI) * 180; } } Alternatively, use the Math::Trig module. use Math::Trig; $radians = deg2rad($degrees); $degrees = rad2deg($radians); Discussion
If you're doing a lot of trigonometry, look into using either the standard Math::Trig or POSIX modules. They provide many more trigonometric functions than are defined in the Perl core. Otherwise, the first solution above will define the If you're looking for the sine in degrees, use this: # deg2rad and rad2deg defined either as above or from Math::Trig sub degree_sine { my $degrees = shift; my $radians = deg2rad($degrees); my $result = sin($radians); return $result; } See Also
The Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|