mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-11 06:39:08 -07:00
computer: stateful: perf improvements
This commit is contained in:
@@ -18,7 +18,7 @@ impl AddressData {
|
||||
}
|
||||
|
||||
pub fn realized_price(&self) -> Dollars {
|
||||
(self.realized_cap / Bitcoin::from(self.amount())).round_nearest_cent()
|
||||
(self.realized_cap / Bitcoin::from(self.amount())).round_to_4_digits()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
||||
@@ -24,10 +24,28 @@ use super::Dollars;
|
||||
)]
|
||||
pub struct Cents(i64);
|
||||
|
||||
const SIGNIFICANT_DIGITS: i32 = 4;
|
||||
|
||||
impl Cents {
|
||||
pub const fn mint(value: i64) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
|
||||
pub fn round_to_4_digits(self) -> Self {
|
||||
let v = self.0;
|
||||
|
||||
let ilog10 = v.checked_ilog10().unwrap_or(0) as i32;
|
||||
|
||||
Self::from(if ilog10 >= SIGNIFICANT_DIGITS {
|
||||
let log_diff = ilog10 - SIGNIFICANT_DIGITS + 1;
|
||||
|
||||
let pow = 10.0_f64.powi(log_diff);
|
||||
|
||||
((v as f64 / pow).round() * pow) as i64
|
||||
} else {
|
||||
v
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Dollars> for Cents {
|
||||
|
||||
@@ -42,6 +42,10 @@ impl Dollars {
|
||||
pub fn round_nearest_cent(self) -> Self {
|
||||
Dollars((self.0 * 100.0).round() / 100.0)
|
||||
}
|
||||
|
||||
pub fn round_to_4_digits(self) -> Self {
|
||||
Self::from(Cents::from(self).round_to_4_digits())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<f32> for Dollars {
|
||||
|
||||
Reference in New Issue
Block a user