Skip to content

Commit

Permalink
Add cast and normalize to WDLAbs.
Browse files Browse the repository at this point in the history
  • Loading branch information
KarelPeeters committed Jan 23, 2024
1 parent 73e30f0 commit 1461db2
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/wdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,17 @@ impl<V> WDLAbs<V> {
pub fn new(win_a: V, draw: V, win_b: V) -> Self {
Self { win_a, draw, win_b }
}

pub fn cast<W>(self) -> WDLAbs<W>
where
V: Cast<W>,
{
WDLAbs {
win_a: self.win_a.cast(),
draw: self.draw.cast(),
win_b: self.win_b.cast(),
}
}
}

impl<V> WDL<V> {
Expand All @@ -170,9 +181,21 @@ impl<V> WDL<V> {
pub fn to_slice(self) -> [V; 3] {
[self.win, self.draw, self.loss]
}

pub fn cast<W>(self) -> WDL<W>
where
V: Cast<W>,
{
WDL {
win: self.win.cast(),
draw: self.draw.cast(),
loss: self.loss.cast(),
}
}
}

impl<V: num_traits::Float> WDL<V> {
#[must_use]
pub fn nan() -> WDL<V> {
WDL {
win: V::nan(),
Expand All @@ -187,13 +210,19 @@ impl<V: num_traits::Float> WDL<V> {
}

impl<V: num_traits::Float> WDLAbs<V> {
#[must_use]
pub fn nan() -> WDLAbs<V> {
WDLAbs {
win_a: V::nan(),
draw: V::nan(),
win_b: V::nan(),
}
}

#[must_use]
pub fn normalized(self) -> WDLAbs<V> {
self / self.sum()
}
}

impl<V: num_traits::One + Default + PartialEq> WDLAbs<V> {
Expand All @@ -210,19 +239,6 @@ impl<V: num_traits::One + Default + PartialEq> WDL<V> {
}
}

impl<V: Copy> WDL<V> {
pub fn cast<W>(self) -> WDL<W>
where
V: Cast<W>,
{
WDL {
win: self.win.cast(),
draw: self.draw.cast(),
loss: self.loss.cast(),
}
}
}

impl<V: Copy + std::ops::Sub<V, Output = V>> WDL<V> {
pub fn value(self) -> V {
self.win - self.loss
Expand Down

0 comments on commit 1461db2

Please sign in to comment.