bearingpro 0.12.0

A Rust library for solving common maritime navigation tasks.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! Angle types that carry their reference frame in the type system.
//!
//! Every quantity in this crate used to be a bare `f64`, which meant that
//! `calculate_true_bearing(bearing, variation)` and
//! `calculate_true_bearing(variation, bearing)` both compiled and both produced a
//! plausible-looking course. The types here make that mistake impossible: a
//! [`CompassCourse`] cannot be passed where a [`MagneticCourse`] is expected, and
//! a [`Variation`] cannot be passed where a course is expected.
//!
//! They also carry the range invariant. A [`Direction`] is always finite and
//! always in `[0°, 360°)`, so the functions that consume one cannot fail on
//! range grounds and do not return `Result` at all.
//!
//! # Example
//!
//! ```rust
//! use bearingpro::{CompassCourse, Deviation, MagneticCourse, Variation};
//!
//! let cc = CompassCourse::new(3.0)?;
//! let variation = Variation::new(-2.7)?;
//!
//! // Out-of-range and non-finite inputs are rejected at construction.
//! assert!(CompassCourse::new(400.0).is_err());
//! assert!(Variation::new(f64::NAN).is_err());
//!
//! // 360° is accepted and normalised to 0°.
//! assert_eq!(CompassCourse::new(360.0)?.degrees(), 0.0);
//!
//! // Arbitrary values can be wrapped explicitly when that is what you mean.
//! assert_eq!(MagneticCourse::wrap(-10.0)?.degrees(), 350.0);
//! # Ok::<(), bearingpro::NavigationError>(())
//! ```

use core::fmt;
use core::marker::PhantomData;

use crate::error::{NavigationError, Result};
use crate::math;

/// Largest magnitude accepted for a magnetic variation, in degrees.
pub const MAX_VARIATION_DEG: f64 = 180.0;

/// Largest magnitude accepted for a compass deviation, in degrees.
pub const MAX_DEVIATION_DEG: f64 = 180.0;

/// Normalises any finite angle into `[0.0, 360.0)`.
///
/// Unlike the `(angle + 360.0) % 360.0` idiom this is correct for every finite
/// input, including values below `-360°`.
#[must_use]
pub fn wrap360(degrees: f64) -> f64 {
    let remainder = degrees % 360.0;
    if remainder < 0.0 {
        let shifted = remainder + 360.0;
        // A remainder of, say, -1e-16 is nearer to 360 than the next `f64` below
        // it, so the addition rounds to exactly 360.0 and would escape the
        // half-open interval. That value belongs at the other end.
        if shifted >= 360.0 {
            0.0
        } else {
            shifted
        }
    } else {
        // Adding zero turns a `-0.0` remainder into `+0.0`.
        remainder + 0.0
    }
}

/// Normalises any finite angle into `[-180.0, 180.0)`.
#[must_use]
pub fn wrap180(degrees: f64) -> f64 {
    let wrapped = wrap360(degrees);
    if wrapped >= 180.0 {
        wrapped - 360.0
    } else {
        wrapped
    }
}

pub(crate) fn ensure_finite(parameter: &'static str, value: f64) -> Result<()> {
    if value.is_finite() {
        Ok(())
    } else {
        Err(NavigationError::NotFinite { parameter, value })
    }
}

pub(crate) fn ensure_range(parameter: &'static str, value: f64, min: f64, max: f64) -> Result<()> {
    ensure_finite(parameter, value)?;
    if value < min || value > max {
        return Err(NavigationError::OutOfRange {
            parameter,
            value,
            min,
            max,
        });
    }
    Ok(())
}

mod sealed {
    pub trait Sealed {}
}

/// The reference frame a [`Direction`] is measured from.
///
/// This trait is sealed: the three frames below are the only ones that exist.
pub trait Frame: sealed::Sealed + Copy + Clone + fmt::Debug + 'static {
    /// Human readable frame name, used in error and display output.
    const NAME: &'static str;
    /// Single-letter suffix used when formatting, as in `045.0°M`.
    const SUFFIX: char;
}

/// Measured from true (geographic) north.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct True;

/// Measured from magnetic north.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Magnetic;

/// Measured from the direction the ship's compass card calls north.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Compass;

/// Measured from the direction the gyrocompass calls north.
///
/// A gyrocompass has no deviation — it is not magnetic — but it does have a
/// single error, which is why it gets a frame of its own rather than sharing the
/// compass one.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Gyro;

impl sealed::Sealed for True {}
impl sealed::Sealed for Magnetic {}
impl sealed::Sealed for Compass {}
impl sealed::Sealed for Gyro {}

impl Frame for True {
    const NAME: &'static str = "true";
    const SUFFIX: char = 'T';
}

impl Frame for Magnetic {
    const NAME: &'static str = "magnetic";
    const SUFFIX: char = 'M';
}

impl Frame for Compass {
    const NAME: &'static str = "compass";
    const SUFFIX: char = 'C';
}

impl Frame for Gyro {
    const NAME: &'static str = "gyro";
    const SUFFIX: char = 'G';
}

/// A direction in `[0°, 360°)`, tagged with the frame it is measured from.
///
/// Courses and bearings share this type: within one frame they are the same
/// quantity and obey the same arithmetic. What the type prevents is mixing
/// *frames* — the error that actually puts a ship aground.
#[derive(Clone, Copy, PartialEq, PartialOrd, Default)]
pub struct Direction<F: Frame> {
    degrees: f64,
    frame: PhantomData<F>,
}

/// A course or bearing referred to true north.
pub type TrueCourse = Direction<True>;
/// A bearing referred to true north. Alias of [`TrueCourse`].
pub type TrueBearing = Direction<True>;
/// A course or bearing referred to magnetic north.
pub type MagneticCourse = Direction<Magnetic>;
/// A bearing referred to magnetic north. Alias of [`MagneticCourse`].
pub type MagneticBearing = Direction<Magnetic>;
/// A course or bearing as read from the ship's compass.
pub type CompassCourse = Direction<Compass>;
/// A bearing as read from the ship's compass. Alias of [`CompassCourse`].
pub type CompassBearing = Direction<Compass>;
/// A course as read from the gyrocompass.
pub type GyroCourse = Direction<Gyro>;
/// A bearing as read from the gyrocompass. Alias of [`GyroCourse`].
pub type GyroBearing = Direction<Gyro>;

impl<F: Frame> Direction<F> {
    /// Due north, `000°`.
    pub const NORTH: Self = Self::from_wrapped(0.0);
    /// Due east, `090°`.
    pub const EAST: Self = Self::from_wrapped(90.0);
    /// Due south, `180°`.
    pub const SOUTH: Self = Self::from_wrapped(180.0);
    /// Due west, `270°`.
    pub const WEST: Self = Self::from_wrapped(270.0);

    /// Builds a direction from a value already known to lie in `[0.0, 360.0)`.
    const fn from_wrapped(degrees: f64) -> Self {
        Self {
            degrees,
            frame: PhantomData,
        }
    }

    /// Wraps a value that is already known to be finite.
    ///
    /// Used inside the crate where the inputs are validated newtypes, so the
    /// result cannot be `NaN` and no `Result` is needed.
    pub(crate) fn from_degrees_wrapped(degrees: f64) -> Self {
        Self::from_wrapped(wrap360(degrees))
    }

    /// Creates a direction from a reading in `[0.0, 360.0]`, mapping `360.0` to `0.0`.
    ///
    /// Values outside that interval are rejected rather than wrapped, because a
    /// course of `400°` is far more likely to be a typo or a unit mix-up than a
    /// deliberate way of writing `040°`. Use [`Direction::wrap`] when wrapping is
    /// what you actually mean.
    ///
    /// # Errors
    ///
    /// Returns [`NavigationError::NotFinite`] for `NaN` or an infinity, and
    /// [`NavigationError::OutOfRange`] for anything outside `[0.0, 360.0]`.
    pub fn new(degrees: f64) -> Result<Self> {
        ensure_range("course", degrees, 0.0, 360.0)?;
        Ok(Self::from_wrapped(wrap360(degrees)))
    }

    /// Creates a direction from any finite value, normalising it into `[0.0, 360.0)`.
    ///
    /// # Errors
    ///
    /// Returns [`NavigationError::NotFinite`] for `NaN` or an infinity.
    pub fn wrap(degrees: f64) -> Result<Self> {
        ensure_finite("course", degrees)?;
        Ok(Self::from_wrapped(wrap360(degrees)))
    }

    /// The direction in degrees, always finite and always in `[0.0, 360.0)`.
    #[must_use]
    pub const fn degrees(self) -> f64 {
        self.degrees
    }

    /// The direction in radians, in `[0.0, 2π)`.
    #[must_use]
    pub fn radians(self) -> f64 {
        math::to_radians(self.degrees)
    }

    /// The reciprocal direction, 180° away.
    #[must_use]
    pub fn reciprocal(self) -> Self {
        Self::from_wrapped(wrap360(self.degrees + 180.0))
    }

    /// Shifts the direction by `delta` degrees, wrapping the result.
    ///
    /// # Errors
    ///
    /// Returns [`NavigationError::NotFinite`] if `delta` is not finite.
    pub fn offset(self, delta: f64) -> Result<Self> {
        ensure_finite("delta", delta)?;
        Ok(Self::from_wrapped(wrap360(self.degrees + delta)))
    }

    /// Signed shortest angle from `self` to `other`, in `[-180.0, 180.0)`.
    ///
    /// Positive means `other` lies clockwise of `self`.
    #[must_use]
    pub fn signed_difference(self, other: Self) -> f64 {
        wrap180(other.degrees - self.degrees)
    }

    /// Unsigned shortest angle between two directions, in `[0.0, 180.0]`.
    #[must_use]
    pub fn angular_distance(self, other: Self) -> f64 {
        math::abs(self.signed_difference(other))
    }

    /// Re-labels the frame without changing the numeric value.
    ///
    /// Private on purpose: outside this crate a frame change must go through a
    /// conversion in [`crate::navigation_solutions`], which applies the correct
    /// variation or deviation.
    pub(crate) const fn relabel<G: Frame>(self) -> Direction<G> {
        Direction::from_wrapped(self.degrees)
    }
}

impl<F: Frame> fmt::Display for Direction<F> {
    /// Formats as `045.0°T`, the way a course is written on a chart.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let precision = f.precision().unwrap_or(1);
        write!(
            f,
            "{:0>width$.precision$}°{}",
            self.degrees,
            F::SUFFIX,
            width = if precision == 0 { 3 } else { precision + 4 },
        )
    }
}

impl<F: Frame> fmt::Debug for Direction<F> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}({}°)", F::NAME, self.degrees)
    }
}

/// Magnetic variation: the angle from true north to magnetic north.
///
/// Positive is easterly. True course = magnetic course + variation.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(try_from = "f64", into = "f64")
)]
pub struct Variation(f64);

impl Variation {
    /// No variation.
    pub const ZERO: Self = Self(0.0);

    /// Creates a variation from a value in `[-180.0, 180.0]` degrees.
    ///
    /// The old API accepted `±360°`, which is not a physically meaningful
    /// variation; anything beyond a half turn is now rejected.
    ///
    /// # Errors
    ///
    /// Returns [`NavigationError::NotFinite`] for `NaN` or an infinity, and
    /// [`NavigationError::OutOfRange`] outside `[-180.0, 180.0]`.
    pub fn new(degrees: f64) -> Result<Self> {
        ensure_range("variation", degrees, -MAX_VARIATION_DEG, MAX_VARIATION_DEG)?;
        Ok(Self(degrees))
    }

    /// The variation in degrees, positive easterly.
    #[must_use]
    pub const fn degrees(self) -> f64 {
        self.0
    }
}

impl fmt::Display for Variation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let precision = f.precision().unwrap_or(1);
        let hemisphere = if self.0 < 0.0 { 'W' } else { 'E' };
        write!(f, "{:.precision$}°{hemisphere}", math::abs(self.0))
    }
}

/// Compass deviation: the angle from magnetic north to compass north.
///
/// Positive is easterly. Magnetic course = compass course + deviation.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(try_from = "f64", into = "f64")
)]
pub struct Deviation(f64);

impl Deviation {
    /// No deviation.
    pub const ZERO: Self = Self(0.0);

    /// Creates a deviation from a value in `[-180.0, 180.0]` degrees.
    ///
    /// # Errors
    ///
    /// Returns [`NavigationError::NotFinite`] for `NaN` or an infinity, and
    /// [`NavigationError::OutOfRange`] outside `[-180.0, 180.0]`.
    pub fn new(degrees: f64) -> Result<Self> {
        ensure_range("deviation", degrees, -MAX_DEVIATION_DEG, MAX_DEVIATION_DEG)?;
        Ok(Self(degrees))
    }

    /// The deviation in degrees, positive easterly.
    #[must_use]
    pub const fn degrees(self) -> f64 {
        self.0
    }
}

impl fmt::Display for Deviation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let precision = f.precision().unwrap_or(1);
        let hemisphere = if self.0 < 0.0 { 'W' } else { 'E' };
        write!(f, "{:.precision$}°{hemisphere}", math::abs(self.0))
    }
}

/// Which side of the bow something lies on.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Side {
    /// Dead ahead, within a rounding tolerance.
    Ahead,
    /// To starboard, `000°` to `180°` relative.
    Starboard,
    /// Dead astern, within a rounding tolerance.
    Astern,
    /// To port, `180°` to `360°` relative.
    Port,
}

/// A bearing measured clockwise from the ship's head, in `[0°, 360°)`.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Default)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Serialize, serde::Deserialize),
    serde(try_from = "f64", into = "f64")
)]
pub struct RelativeBearing(f64);

impl RelativeBearing {
    /// Dead ahead.
    pub const AHEAD: Self = Self(0.0);
    /// Right abeam.
    pub const ABEAM_STARBOARD: Self = Self(90.0);
    /// Dead astern.
    pub const ASTERN: Self = Self(180.0);
    /// Left abeam.
    pub const ABEAM_PORT: Self = Self(270.0);

    /// Creates a relative bearing from a value in `[0.0, 360.0]`, mapping `360.0` to `0.0`.
    ///
    /// # Errors
    ///
    /// Returns [`NavigationError::NotFinite`] for `NaN` or an infinity, and
    /// [`NavigationError::OutOfRange`] outside `[0.0, 360.0]`.
    pub fn new(degrees: f64) -> Result<Self> {
        ensure_range("relative bearing", degrees, 0.0, 360.0)?;
        Ok(Self(wrap360(degrees)))
    }

    /// Creates a relative bearing from any finite value, normalising it into `[0.0, 360.0)`.
    ///
    /// # Errors
    ///
    /// Returns [`NavigationError::NotFinite`] for `NaN` or an infinity.
    pub fn wrap(degrees: f64) -> Result<Self> {
        ensure_finite("relative bearing", degrees)?;
        Ok(Self(wrap360(degrees)))
    }

    /// Wraps a value that is already known to be finite.
    pub(crate) fn from_degrees_wrapped(degrees: f64) -> Self {
        Self(wrap360(degrees))
    }

    /// The relative bearing in degrees, in `[0.0, 360.0)`.
    #[must_use]
    pub const fn degrees(self) -> f64 {
        self.0
    }

    /// The relative bearing as a signed angle in `[-180.0, 180.0)`.
    ///
    /// Positive is to starboard, negative to port.
    #[must_use]
    pub fn signed_degrees(self) -> f64 {
        wrap180(self.0)
    }

    /// Which side of the bow the object lies on.
    #[must_use]
    pub fn side(self) -> Side {
        const TOLERANCE: f64 = 1e-9;
        let signed = self.signed_degrees();
        if math::abs(signed) < TOLERANCE {
            Side::Ahead
        } else if math::abs(math::abs(signed) - 180.0) < TOLERANCE {
            Side::Astern
        } else if signed > 0.0 {
            Side::Starboard
        } else {
            Side::Port
        }
    }
}

impl fmt::Display for RelativeBearing {
    /// Formats as `030.0° green` / `045.0° red`, the way a relative bearing is reported.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let precision = f.precision().unwrap_or(1);
        let magnitude = math::abs(self.signed_degrees());
        match self.side() {
            Side::Ahead => write!(f, "dead ahead"),
            Side::Astern => write!(f, "dead astern"),
            Side::Starboard => write!(f, "{magnitude:.precision$}° green"),
            Side::Port => write!(f, "{magnitude:.precision$}° red"),
        }
    }
}

#[cfg(feature = "serde")]
impl TryFrom<f64> for Variation {
    type Error = NavigationError;

    /// Validates on the way in, so a stored value cannot be out of range.
    fn try_from(value: f64) -> Result<Self> {
        Self::new(value)
    }
}

#[cfg(feature = "serde")]
impl From<Variation> for f64 {
    fn from(value: Variation) -> Self {
        value.0
    }
}

#[cfg(feature = "serde")]
impl TryFrom<f64> for Deviation {
    type Error = NavigationError;

    /// Validates on the way in, so a stored value cannot be out of range.
    fn try_from(value: f64) -> Result<Self> {
        Self::new(value)
    }
}

#[cfg(feature = "serde")]
impl From<Deviation> for f64 {
    fn from(value: Deviation) -> Self {
        value.0
    }
}

#[cfg(feature = "serde")]
impl TryFrom<f64> for RelativeBearing {
    type Error = NavigationError;

    /// Validates on the way in, so a stored value cannot be out of range.
    fn try_from(value: f64) -> Result<Self> {
        Self::new(value)
    }
}

#[cfg(feature = "serde")]
impl From<RelativeBearing> for f64 {
    fn from(value: RelativeBearing) -> Self {
        value.0
    }
}

#[cfg(feature = "serde")]
impl<F: Frame> serde::Serialize for Direction<F> {
    /// Written as plain degrees; the frame lives in the type, not the data.
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> core::result::Result<S::Ok, S::Error> {
        serializer.serialize_f64(self.degrees)
    }
}

#[cfg(feature = "serde")]
impl<'de, F: Frame> serde::Deserialize<'de> for Direction<F> {
    /// Read back through [`Direction::new`], so a stored direction outside
    /// `[0°, 360°]` is rejected rather than trusted.
    fn deserialize<D: serde::Deserializer<'de>>(
        deserializer: D,
    ) -> core::result::Result<Self, D::Error> {
        let degrees = f64::deserialize(deserializer)?;
        Self::new(degrees).map_err(serde::de::Error::custom)
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::float_cmp, clippy::indexing_slicing)]
mod tests {
    use super::*;
    use alloc::format;

    #[test]
    fn wrap360_is_correct_below_minus_360() {
        // The old `(x + 360.0) % 360.0` returned -40.0 here.
        assert_eq!(wrap360(-400.0), 320.0);
        assert_eq!(wrap360(-720.0), 0.0);
        assert_eq!(wrap360(-0.0), 0.0);
        assert_eq!(wrap360(0.0), 0.0);
        assert_eq!(wrap360(360.0), 0.0);
        assert_eq!(wrap360(725.0), 5.0);
        assert!(wrap360(-1e15).is_finite());
    }

    #[test]
    fn wrap360_keeps_tiny_negatives_off_the_far_end() {
        // These round to exactly 360.0 when 360 is added naively.
        for value in [-1e-16, -1e-18, -f64::MIN_POSITIVE, -1e-14] {
            let wrapped = wrap360(value);
            assert!(
                (0.0..360.0).contains(&wrapped),
                "{value} wrapped to {wrapped}"
            );
        }
        assert_eq!(wrap360(-1e-16), 0.0);
        // A negative big enough to represent still lands just below 360.
        assert!(wrap360(-1e-10) < 360.0);
        assert!(wrap360(-1e-10) > 359.999);
    }

    #[test]
    fn wrap360_never_leaves_the_interval() {
        let mut value = -2000.0;
        while value < 2000.0 {
            let wrapped = wrap360(value);
            assert!((0.0..360.0).contains(&wrapped), "{value} -> {wrapped}");
            value += 0.37;
        }
    }

    #[test]
    fn wrap180_is_symmetric() {
        assert_eq!(wrap180(0.0), 0.0);
        assert_eq!(wrap180(90.0), 90.0);
        assert_eq!(wrap180(180.0), -180.0);
        assert_eq!(wrap180(190.0), -170.0);
        assert_eq!(wrap180(-190.0), 170.0);
    }

    #[test]
    fn direction_rejects_bad_input() {
        assert!(TrueCourse::new(f64::NAN).is_err());
        assert!(TrueCourse::new(f64::INFINITY).is_err());
        assert!(TrueCourse::new(-0.1).is_err());
        assert!(TrueCourse::new(400.0).is_err());
        assert!(TrueCourse::wrap(f64::NAN).is_err());
    }

    #[test]
    fn direction_normalises() {
        assert_eq!(TrueCourse::new(360.0).unwrap().degrees(), 0.0);
        assert_eq!(TrueCourse::wrap(-10.0).unwrap().degrees(), 350.0);
        assert_eq!(TrueCourse::wrap(730.0).unwrap().degrees(), 10.0);
    }

    #[test]
    fn reciprocal_round_trips() {
        for degrees in [0.0, 45.0, 179.0, 180.0, 359.9] {
            let direction = TrueCourse::new(degrees).unwrap();
            assert!((direction.reciprocal().reciprocal().degrees() - degrees).abs() < 1e-12);
        }
    }

    #[test]
    fn signed_difference_takes_the_short_way() {
        let a = TrueCourse::new(350.0).unwrap();
        let b = TrueCourse::new(10.0).unwrap();
        assert!((a.signed_difference(b) - 20.0).abs() < 1e-12);
        assert!((b.signed_difference(a) + 20.0).abs() < 1e-12);
        assert!((a.angular_distance(b) - 20.0).abs() < 1e-12);
    }

    #[test]
    fn variation_and_deviation_validate() {
        assert!(Variation::new(-181.0).is_err());
        assert!(Variation::new(f64::NAN).is_err());
        assert!(Variation::new(180.0).is_ok());
        assert!(Deviation::new(f64::INFINITY).is_err());
        assert_eq!(Deviation::ZERO.degrees(), 0.0);
    }

    #[test]
    fn relative_bearing_sides() {
        assert_eq!(RelativeBearing::new(0.0).unwrap().side(), Side::Ahead);
        assert_eq!(RelativeBearing::new(90.0).unwrap().side(), Side::Starboard);
        assert_eq!(RelativeBearing::new(180.0).unwrap().side(), Side::Astern);
        assert_eq!(RelativeBearing::new(270.0).unwrap().side(), Side::Port);
        assert!((RelativeBearing::new(270.0).unwrap().signed_degrees() + 90.0).abs() < 1e-12);
    }

    #[test]
    fn display_is_chart_style() {
        assert_eq!(format!("{}", TrueCourse::new(45.0).unwrap()), "045.0°T");
        assert_eq!(
            format!("{}", MagneticCourse::new(357.89).unwrap()),
            "357.9°M"
        );
        assert_eq!(format!("{}", Variation::new(-2.7).unwrap()), "2.7°W");
        assert_eq!(format!("{}", Deviation::new(1.5).unwrap()), "1.5°E");
        assert_eq!(
            format!("{}", RelativeBearing::new(300.0).unwrap()),
            "60.0° red"
        );
    }
}