Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- `classify_bits`, `is_infinity_bits` are now `const`
- Make the following methods `const` on all float types:
- `abs()`
- `classify()`
- `classify_bits()`
- `copysign()`
- `is_finite()`
- `is_infinite()`
- `is_infinity_bits()`
- `is_nan()`
- `is_normal()`
- `signum()`

## [0.1.2](https://github.com/LDeakin/microfloat/releases/tag/v0.1.2) - 2026-04-29

Expand Down
16 changes: 8 additions & 8 deletions src/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,27 +315,27 @@ macro_rules! define_format {
}

/// Returns `true` if this value is NaN.
pub fn is_nan(self) -> bool {
pub const fn is_nan(self) -> bool {
self.0.is_nan()
}

/// Returns `true` if this value is positive or negative infinity.
pub fn is_infinite(self) -> bool {
pub const fn is_infinite(self) -> bool {
self.0.is_infinite()
}

/// Returns `true` if this value is neither infinite nor NaN.
pub fn is_finite(self) -> bool {
pub const fn is_finite(self) -> bool {
self.0.is_finite()
}

/// Returns `true` if this value is finite, nonzero, and not subnormal.
pub fn is_normal(self) -> bool {
pub const fn is_normal(self) -> bool {
self.0.is_normal()
}

/// Returns the floating point category of this value.
pub fn classify(self) -> core::num::FpCategory {
pub const fn classify(self) -> core::num::FpCategory {
self.0.classify()
}

Expand All @@ -354,19 +354,19 @@ macro_rules! define_format {
}

/// Returns a value with the magnitude of `self` and the sign of `sign`.
pub fn copysign(self, sign: Self) -> Self {
pub const fn copysign(self, sign: Self) -> Self {
Self(self.0.copysign(sign.0))
}

/// Returns a number representing the sign of `self`.
///
/// NaN and zero values are returned unchanged.
pub fn signum(self) -> Self {
pub const fn signum(self) -> Self {
Self(self.0.signum())
}

/// Returns the absolute value of `self`.
pub fn abs(self) -> Self {
pub const fn abs(self) -> Self {
Self(self.0.abs())
}

Expand Down
23 changes: 12 additions & 11 deletions src/micro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,24 @@ impl<F: Format> MicroFloat<F> {
f64::from(self.to_f32())
}

pub fn is_nan(self) -> bool {
pub const fn is_nan(self) -> bool {
classify_bits::<F>(self.bits).is_nan
}

pub fn is_infinite(self) -> bool {
pub const fn is_infinite(self) -> bool {
classify_bits::<F>(self.bits).is_infinite
}

pub fn is_finite(self) -> bool {
pub const fn is_finite(self) -> bool {
!self.is_nan() && !self.is_infinite()
}

pub fn is_normal(self) -> bool {
matches!(self.classify(), FpCategory::Normal)
pub const fn is_normal(self) -> bool {
let class = classify_bits::<F>(self.bits);
!class.is_nan && !class.is_infinite && !class.is_zero && !class.is_subnormal
}

pub fn classify(self) -> FpCategory {
pub const fn classify(self) -> FpCategory {
let class = classify_bits::<F>(self.bits);
if class.is_nan {
FpCategory::Nan
Expand Down Expand Up @@ -126,11 +127,11 @@ impl<F: Format> MicroFloat<F> {
}
}

pub fn copysign(self, sign: Self) -> Self {
pub const fn copysign(self, sign: Self) -> Self {
if matches!(F::NAN, NanEncoding::Single(_)) && self.is_nan() {
return self;
}
if F::SIGN == SignMode::Unsigned {
if matches!(F::SIGN, SignMode::Unsigned) {
self
} else if sign.is_sign_negative() {
Self::from_bits(abs_bits::<F>(self.bits) | F::SIGN_BIT)
Expand All @@ -139,8 +140,8 @@ impl<F: Format> MicroFloat<F> {
}
}

pub fn signum(self) -> Self {
if self.is_nan() || self.classify() == FpCategory::Zero {
pub const fn signum(self) -> Self {
if self.is_nan() || matches!(self.classify(), FpCategory::Zero) {
self
} else if self.is_sign_negative() {
Self::NEG_ONE
Expand All @@ -149,7 +150,7 @@ impl<F: Format> MicroFloat<F> {
}
}

pub fn abs(self) -> Self {
pub const fn abs(self) -> Self {
if matches!(F::NAN, NanEncoding::Single(_)) && self.is_nan() {
return self;
}
Expand Down
54 changes: 54 additions & 0 deletions tests/const_methods.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use microfloat::{
f4e2m1fn, f6e2m3fn, f6e3m2fn, f8e3m4, f8e4m3, f8e4m3b11fnuz, f8e4m3fn, f8e4m3fnuz, f8e5m2,
f8e5m2fnuz, f8e8m0fnu,
};

macro_rules! assert_const_methods_compile {
($($type:ty),* $(,)?) => {
$(
const _: () = {
let value = <$type>::from_bits(0x01);
let sign = <$type>::NEG_ONE;
let bits = value.to_bits();
let _ = <$type>::from_le_bytes([bits]);
let _ = <$type>::from_be_bytes([bits]);
let _ = <$type>::from_ne_bytes([bits]);
let _ = value.to_le_bytes();
let _ = value.to_be_bytes();
let _ = value.to_ne_bytes();
let _ = <$type>::has_inf();
let _ = <$type>::has_nan();
let _ = <$type>::is_finite_only();
let _ = value.is_nan();
let _ = value.is_infinite();
let _ = value.is_finite();
let _ = value.is_normal();
let _ = value.classify();
let _ = value.is_sign_positive();
let _ = value.is_sign_negative();
let _ = value.copysign(sign);
let _ = value.signum();
let _ = value.abs();
let _ = value.next_up();
let _ = value.next_down();
};
)*
};
}

assert_const_methods_compile!(
f8e3m4,
f8e4m3,
f8e4m3b11fnuz,
f8e4m3fn,
f8e4m3fnuz,
f8e5m2,
f8e5m2fnuz,
f8e8m0fnu,
f4e2m1fn,
f6e2m3fn,
f6e3m2fn,
);

#[test]
fn const_methods_compile() {}