Skip to content

Commit

Permalink
2.3.0: better test cases & debug display
Browse files Browse the repository at this point in the history
  • Loading branch information
rpitasky committed Jul 20, 2024
1 parent f10d25d commit 98fbd68
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
/Cargo.lock

flamegraph.svg
perf.data*
perf.data*

.idea
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.2.0"
version = "2.3.0"
edition = "2021"
license = "MIT OR Apache-2.0"
homepage = "https://github.com/TI-Toolkit/tifloats_lib_rs"
Expand Down
21 changes: 19 additions & 2 deletions src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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::*;
Expand Down Expand Up @@ -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)));
}
}
45 changes: 44 additions & 1 deletion src/mantissa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
);
}
}

0 comments on commit 98fbd68

Please sign in to comment.