native_solver/solver/
chips.rs1use std::fmt;
8use std::ops::{Add, AddAssign, Sub, SubAssign};
9
10pub const CHIP_SCALE: i64 = 1000;
12
13const MAX_UNITS: f64 = 9_007_199_254_740_992.0;
16
17#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
33pub struct Chips(pub(crate) i64);
34
35impl Chips {
36 pub const ZERO: Chips = Chips(0);
38
39 pub const fn whole(chips: i64) -> Chips {
45 match chips.checked_mul(CHIP_SCALE) {
46 Some(units) if units.unsigned_abs() <= 1 << 53 => Chips(units),
47 _ => panic!("Chips::whole: amount out of range"),
48 }
49 }
50
51 pub const fn from_units(units: i64) -> Chips {
54 Chips(units)
55 }
56
57 pub const fn units(self) -> i64 {
59 self.0
60 }
61
62 pub fn from_f64(amount: f64) -> Option<Chips> {
68 let scaled = amount * CHIP_SCALE as f64;
69 if !scaled.is_finite() || scaled.abs() > MAX_UNITS {
70 return None;
71 }
72 let units = scaled.round();
73 let error = (units / CHIP_SCALE as f64 - amount).abs();
74 (error <= 1e-9 || error <= 4.0 * f64::EPSILON * amount.abs()).then_some(Chips(units as i64))
75 }
76
77 pub fn to_f64(self) -> f64 {
79 self.0 as f64 / CHIP_SCALE as f64
80 }
81
82 pub fn fraction(self, fraction: f64) -> Chips {
86 Chips((self.0 as f64 * fraction).round_ties_even() as i64)
88 }
89}
90
91impl Add for Chips {
92 type Output = Chips;
93 fn add(self, rhs: Chips) -> Chips {
94 Chips(self.0 + rhs.0)
95 }
96}
97
98impl AddAssign for Chips {
99 fn add_assign(&mut self, rhs: Chips) {
100 self.0 += rhs.0;
101 }
102}
103
104impl Sub for Chips {
105 type Output = Chips;
106 fn sub(self, rhs: Chips) -> Chips {
107 Chips(self.0 - rhs.0)
108 }
109}
110
111impl SubAssign for Chips {
112 fn sub_assign(&mut self, rhs: Chips) {
113 self.0 -= rhs.0;
114 }
115}
116
117impl fmt::Display for Chips {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 let sign = if self.0 < 0 { "-" } else { "" };
122 let units = self.0.unsigned_abs();
123 let scale = CHIP_SCALE as u64;
124 let (whole, frac) = (units / scale, units % scale);
125 if frac == 0 {
126 return write!(f, "{sign}{whole}");
127 }
128 let digits = format!("{frac:03}");
129 write!(f, "{sign}{whole}.{}", digits.trim_end_matches('0'))
130 }
131}
132
133#[cfg(test)]
134mod tests {
135 use super::*;
136
137 #[test]
138 fn whole_and_units_round_trip_within_the_exact_range() {
139 assert_eq!(Chips::whole(7).units(), 7 * CHIP_SCALE);
140 assert_eq!(Chips::from_units(CHIP_SCALE), Chips::whole(1));
141 let largest = (1i64 << 53) / CHIP_SCALE;
142 assert_eq!(Chips::whole(largest).units(), largest * CHIP_SCALE);
143 assert_eq!(Chips::whole(-largest).units(), -largest * CHIP_SCALE);
144 }
145
146 #[test]
147 #[should_panic(expected = "Chips::whole: amount out of range")]
148 fn whole_rejects_amounts_beyond_the_exact_range() {
149 let _ = Chips::whole(i64::MAX / CHIP_SCALE);
150 }
151
152 #[test]
153 fn from_f64_accepts_thousandths_and_rejects_finer_amounts() {
154 assert_eq!(Chips::from_f64(5.5), Some(Chips(5500)));
155 assert_eq!(Chips::from_f64(0.001), Some(Chips(1)));
156 assert_eq!(Chips::from_f64(0.1 + 0.2), Some(Chips(300)));
157 assert_eq!(Chips::from_f64(1e9), Some(Chips::whole(1_000_000_000)));
158 assert_eq!(
159 Chips::from_f64(123_456_789.123),
160 Some(Chips(123_456_789_123))
161 );
162 assert_eq!(Chips::from_f64(0.0005), None);
163 assert_eq!(Chips::from_f64(1e-6), None);
164 assert_eq!(Chips::from_f64(1_000_000.000_1), None);
165 assert_eq!(Chips::from_f64(f64::NAN), None);
166 assert_eq!(Chips::from_f64(f64::INFINITY), None);
167 assert_eq!(Chips::from_f64(f64::MAX), None);
168 }
169
170 #[test]
171 fn display_is_exact_and_trims_trailing_zeros() {
172 assert_eq!(Chips(1815).to_string(), "1.815");
173 assert_eq!(Chips(500).to_string(), "0.5");
174 assert_eq!(Chips(1).to_string(), "0.001");
175 assert_eq!(Chips::whole(100).to_string(), "100");
176 assert_eq!(Chips(-2050).to_string(), "-2.05");
177 assert_ne!(Chips(1814).to_string(), Chips(1815).to_string());
178 }
179
180 #[test]
181 fn fraction_rounds_half_to_even() {
182 assert_eq!(Chips(5500).fraction(0.33), Chips(1815));
183 assert_eq!(Chips(5).fraction(0.5), Chips(2));
184 assert_eq!(Chips(7).fraction(0.5), Chips(4));
185 assert_eq!(Chips(100).fraction(f64::NAN), Chips::ZERO);
186 }
187}