Skip to content

Commit

Permalink
Add digits and significant figures getters
Browse files Browse the repository at this point in the history
  • Loading branch information
rpitasky committed Aug 18, 2024
1 parent 98fbd68 commit 9fa2793
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
50 changes: 44 additions & 6 deletions src/float.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -153,6 +153,19 @@ impl Float {
pub fn is_complex_half(&self) -> bool {
self.flags.contains(Flags::COMPLEX_HALF)
}

pub fn digits(&self) -> Vec<u8> {
self.mantissa.digits()
}

pub fn significant_figures(&self) -> Vec<u8> {
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 {
Expand Down Expand Up @@ -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)
))
}
}

Expand Down Expand Up @@ -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()
);
}
}
24 changes: 24 additions & 0 deletions src/mantissa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -214,6 +215,18 @@ impl Mantissa {
}
}

impl Mantissa {
pub fn digits(&self) -> Vec<u8> {
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::*;
Expand Down Expand Up @@ -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]
)
}
}

0 comments on commit 9fa2793

Please sign in to comment.