Skip to main content

deep_causality_physics/units/
probability.rs

1/*
2 * SPDX-License-Identifier: MIT
3 * Copyright (c) 2023 - 2026. The DeepCausality Authors and Contributors. All Rights Reserved.
4 */
5use crate::PhysicsError;
6
7/// Probability value [0, 1].
8#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
9pub struct Probability(f64);
10
11impl Default for Probability {
12    fn default() -> Self {
13        Self(0.0)
14    }
15}
16
17impl Probability {
18    /// Creates a new `Probability` instance.
19    ///
20    /// # Errors
21    /// Returns `PhysicsError::NormalizationError` if `val` is not in [0, 1].
22    pub fn new(val: f64) -> Result<Self, PhysicsError> {
23        if !(0.0..=1.0).contains(&val) {
24            return Err(PhysicsError::NormalizationError(format!(
25                "Probability must be between 0 and 1, got {}",
26                val
27            )));
28        }
29        Ok(Self(val))
30    }
31    pub fn new_unchecked(val: f64) -> Self {
32        Self(val)
33    }
34    pub fn value(&self) -> f64 {
35        self.0
36    }
37}
38
39impl From<Probability> for f64 {
40    fn from(val: Probability) -> Self {
41        val.0
42    }
43}