Struct stochasta::Probability
source · pub struct Probability { /* private fields */ }
Expand description
A probability is a rational number (ℚ) in the range of 0 and 1 (both inclusive).
In other words: if you got a variable p
from type Probability
you can be sure about the following: 0 <= p && p <= 1
.
Implementations§
source§impl Probability
impl Probability
sourcepub fn new(numerator: u64, denominator: u64) -> Self
pub fn new(numerator: u64, denominator: u64) -> Self
Creates a new Probability
.
For a safer method (panic-free), please consider using: Probability::try_new
.
§Panics
- if numerator > denominator ⇒ ratio > 1 ⇒ value out of bounds!
- if denominator == 0 ⇒ impossible value!
§Example
use num_rational::Ratio;
use stochasta::Probability;
let p = Probability::new(1, 3);
assert_eq!(p.ratio(), &Ratio::new(1, 3));
sourcepub fn try_new(
numerator: u64,
denominator: u64
) -> Result<Self, ProbabilityRatioError>
pub fn try_new( numerator: u64, denominator: u64 ) -> Result<Self, ProbabilityRatioError>
Tries to create a new Probability
from the given ratio.
§Errors
- ratio > 1 => value out of bounds!
use num_rational::Ratio;
use stochasta::{Probability, ProbabilityRatioError};
assert!(Probability::try_new(1, 2).is_ok());
assert_eq!(Probability::try_new(1, 0), Err(ProbabilityRatioError::DenominatorZero));
assert_eq!(Probability::try_new(2, 1), Err(ProbabilityRatioError::RatioGreaterOne));
sourcepub fn complementary(&self) -> Self
pub fn complementary(&self) -> Self
Returns the complementary probability: 1 - self
.
§Example
use stochasta::Probability;
let one_third = Probability::new(1, 3);
let two_third = one_third.complementary();
assert_eq!(two_third, Probability::new(2, 3));
Trait Implementations§
source§impl Clone for Probability
impl Clone for Probability
source§fn clone(&self) -> Probability
fn clone(&self) -> Probability
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for Probability
impl Debug for Probability
source§impl Default for Probability
impl Default for Probability
source§impl<'de> Deserialize<'de> for Probability
impl<'de> Deserialize<'de> for Probability
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl Display for Probability
impl Display for Probability
source§impl Hash for Probability
impl Hash for Probability
source§impl Mul for Probability
impl Mul for Probability
§type Output = Probability
type Output = Probability
The resulting type after applying the
*
operator.source§impl Ord for Probability
impl Ord for Probability
source§fn cmp(&self, other: &Probability) -> Ordering
fn cmp(&self, other: &Probability) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl PartialEq for Probability
impl PartialEq for Probability
source§fn eq(&self, other: &Probability) -> bool
fn eq(&self, other: &Probability) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for Probability
impl PartialOrd for Probability
source§fn partial_cmp(&self, other: &Probability) -> Option<Ordering>
fn partial_cmp(&self, other: &Probability) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl Serialize for Probability
impl Serialize for Probability
impl Copy for Probability
impl Eq for Probability
impl StructuralPartialEq for Probability
Auto Trait Implementations§
impl Freeze for Probability
impl RefUnwindSafe for Probability
impl Send for Probability
impl Sync for Probability
impl Unpin for Probability
impl UnwindSafe for Probability
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more