Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Angles

Astronomy mixes angle units constantly. Right ascension comes in hours, declination in degrees, catalog errors in arcseconds, and every trig function wants radians. A bare f64 doesn’t say which one it holds, so a degree value passed where radians belong compiles fine and gives a wrong answer.

Angle fixes that by making the unit part of the type. You build one from whatever unit your data uses and read it back in whatever unit you need:

use celestial_core::angle::Angle;

let dec = Angle::from_degrees(-16.7161);
let ra = Angle::from_hours(6.752472);

let (sin_dec, cos_dec) = dec.sin_cos(); // no manual conversion to radians
ra.degrees(); // 101.28708

Angle stores radians in a single f64. It’s Copy, so you pass it by value everywhere. All of its trig methods call the pure-Rust libm crate instead of the platform math library.

Create an angle

There’s one constructor per unit:

use celestial_core::angle::Angle;
use celestial_core::constants::QUARTER_PI;

let a = Angle::from_degrees(45.0);
let b = Angle::from_radians(QUARTER_PI);
let c = Angle::from_hours(3.0);           // 1h = 15°
let d = Angle::from_arcseconds(162000.0); // 3600" per degree
let e = Angle::from_arcminutes(2700.0);   // 60' per degree

from_radians is the only const fn constructor, because radians need no conversion. Use it to define angle constants:

use celestial_core::angle::Angle;
use celestial_core::constants::HALF_PI;

const RIGHT_ANGLE: Angle = Angle::from_radians(HALF_PI);

Take PI, HALF_PI, TWOPI and friends from celestial_core::constants, not std::f64::consts. The library uses its own constants everywhere, so your values match the ones its code computes with.

Angle also has three associated constants: Angle::ZERO, Angle::PI (180°) and Angle::HALF_PI (90°).

Shorthand functions

The angle module has short free functions that call the matching constructor:

use celestial_core::angle::{arcmin, arcsec, deg, hours, rad};
use celestial_core::constants::QUARTER_PI;

let a = deg(45.0);       // Angle::from_degrees
let b = rad(QUARTER_PI); // Angle::from_radians
let c = hours(3.0);      // Angle::from_hours
let d = arcsec(162000.0);
let e = arcmin(2700.0);

Read an angle in another unit

Every constructor has a matching accessor:

use celestial_core::angle::Angle;

let angle = Angle::from_degrees(45.0);

angle.radians();    // 0.7853981633974483
angle.degrees();    // 45.0
angle.hours();      // 3.0
angle.arcseconds(); // 162000.0
angle.arcminutes(); // 2700.0

Right ascension is measured in hours of time, and 24 hours make a full circle. One hour is 15°, one minute of time is 15 arcminutes, and one second of time is 15 arcseconds.

use celestial_core::angle::Angle;

// Sirius RA: 6h 45m 8.9s
let ra = Angle::from_hours(6.0 + 45.0 / 60.0 + 8.9 / 3600.0);
ra.degrees(); // 101.28708333333333
ra.hours();   // 6.752472222222222

Trigonometry

sin, cos and tan return f64. sin_cos returns both as a (sin, cos) tuple, which is what rotation matrices and coordinate transforms usually need.

use celestial_core::angle::Angle;

let angle = Angle::from_degrees(30.0);

angle.sin(); // 0.5
angle.cos(); // 0.8660254037844386
angle.tan(); // 0.5773502691896258

let (sin, cos) = angle.sin_cos();

Wrap an angle into a standard range

270° and -90° point the same way, but conventions differ on which one to report. Right ascension and azimuth are conventionally positive. Hour angles and longitude differences read better as the short way round. Two methods cover both:

use celestial_core::angle::Angle;

let angle = Angle::from_degrees(-90.0);

angle.normalized()?.degrees(); // 270.0, range [0°, 360°)
angle.wrapped()?.degrees();    // -90.0, range -180° to +180°

Both return Result and fail on NaN or infinity, so a bad input stops at the wrap instead of spreading through later math. One edge case: wrapped() turns exactly +180° into -180° and exactly -180° into +180°.

Arithmetic results are never wrapped for you. Call normalized() or wrapped() when you need a result in a standard range.

abs() returns the absolute value:

use celestial_core::angle::Angle;

Angle::from_degrees(-45.0).abs().degrees(); // 45.0

Wrap raw radians

When you have a bare f64 in radians, wrap_0_2pi and wrap_pm_pi do the same job. Both return Result<f64, AstroError> and fail on NaN or infinity.

use celestial_core::angle::{wrap_0_2pi, wrap_pm_pi};

wrap_0_2pi(-1.0)?; // 5.283185307179586
wrap_pm_pi(5.0)?;  // -1.2831853071795862

Validate an angle

User input and file headers can hold anything. The validators check that an angle makes sense for what it claims to be. Each returns Result<Angle, AstroError> and rejects NaN and infinity.

MethodAcceptsOn success returns
validate_right_ascension()any finite anglethe angle wrapped to [0°, 360°)
validate_declination(false)[-90°, +90°]the angle unchanged
validate_declination(true)[-180°, +180°]the angle unchanged
validate_latitude()[-90°, +90°]the angle unchanged
use celestial_core::angle::Angle;

let ra = Angle::from_degrees(400.0).validate_right_ascension()?;
ra.degrees(); // 40.00000000000001

let dec = Angle::from_degrees(100.0);
dec.validate_declination(false).is_err(); // true
dec.validate_declination(true)?;          // ok

Right ascension is cyclic, so its validator wraps instead of rejecting. 2π has no exact f64 value, which is why 400° comes back one bit above 40°.

The beyond_pole flag on validate_declination widens the range to ±180°. German equatorial mounts report declinations past the pole when they track through it.

There is no longitude validator. Longitude is cyclic, so use normalized().

The same checks exist as free functions that take the angle as an argument:

use celestial_core::angle::{
    validate_declination, validate_latitude, validate_right_ascension, Angle,
};

let angle = Angle::from_degrees(45.0);

validate_right_ascension(angle)?;
validate_declination(angle, false)?;
validate_latitude(angle)?;

Parse an angle from a string

The angle module has two parsers. They accept different notations, so pick by where your strings come from.

Units you know: AngleUnits

When you know what a string holds, the AngleUnits trait on str reads it directly. Every method returns Result<Angle, AstroError>.

use celestial_core::angle::AngleUnits;

// Decimal numbers in a named unit
let a = "45.5".deg()?;
let b = "0.785".rad()?;
let c = "12.5".hours()?;
let d = "60.0".arcmin()?;
let e = "3600.0".arcsec()?;

// Sexagesimal
let ra = "12:34:56".hms()?;
let dec = "-45:30:15".dms()?;

// Letter markers and spelled-out units
let ra2 = "12h34m56s".hms()?;
let ra3 = "12 hours 34 minutes 56 seconds".hms()?;
let dec2 = "45d30m15s".dms()?;
let dec3 = "45 degrees 30 arcmin 15 arcsec".dms()?;

.hms() and .dms() need all three fields. "12h30m" is an error. The free functions parse_hms and parse_dms do the same parsing as these two methods.

Units you don’t know: ParseAngle

For free-form input, ParseAngle::to_angle tries HMS, then DMS, then decimal degrees:

use celestial_core::angle::ParseAngle;

"12:34:56".to_angle()?.hours();    // 12.582222222222223, read as HMS
"45d30m15s".to_angle()?.degrees(); // 45.50416666666667, read as DMS
"45.5".to_angle()?.degrees();      // 45.5, read as decimal degrees

A colon string like 12:34:56 could be hours or degrees, and to_angle always picks hours. Call .dms() when you know the string is a declination.

Symbol notation: parse_angle

parse_angle splits on marker characters, including °, ', " and the superscripts ʰ ᵐ ˢ. It reads the output of DmsFmt and HmsFmt (below), which the AngleUnits methods reject. It returns a ParsedAngle, whose angle field holds the result.

use celestial_core::angle::parse_angle;

let ra = parse_angle("05h14m32.27s")?.angle;
ra.hours(); // 5.242297222222223

let dec = parse_angle("-08°12'05.9\"")?.angle;
dec.degrees(); // -8.201638888888889

parse_angle reads a string as HMS when it has an h, ʰ or :, and as DMS otherwise. A bare number such as "45.5" is 45.5°. A colon string such as "12:30:15" comes back in hours, so call .dms() from AngleUnits when a colon string holds degrees.

Format an angle for display

DmsFmt writes degrees, arcminutes and arcseconds, for declination, latitude and altitude. The sign is always shown. frac_digits sets the decimal places on the arcseconds.

use celestial_core::angle::{Angle, DmsFmt};

let dec = Angle::from_degrees(-23.4392);

DmsFmt { frac_digits: 0 }.fmt(dec); // -23° 26' 21"
DmsFmt { frac_digits: 2 }.fmt(dec); // -23° 26' 21.12"

HmsFmt writes hours, minutes and seconds with the superscript markers ʰ ᵐ ˢ, for right ascension and hour angles. It wraps negative angles into [0h, 24h).

use celestial_core::angle::{Angle, HmsFmt};

let fmt = HmsFmt { frac_digits: 1 };

fmt.fmt(Angle::from_hours(14.5));  // 14ʰ 30ᵐ 0.0ˢ
fmt.fmt(Angle::from_hours(-1.5));  // 22ʰ 30ᵐ 0.0ˢ

Display prints decimal degrees with six decimal places:

use celestial_core::angle::Angle;

format!("{}", Angle::from_degrees(45.123456789)); // 45.123457°

Arithmetic

Angles add, subtract and negate, and multiply or divide by an f64:

use celestial_core::angle::Angle;

let a = Angle::from_degrees(30.0);
let b = Angle::from_degrees(15.0);

(a + b).degrees();   // 45.00000000000001
(a - b).degrees();   // 15.000000000000002
(a * 2.0).degrees(); // 60.00000000000001
(a / 2.0).degrees(); // 15.000000000000002
(-a).degrees();      // -30.000000000000004

The trailing digits come from the round trip through radians. Each degree value is converted to radians on the way in and back on the way out, and each conversion rounds. Compare angles with a tolerance, or keep your values in radians, when the last bit matters.

Where next