Skip to main content

hydr_structures/structures/blockage/
fraction.rs

1//! Checked blocked share of a declared unblocked opening.
2
3use crate::structures::StructureError;
4
5/// Blocked share `f` of a declared unblocked opening, in `[0, 1)`.
6///
7/// A fully blocked structure is [`StructureCondition::Closed`](super::StructureCondition::Closed),
8/// never a fraction of one.
9#[derive(Clone, Copy, Debug, PartialEq)]
10pub struct BlockageFraction(f64);
11
12impl BlockageFraction {
13    /// No blockage. Every effective value keeps the bits of its base value.
14    pub const NONE: Self = Self(0.0);
15
16    /// Checks a finite blocked share with `0 <= f < 1`. `-0.0` becomes `+0.0`.
17    pub fn new(blocked_share: f64) -> Result<Self, StructureError> {
18        if !blocked_share.is_finite() || !(0.0..1.0).contains(&blocked_share) {
19            return Err(StructureError::definition(format!(
20                "blocked_share={blocked_share}: supply a finite blocked share with 0 <= f < 1. Declare StructureCondition::Closed for a fully blocked structure"
21            )));
22        }
23        Ok(Self(blocked_share + 0.0))
24    }
25
26    /// Blocked share `f`.
27    pub fn blocked_share(self) -> f64 {
28        self.0
29    }
30
31    /// Open share `fl(1 - f)`, in `(0, 1]`.
32    pub fn open_share(self) -> f64 {
33        1.0 - self.0
34    }
35
36    /// Effective value `fl(x ยท fl(1 - f))` of a base value `x`.
37    pub(crate) fn apply(self, base: f64) -> f64 {
38        base * self.open_share()
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use super::BlockageFraction;
45
46    #[test]
47    fn negative_zero_becomes_positive_zero() -> Result<(), Box<dyn std::error::Error>> {
48        let fraction = BlockageFraction::new(-0.0)?;
49        assert_eq!(fraction.blocked_share().to_bits(), 0.0_f64.to_bits());
50        assert_eq!(fraction, BlockageFraction::NONE);
51        Ok(())
52    }
53
54    #[test]
55    fn zero_fraction_keeps_base_bits() -> Result<(), Box<dyn std::error::Error>> {
56        let none = BlockageFraction::new(0.0)?;
57        for base in [f64::MIN_POSITIVE, 1e-300, 0.1, 1.0, 3.7, 1e300, f64::MAX] {
58            assert_eq!(none.apply(base).to_bits(), base.to_bits());
59        }
60        Ok(())
61    }
62
63    #[test]
64    fn domain_outside_half_open_unit_rejects() {
65        for value in [1.0, 1.5, -0.1, f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
66            let error = BlockageFraction::new(value).err();
67            let message = error.map(|error| error.to_string()).unwrap_or_default();
68            assert!(
69                message.contains("blocked_share=") && message.contains("Closed"),
70                "value={value}: {message}"
71            );
72        }
73    }
74
75    #[test]
76    fn largest_share_below_one_admits() -> Result<(), Box<dyn std::error::Error>> {
77        let fraction = BlockageFraction::new(1.0 - f64::EPSILON / 2.0)?;
78        assert!(fraction.open_share() > 0.0);
79        Ok(())
80    }
81}