From 9fa2793b24c748ad40a0771f2893d1b6d1fa9d96 Mon Sep 17 00:00:00 2001 From: Ryan Pitasky Date: Sun, 18 Aug 2024 19:06:30 -0400 Subject: [PATCH] Add digits and significant figures getters --- Cargo.toml | 2 +- src/float.rs | 50 +++++++++++++++++++++++++++++++++++++++++++------ src/mantissa.rs | 24 ++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 901b877..5bab706 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tifloats" -version = "2.3.0" +version = "2.3.1" 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 6e26b5f..c9c0adb 100644 --- a/src/float.rs +++ b/src/float.rs @@ -1,9 +1,9 @@ +use crate::mantissa::Mantissa; +use std::fmt::{Debug, Formatter}; use std::{ cmp::Ordering, ops::{Add, Div, Mul, Neg, Sub}, }; -use std::fmt::{Debug, Formatter}; -use crate::mantissa::Mantissa; use bitflags::bitflags; @@ -153,6 +153,19 @@ impl Float { pub fn is_complex_half(&self) -> bool { self.flags.contains(Flags::COMPLEX_HALF) } + + pub fn digits(&self) -> Vec { + self.mantissa.digits() + } + + pub fn significant_figures(&self) -> Vec { + let mut digits = self.mantissa.digits(); + + let new_length = digits.iter().rposition(|x| *x != 0).map_or(1, |i| i + 1); + + digits.truncate(new_length); + digits + } } impl PartialOrd for Float { @@ -302,7 +315,11 @@ impl Debug for Float { 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))) + f.write_str(&format!( + "0x{} * 10 ^ {}", + self.mantissa.to_dec().to_string(), + (self.exponent as i8).wrapping_add(Float::EXPONENT_NORM as i8) + )) } } @@ -374,8 +391,29 @@ mod tests { #[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))); + 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)) + ); + } + + #[test] + fn sig_figs() { + assert_eq!( + vec![1, 2, 3], + tifloat!(0x12300000000000 * 10 ^ -1).significant_figures() + ); + assert_eq!( + vec![0], + tifloat!(0x00000000000000 * 10 ^ 0).significant_figures() + ); } } diff --git a/src/mantissa.rs b/src/mantissa.rs index d32200d..4818162 100644 --- a/src/mantissa.rs +++ b/src/mantissa.rs @@ -132,6 +132,7 @@ impl Sub for Mantissa { } } +/// # Core operations impl Mantissa { /// Returns the unnormalized sum and the overflow flag. pub fn overflowing_add(self, rhs: Self) -> (Self, bool) { @@ -214,6 +215,18 @@ impl Mantissa { } } +impl Mantissa { + pub fn digits(&self) -> Vec { + let mut nibbles = Vec::with_capacity(16); + for i in (0..14).rev() { + let nibble = (self.data >> 4*i) & 0x0F; + nibbles.push(nibble as u8); + } + + nibbles + } +} + #[cfg(test)] mod tests { use super::*; @@ -340,4 +353,15 @@ mod tests { ) ); } + + #[test] + fn digits() { + assert_eq!( + Mantissa { + data: 0x0014285714285714 + } + .digits(), + vec![1, 4, 2, 8, 5, 7, 1, 4, 2, 8, 5, 7, 1, 4] + ) + } }