removed Float; cleanup

This commit is contained in:
2025-07-18 21:19:30 +01:00
parent f9b8215596
commit bf11389ddf

View File

@@ -27,38 +27,10 @@ impl Display for DecimalError {
}
}
#[derive(Debug, Default, Clone, Copy)]
pub struct Float(f64);
impl Float {
pub fn add(left: Float, right: Float) -> Float {
Float(left.0 + right.0)
}
pub fn sub(left: Float, right: Float) -> Float {
Float(left.0 - right.0)
}
pub fn mul(left: Float, right: Float) -> Float {
Float(left.0 * right.0)
}
pub fn div(left: Float, right: Float) -> Float {
Float(left.0 / right.0)
}
}
impl From<f64> for Float {
fn from(f: f64) -> Float {
Float(f)
}
}
impl<const S: usize, E: PrimInt + Unsigned> TryFrom<Float> for Decimal<S, E> {
impl<const S: usize, E: PrimInt + Unsigned> TryFrom<f64> for Decimal<S, E> {
type Error = DecimalError;
fn try_from(val: Float) -> Result<Self, Self::Error> {
let mut f = val.0;
fn try_from(mut f: f64) -> Result<Self, Self::Error> {
if !f.is_finite() {
return Err(DecimalError::NotANumber);
}
@@ -99,47 +71,13 @@ impl<const S: usize, E: PrimInt + Unsigned> TryFrom<Float> for Decimal<S, E> {
}
}
#[derive(Default)]
pub struct SciNum {
significant: f64,
exponent: i8,
}
impl SciNum {
pub fn new(significant: f64, exponent: i8) -> SciNum {
let mut out = SciNum {
significant,
exponent,
};
out.normalize();
out
}
pub fn normalize(&mut self) {
while self.significant > 10.0 {
self.significant /= 10.0;
self.exponent = self.exponent.checked_add(1).expect("exponent overflow");
}
while self.significant < 0.0 {
self.significant *= 10.0;
self.exponent = self.exponent.checked_sub(1).expect("exponent underflow");
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_works() {
SciNum::new(1.2, 10);
}
#[test]
fn float_to_decimal_zero() {
let dec = Decimal::<5>::try_from(Float::from(0.0)).unwrap();
let dec = Decimal::<5>::try_from(0.0).unwrap();
assert!(!dec.minus);
assert_eq!(dec.significant, [0, 0, 0, 0, 0]);
assert!(!dec.minus_exponent);
@@ -148,7 +86,7 @@ mod tests {
#[test]
fn float_to_decimal_pos_big() {
let dec = Decimal::<7>::try_from(Float::from(1337.42)).unwrap();
let dec = Decimal::<7>::try_from(1337.42).unwrap();
assert!(!dec.minus);
assert_eq!(dec.significant, [1, 3, 3, 7, 4, 2, 0]);
assert!(!dec.minus_exponent);
@@ -157,7 +95,7 @@ mod tests {
#[test]
fn float_to_decimal_pos_small() {
let dec = Decimal::<7>::try_from(Float::from(0.0133742)).unwrap();
let dec = Decimal::<7>::try_from(0.0133742).unwrap();
assert!(!dec.minus);
assert_eq!(dec.significant, [1, 3, 3, 7, 4, 2, 0]);
assert!(dec.minus_exponent);
@@ -166,7 +104,7 @@ mod tests {
#[test]
fn float_to_decimal_neg_big() {
let dec = Decimal::<7>::try_from(Float::from(-1337.42)).unwrap();
let dec = Decimal::<7>::try_from(-1337.42).unwrap();
assert!(dec.minus);
assert_eq!(dec.significant, [1, 3, 3, 7, 4, 2, 0]);
assert!(!dec.minus_exponent);
@@ -175,7 +113,7 @@ mod tests {
#[test]
fn float_to_decimal_neg_small() {
let dec = Decimal::<7>::try_from(Float::from(-0.0133742)).unwrap();
let dec = Decimal::<7>::try_from(-0.0133742).unwrap();
assert!(dec.minus);
assert_eq!(dec.significant, [1, 3, 3, 7, 4, 2, 0]);
assert!(dec.minus_exponent);
@@ -184,13 +122,13 @@ mod tests {
#[test]
fn float_to_decimal_nan() {
let err = Decimal::<7>::try_from(Float::from(f64::NAN)).unwrap_err();
let err = Decimal::<7>::try_from(f64::NAN).unwrap_err();
assert!(matches!(err, DecimalError::NotANumber));
}
#[test]
fn float_to_decimal_max() {
let dec = Decimal::<7>::try_from(Float::from(f64::MAX)).unwrap();
let dec = Decimal::<7>::try_from(f64::MAX).unwrap();
assert!(!dec.minus);
assert_eq!(dec.significant, [1, 7, 9, 7, 6, 9, 3]);
assert!(!dec.minus_exponent);
@@ -199,13 +137,13 @@ mod tests {
#[test]
fn float_to_decimal_max_overflow() {
let err = Decimal::<7, u8>::try_from(Float::from(f64::MAX)).unwrap_err();
let err = Decimal::<7, u8>::try_from(f64::MAX).unwrap_err();
assert!(matches!(err, DecimalError::ExponentOverflow));
}
#[test]
fn float_to_decimal_min() {
let dec = Decimal::<7>::try_from(Float::from(f64::MIN)).unwrap();
let dec = Decimal::<7>::try_from(f64::MIN).unwrap();
assert!(dec.minus);
assert_eq!(dec.significant, [1, 7, 9, 7, 6, 9, 3]);
assert!(!dec.minus_exponent);
@@ -214,13 +152,13 @@ mod tests {
#[test]
fn float_to_decimal_min_overflow() {
let err = Decimal::<7, u8>::try_from(Float::from(f64::MIN)).unwrap_err();
let err = Decimal::<7, u8>::try_from(f64::MIN).unwrap_err();
assert!(matches!(err, DecimalError::ExponentOverflow));
}
#[test]
fn float_to_decimal_epsilon() {
let dec = Decimal::<7>::try_from(Float::from(f64::EPSILON)).unwrap();
let dec = Decimal::<7>::try_from(f64::EPSILON).unwrap();
assert!(!dec.minus);
assert_eq!(dec.significant, [2, 2, 2, 0, 4, 4, 6]);
assert!(dec.minus_exponent);