Make Float::exp return an unsigned integer

`exp` does not perform any form of unbiasing, so there isn't any reason
it should be signed. Change this.

Additionally, add `EPSILON` to the `Float` trait.
This commit is contained in:
Trevor Gross 2025-01-22 07:13:45 +00:00
parent 5ac2f99954
commit a7cd13b6a3
3 changed files with 8 additions and 4 deletions

View file

@ -30,6 +30,8 @@ impl Float for f8 {
const INFINITY: Self = Self(0b0_1111_000);
const NEG_INFINITY: Self = Self(0b1_1111_000);
const NAN: Self = Self(0b0_1111_100);
// FIXME: incorrect values
const EPSILON: Self = Self::ZERO;
const PI: Self = Self::ZERO;
const NEG_PI: Self = Self::ZERO;
const FRAC_PI_2: Self = Self::ZERO;

View file

@ -96,7 +96,7 @@ where
ix = scaled.to_bits();
match top {
Exp::Shifted(ref mut v) => {
*v = scaled.exp().unsigned();
*v = scaled.exp();
*v = (*v).wrapping_sub(F::SIG_BITS);
}
Exp::NoShift(()) => {

View file

@ -34,6 +34,7 @@ pub trait Float:
const NAN: Self;
const MAX: Self;
const MIN: Self;
const EPSILON: Self;
const PI: Self;
const NEG_PI: Self;
const FRAC_PI_2: Self;
@ -107,13 +108,13 @@ pub trait Float:
}
/// Returns the exponent, not adjusting for bias, not accounting for subnormals or zero.
fn exp(self) -> i32 {
(u32::cast_from(self.to_bits() >> Self::SIG_BITS) & Self::EXP_MAX).signed()
fn exp(self) -> u32 {
u32::cast_from(self.to_bits() >> Self::SIG_BITS) & Self::EXP_MAX
}
/// Extract the exponent and adjust it for bias, not accounting for subnormals or zero.
fn exp_unbiased(self) -> i32 {
self.exp() - (Self::EXP_BIAS as i32)
self.exp().signed() - (Self::EXP_BIAS as i32)
}
/// Returns the significand with no implicit bit (or the "fractional" part)
@ -180,6 +181,7 @@ macro_rules! float_impl {
const MAX: Self = -Self::MIN;
// Sign bit set, saturated mantissa, saturated exponent with last bit zeroed
const MIN: Self = $from_bits(Self::Int::MAX & !(1 << Self::SIG_BITS));
const EPSILON: Self = <$ty>::EPSILON;
const PI: Self = core::$ty::consts::PI;
const NEG_PI: Self = -Self::PI;