sim_lib_discrete_algebra/
real.rs1use crate::semiring::Semiring;
5
6#[derive(Debug, Clone, Copy, PartialEq)]
12pub struct RealF64(pub f64);
13
14impl RealF64 {
15 pub fn new(value: f64) -> Self {
17 RealF64(value)
18 }
19 pub fn total_cmp(&self, other: &Self) -> core::cmp::Ordering {
22 self.0.total_cmp(&other.0)
23 }
24}
25
26impl Semiring for RealF64 {
27 fn zero() -> Self {
28 RealF64(0.0)
29 }
30 fn one() -> Self {
31 RealF64(1.0)
32 }
33 fn add(&self, other: &Self) -> Self {
34 RealF64(self.0 + other.0)
35 }
36 fn mul(&self, other: &Self) -> Self {
37 RealF64(self.0 * other.0)
38 }
39 fn is_zero(&self) -> bool {
40 self.0 == 0.0
41 }
42 }
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49 use crate::semiring::laws::assert_semiring_laws;
50 use core::cmp::Ordering;
51
52 #[test]
53 fn laws_hold() {
54 assert_semiring_laws(&[RealF64(0.0), RealF64(1.0), RealF64(2.0), RealF64(4.0)]);
56 }
57
58 #[test]
59 fn total_order_is_total() {
60 assert_eq!(RealF64(1.0).total_cmp(&RealF64(2.0)), Ordering::Less);
61 assert_eq!(RealF64(2.0).total_cmp(&RealF64(2.0)), Ordering::Equal);
62 assert_eq!(RealF64::zero().star(), None);
63 }
64}