use crate::semiring::Semiring;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RealF64(pub f64);
impl RealF64 {
pub fn new(value: f64) -> Self {
RealF64(value)
}
pub fn total_cmp(&self, other: &Self) -> core::cmp::Ordering {
self.0.total_cmp(&other.0)
}
}
impl Semiring for RealF64 {
fn zero() -> Self {
RealF64(0.0)
}
fn one() -> Self {
RealF64(1.0)
}
fn add(&self, other: &Self) -> Self {
RealF64(self.0 + other.0)
}
fn mul(&self, other: &Self) -> Self {
RealF64(self.0 * other.0)
}
fn is_zero(&self) -> bool {
self.0 == 0.0
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::semiring::laws::assert_semiring_laws;
use core::cmp::Ordering;
#[test]
fn laws_hold() {
assert_semiring_laws(&[RealF64(0.0), RealF64(1.0), RealF64(2.0), RealF64(4.0)]);
}
#[test]
fn total_order_is_total() {
assert_eq!(RealF64(1.0).total_cmp(&RealF64(2.0)), Ordering::Less);
assert_eq!(RealF64(2.0).total_cmp(&RealF64(2.0)), Ordering::Equal);
assert_eq!(RealF64::zero().star(), None);
}
}