Skip to content

Commit

Permalink
2.0.0: Nailed the 1.0.0 release.
Browse files Browse the repository at this point in the history
Adjusts the constructor to accept an i8 for exponent, which is more reasonable than having people know a magic constant hidden behind a missing visibility modifier.
  • Loading branch information
rpitasky committed Jul 11, 2023
1 parent 8fb857d commit 2cd4812
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 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 = "1.0.0"
version = "2.0.0"
edition = "2021"
license = "MIT OR Apache-2.0"
homepage = "https://github.com/TI-Toolkit/tifloats_lib_rs"
Expand Down
26 changes: 16 additions & 10 deletions src/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use crate::FloatError;
#[macro_export]
macro_rules! tifloat {
(-$mantissa:literal * 10 ^ $exponent:literal) => {{
let float = Float::new(true, 0x80 + ($exponent), $mantissa);
let float = Float::new_unchecked(true, $exponent, $mantissa);

float
}};

($mantissa:literal * 10 ^ $exponent:literal) => {{
let float = Float::new(false, 0x80 + ($exponent), $mantissa);
let float = Float::new_unchecked(false, $exponent, $mantissa);

float
}};
Expand Down Expand Up @@ -63,22 +63,19 @@ impl Float {
| self.mantissa.bits() as u128
}

fn negated(&self) -> Self {
Float {
flags: self.flags ^ Flags::NEGATIVE,
..*self
}
/// Intended for use with the tifloat! macro
pub fn new(negative: bool, exponent: i8, mantissa: u64) -> Result<Self, FloatError> {
Self::new_unchecked(negative, exponent, mantissa).check()
}

/// Intended for use with the tifloat! macro
pub fn new(negative: bool, exponent: u8, mantissa: u64) -> Float {
pub fn new_unchecked(negative: bool, exponent: i8, mantissa: u64) -> Self {
Float {
flags: if negative {
Flags::NEGATIVE
} else {
Flags::empty()
},
exponent,
exponent: Self::EXPONENT_NORM + (exponent as u8),
mantissa: Mantissa::from(mantissa).unwrap(),
}
}
Expand Down Expand Up @@ -113,6 +110,15 @@ impl Float {
mantissa,
})
}

/// Checks if this float's exponent is within the allowed range
pub fn check(self) -> Result<Self, FloatError> {
if (Self::EXPONENT_MIN..=Self::EXPONENT_MAX).contains(&self.exponent) {
Ok(self)
} else {
Err(FloatError::Overflow)
}
}
}

impl Float {
Expand Down

0 comments on commit 2cd4812

Please sign in to comment.