Spaces:
Build error
Build error
File size: 1,571 Bytes
f832ee5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | use crate::{QANTO_SCALE, SCORE_SCALE};
/// Deterministic multiplication for fixed-point math with SCORE_SCALE (i128).
/// Formula: (a * b) / SCORE_SCALE
pub fn mul_scale(a: i128, b: i128) -> i128 {
a.checked_mul(b)
.map(|res| res / SCORE_SCALE)
.unwrap_or(0)
}
/// Deterministic multiplication for fixed-point math with QANTO_SCALE (u128).
/// Formula: (a * b) / QANTO_SCALE
pub fn mul_scale_u128(a: u128, b: u128) -> u128 {
a.checked_mul(b)
.map(|res| res / QANTO_SCALE)
.unwrap_or(0)
}
/// Deterministic division for fixed-point math with SCORE_SCALE (i128).
/// Formula: (a * SCORE_SCALE) / b
pub fn div_scale(a: i128, b: i128) -> i128 {
if b == 0 {
return 0;
}
a.checked_mul(SCORE_SCALE)
.map(|res| res / b)
.unwrap_or(0)
}
/// Deterministic division for fixed-point math with QANTO_SCALE (u128).
/// Formula: (a * QANTO_SCALE) / b
pub fn div_scale_u128(a: u128, b: u128) -> u128 {
if b == 0 {
return 0;
}
a.checked_mul(QANTO_SCALE)
.map(|res| res / b)
.unwrap_or(0)
}
/// Saturating multiplication for fixed-point math to prevent overflow.
pub fn saturating_mul_scale(a: i128, b: i128) -> i128 {
a.saturating_mul(b) / SCORE_SCALE
}
/// Deterministic integer square root for u128.
/// Useful for distance and standard deviation calculations.
pub fn integer_sqrt(n: u128) -> u128 {
if n == 0 {
return 0;
}
let mut x = n;
let mut y = (x + 1) / 2;
while y < x {
x = y;
y = (x + n / x) / 2;
}
x
}
|