From 98fbd68ee21e273f9412b70bc29d2f309c4088a6 Mon Sep 17 00:00:00 2001 From: Ryan Pitasky Date: Sat, 20 Jul 2024 18:13:32 -0400 Subject: [PATCH] 2.3.0: better test cases & debug display --- .gitignore | 4 +++- Cargo.toml | 2 +- src/float.rs | 21 +++++++++++++++++++-- src/mantissa.rs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index a3d2906..002168d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ /Cargo.lock flamegraph.svg -perf.data* \ No newline at end of file +perf.data* + +.idea \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index c6f4a08..901b877 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tifloats" -version = "2.2.0" +version = "2.3.0" edition = "2021" license = "MIT OR Apache-2.0" homepage = "https://github.com/TI-Toolkit/tifloats_lib_rs" diff --git a/src/float.rs b/src/float.rs index 9c1cf50..6e26b5f 100644 --- a/src/float.rs +++ b/src/float.rs @@ -2,7 +2,7 @@ use std::{ cmp::Ordering, ops::{Add, Div, Mul, Neg, Sub}, }; - +use std::fmt::{Debug, Formatter}; use crate::mantissa::Mantissa; use bitflags::bitflags; @@ -45,7 +45,7 @@ pub enum ParseFloatError { InvalidMantissa, } -#[derive(PartialEq, Eq, Copy, Clone, Debug)] +#[derive(PartialEq, Eq, Copy, Clone)] #[repr(C)] pub struct Float { flags: Flags, @@ -296,6 +296,16 @@ impl Div for Float { } } +impl Debug for Float { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + if self.flags.contains(Flags::NEGATIVE) { + f.write_str("-")?; + } + + f.write_str(&format!("0x{} * 10 ^ {}", self.mantissa.to_dec().to_string(), (self.exponent as i8).wrapping_add(Float::EXPONENT_NORM as i8))) + } +} + #[cfg(test)] mod tests { use super::*; @@ -361,4 +371,11 @@ mod tests { assert_eq!(Float::mantissa_from(&digits), expected); } } + + #[test] + fn debug() { + assert_eq!("0x50000000000000 * 10 ^ 5", format!("{:?}", tifloat!(0x50000000000000 * 10 ^ 5))); + assert_eq!("-0x50000000000000 * 10 ^ 5", format!("{:?}", tifloat!(-0x50000000000000 * 10 ^ 5))); + assert_eq!("0x50000000000000 * 10 ^ -5", format!("{:?}", tifloat!(0x50000000000000 * 10 ^ -5))); + } } diff --git a/src/mantissa.rs b/src/mantissa.rs index 8ffbf37..d32200d 100644 --- a/src/mantissa.rs +++ b/src/mantissa.rs @@ -295,6 +295,49 @@ mod tests { #[test] fn div() { - // don't care enough, just trust me- it works + assert_eq!( + Mantissa { + data: 0x6000000000000000 + } + .overflowing_div(Mantissa { + data: 0x7000000000000000 + }), + ( + Mantissa { + data: 0x0085714285714286 + }, + false + ) + ); + + assert_eq!( + Mantissa { + data: 0x1000000000000000 + } + .overflowing_div(Mantissa { + data: 0x3000000000000000 + }), + ( + Mantissa { + data: 0x0033333333333333 + }, + false + ) + ); + + assert_eq!( + Mantissa { + data: 0x0355000000000000 + } + .overflowing_div(Mantissa { + data: 0x1130000000000000 + }), + ( + Mantissa { + data: 0x0031415929203540 + }, + false + ) + ); } }