deep_causality_physics/units/
probability.rs1use crate::PhysicsError;
6
7#[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 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}