Math::Angle — Angle
NAME
Math::Angle - a class to handle geometric angles
SYNOPSIS
use Math::Angle;
my \φ = Math::Angle.new(deg => 63.4);
say φ.sin;
say cos φ;
DESCRIPTION
Math::Angle defines objects which represent angles. It was written to try to prevent the problems I kept encountering in forgetting to convert angles between radians and degrees.
Thus an angle may be defined in terms of radians, degrees, hours, grads, or turns, and then used in trigonometric expressions without concern for conversion between units.
The standard trigonometric functions available in Raku can all be
used as either methods on the Math::Angle object, or as functions
with the Math::Angle object as argument.
In addition, subroutines are available for the inverse trigonometric functions which return a Math::Angle object.
Some basic arithmetic using Math::Angle objects is also possible
where it makes sense.
Object creation
Math::Angle objects are created by calling the Math::Angle.new
routine with a named argument specifying the angle required. The following five possibilities are equivalent:
my $φ = Math::Angle.new( :rad( π/4 ));
my $λ = Math::Angle.new( :turn( 1/8 ));
my $η = Math::Angle.new( :deg( 45 ));
my $θ = Math::Angle.new( :grad( 50 ));
my $α = Math::Angle.new( :hour( 3 ));
When Math::Angle.new is called on named arguments min or
sec without also supplying deg or hour, then the minutes
and seconds are interpreted as minutes and seconds of arc, not of
time-angle: that is, as if :deg(0) accompanied them. Use :hour(0)
to force the other interpretation.
my Math::Angle $b .= new( :min(30) :sec(30) );
say $b.dms # OUTPUT: 0° 30′ 30.0000″
See also the functions from-dms and from-hms.
Accessing the angle value
The angle embedded within a Math::Angle object can be accessed using the five methods rad, deg, hour, grad and turn which convert the internal value to the requested representation
Note that it is thus possible to convert an angle from degrees to radians by writing
Math::Angle.new(deg => 27).rad
Arithmetic
Arithmetic involving Math::Angle objects is possible where it makes sense:
my \φ1 = Math::Angle.new(:37deg);
my \φ2 = Math::Angle.new(rad => π/6);
say (φ1 - φ2).deg;
say (φ1 + φ2).rad;
say (φ1 × 2).rad;
say (4 × φ2).deg;
say (φ1 ÷ 4).grad;
say φ1 ÷ φ2;
With the exception of division of two angles,
the results of all those objects will be a Math::Angle object.
The result of the division of two angles is a plain number
representing the ratio of the two angles.
Coercion
Math::Angle has two coercion methods, Numeric and Complex.
Numeric returns the numeric value of the angle in radians. Complex returns a complex number with radius 1 and angle given by the Math::Angle object.
Math::Angle also inherits from Cool. When combined with the Numeric
coercion, this gives access to the trigonometric functions
sin,
cos,
tan,
sec,
cosec,
cotan,
sinh,
cosh,
tanh,
sech.
cosech, and
cotanh.
These may all be used as functions with the Math::Angle object as argument,
or as methods on the Math::Angle object.
It probably also leads to all sorts of other undesirable effects.
Functions
The following standard trigonometric functions may be used to return
a Math::Angle object:
asin,
acos,
atan,
atan2,
asec,
acosec,
acotan,
asinh,
acosh,
atanh,
asech,
acosech, and
acotanh.
from-dms
from-dms takes a string argument representing an angle in degrees, minutes and seconds, and converts it into a Math::Angle object.
The string contains:
sign An optional sign
+or-;degrees An optional decimal number of degrees followed by a degree designator (one of the degree symbol (
°) or a letterdof either case);minutes An optional decimal number of minutes followed by a minutes designator (one of a single quote (
'), the Unicode characterU+2032—PRIME, the Unicode characterU+1D50—MODIFIER LETTER SMALL M, or the lettermin either case);seconds An optional decimal number of seconds followed by a seconds designator (one of a double quote
"), the Unicode characterU+2033—DOUBLE PRIME, the Unicode characterU+02E2—MODIFIER LETTER SMALL S, or the lettersin either case);hemisphere To allow for latitude and longitude to be represented easily, the angle can be followed by one of the letters
E,W,NorSin either case. If the letter isNorEthen the angle is positive; if it isSorWthen the angle is negative. Note that if a sign is also present at the start of the string, then the two indicators are both used. Thus an angle like-12°Swill end up being positive.
The various fields may be separated by white space.
The decimal numbers can be fractional, and contain exponents such as 12.53e5.
Note that no sanity checks are made. An angle of "-2473.2857° 372974.2746′ 17382.2948e5s W" is acceptable, and equal to about 491531.046°. The values for degrees, minutes and seconds are simply added together with the appropriate scaling and with the appropriate sign calculated from the initial sign and the hemisphere.
from-hms
from-hms takes a string argument representing an angle in hours, minutes, and seconds, and converts it into a Math::Angle object.
The string contains:
sign An optional sign
+or-;hours An optional decimal number of hours followed by an hour designator (one of the hour symbol (
ʰ) or a letterhof either case);minutes An optional decimal number of minutes followed by a minutes designator (one of the minute symbol (
ᵐ) or the lettermin either case);seconds An optional decimal number of seconds followed by a seconds designator (one of a seconds symbol
ˢ) or the lettersin either case);
The various fields may be separated by white space.
The decimal numbers can be fractional, and contain exponents such as 12.53e5.
Note that no sanity checks are made, as with degree-minute-second (see above). The values for hours, minutes and seconds are simply added together with the appropriate scaling and with the appropriate sign calculated from the initial sign.
Normalization
Objects created in any way, including as a result of arithmetic functions, are not generally normalized in any way. The angle can be as large (positive or negative) as the code generates. If an angle needs to be restricted in its range to [0, 360°) or [-180°, 180°) then the following techniques may be used.
normalize function
The function Math::Angle::normalize takes a single argument as an angle in radians, and returns a Real in the range [0, 2π).
An optional named argument range is available which can take the value Math::Angle::Range::SYMMETRIC to specify that the result should be in the range [-π, π) or the value Math::Angle::Range::POSITIVE to specify that the result should be in the range [0, 2π).
normalize method
A Math::Angle object has a method c<normalize> which may be used to force an angle to the range [-π, π). An optional named argument range is available. This may be set to the value Math::Angle::Range::SYMMETRIC (the default) or Math::Angle::Range::POSITIVE to specify a range of [0, 2π).
Note that as an artefact of the current implementation this method returns the resultant angle in radians. Use of this is deprecated, and this will be removed in a future version.
normalize named argument to object creation
The three explicit methods of creating Math::Angle objects (new, from-dms and from-hms) all allow a named argument normalize. This defaults to False but may be set to <True>. If it is true, the resultant angle will be truncated to the range [-π, π). An additional named argument range may be set to Math::Angle::Range::POSITIVE to force the angle to the range [0, 2π).
Alternative representations
Four methods are available to produce string output in useful forms for angles expressed in degrees or hours.
dms
Method dms will return a string containing a representation of the angle in degrees, minutes and seconds. For example:
Math::Angle.new(rad => 0.82).dms.say; # 46° 58′ 57.1411″
However the output is fairly configurable. For example, signed numbers can be output in various forms:
Math::Angle.new(rad => -0.82).dms.say; # -46° 58′ 57.1411″
Math::Angle.new(rad => 0.82).dms(presign=>"+-").say; # +46° 58′ 57.1411″
Math::Angle.new(rad => 0.82).dms(presign=>"", postsign=>"NS").say; # 46° 58′ 57.1411″N
Math::Angle.new(rad => -0.82).dms(presign=>"", postsign=>"NS").say; # 46° 58′ 57.1411″S
Math::Angle.new(rad => -0.82).dms(presign=>"", postsign=>"EW").say; # 46° 58′ 57.1411″W
The full set of options is:
| option | meaning | default | |
|---|---|
| degsym | A string representing the symbol printed after the number of degrees | "°" (unicode U00B0 — DEGREE SIGN) | |
| minsym | A string representing the symbol printed after the number of minutes | "′" (unicode U2032 — PRIME) | |
| secsym | (dms only) A string giving the symbol printed after the number of seconds | '″' (unicode U2033 — DOUBLE PRIME) | |
| presign | A string where the first character is printed before a positive number, and the second character is printed before a negative number; spaces are not printed | " -" (a space and a minus sign) |
| postsign | A string where the first character is printed after a positive number, and the second character is printed after a negative number; spaces are not printed | " " (two spaces) |
| separator | A string printed between the degrees and minutes, and between the minutes and seconds | " " | |
| secfmt | (dms only) The format string for the seconds field | "%.4f" | |
| minfmt | (dm only) The format string for the minutes field | "%.4f" |
Note for presign and postsign that a blank in the string is not printed.
hms
Method hms will return a string containing a representation of the angle in hours, minutes and seconds. For example:
Math::Angle.new(rad => 0.82).hms.say; # 3ʰ 7ᵐ 55.8094ˢ
The output is configurable for sign output and decimal format. For example:
Math::Angle.new(:rad(-0.82)).hms.say; # -3ʰ 7ᵐ 55.8094ˢ
Math::Angle.new(:rad( 0.82)).hms(presign=>"+-").say; # +3ʰ 7ᵐ 55.8094ˢ
The full set of options is:
| option | meaning | default |
|---|---|
| hrsym | A string representing the symbol printed after the number of hours | "ʰ" (unicode U02B0 — MODIFIER LETTER SMALL H) |
| minsym | A string representing the symbol printed after the number of minutes | "ᵐ" (unicode U1D50 — MODIFIER LETTER SMALL M) |
| secsym | (hms only) A string giving the symbol printed after the number of seconds | 'ˢ' (unicode U02E2 — MODIFIER LETTER SMALL S) |
| presign | A string where the first character prefixes a positive number, and the second a negative number | " -" (space, minus sign) |
| secfmt | The format string for the seconds field | "%.4f" |
| minfmt | The format string for the minutes field | "%.4f" |
Note for presign that a blank in the string is not printed.
dm
Method dm will return a string containing a representation of the angle in degrees and minutes. For example:
Math::Angle.new(rad => 0.82).dm.say; # 46° 58' 57.1411"
The same configurability as in dms is available:
Math::Angle.new(rad => -0.82).dm.say; # -46° 58.9524′
Math::Angle.new(rad => 0.82).dm(presign=>"+-").say; # +46° 58.9524′
Math::Angle.new(rad => 0.82).dm(presign=>"", postsign=>"NS").say; # 46° 58.9524'N
Math::Angle.new(rad => -0.82).dm(presign=>"", postsign=>"NS").say; # 46° 58.9524′S
Math::Angle.new(rad => -0.82).dm(presign=>"", postsign=>"EW").say; # 46° 58.9524′W
The options are the same as for dms, except that secsym is not available and secfmt is replaced by minfmt.
hm
Method hm will return a string containing a representation of the angle in hours and minutes. For example:
Math::Angle.new(:rad(0.82)).hm.say; # 3ʰ 8ᵐ
Math::Angle.new(:rad(-0.82)).hm(:minfmt('%07.4f') :hrsym(':') :minsym('')).say; # -3:07.9302
The same configurability as in hms is available:
Math::Angle.new(rad => -0.82).dm.say; # -3ʰ07.9302ᵐ
Math::Angle.new(rad => 0.82).dm(presign=>"+-").say; # +3ʰ07.9302ᵐ
The options are the same as for hms, except that secsym is not available and secfmt is replaced by minfmt.
AUTHOR
Kevin Pye <[email protected]>
COPYRIGHT AND LICENSE
© Copyright 2022–2026 Kevin Pye
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.