4 releases

0.1.3 Apr 18, 2023
0.1.2 Feb 18, 2023
0.1.1 Feb 7, 2023
0.1.0 Feb 7, 2023

#23 in No standard library

Download history 43/week @ 2023-02-13 16/week @ 2023-02-20 5/week @ 2023-02-27 5/week @ 2023-03-06 3/week @ 2023-03-13 6/week @ 2023-03-20 5/week @ 2023-03-27 78/week @ 2023-04-03 91/week @ 2023-04-10 223/week @ 2023-04-17 264/week @ 2023-04-24 222/week @ 2023-05-01 220/week @ 2023-05-08 1439/week @ 2023-05-15 1508/week @ 2023-05-22 2008/week @ 2023-05-29

5,185 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

140KB
3K SLoC

Rust float-point in constant context

Floating-point code is from compiler_builtins = "0.1.87" and libm = "0.2.6", and has been rewritten for use in a constant context.

Some fuzzing of operations is performed to ensure correctness of the port, but please open an issue if there is any inconsistent behavior.

Features:

  • no_std
  • const_trait_impl
  • const_mut_refs

On stable:

const fn const_f32_add(a: f32, b: f32) -> f32 {
    SoftF32(a).add(SoftF32(b)).to_f32()
}

On nightly with const_trait_impl usage:

const fn const_f32_add(a: f32, b: f32) -> f32 {
    (SoftF32(a) + SoftF32(b)).to_f32()
}

On nightly with const_mut_refs usage:

const fn const_f32_add(a: f32, b: f32) -> f32 {
    let mut x = SoftF32(a);
    x += SoftF32(b);
    x.to_f32()
}

lib.rs:

Rust float-point in constant context

Features:

  • no_std
  • const_trait_impl
  • const_mut_refs

work in stable:

# use const_soft_float::soft_f32::SoftF32;
const fn const_f32_add(a: f32, b: f32) -> f32 {
    SoftF32(a).add(SoftF32(b)).to_f32()
}

with const_trait_impl usage (requires nightly):

# cfg_if::cfg_if! {
# if #[cfg(nightly)] {
# #![feature(const_trait_impl)]
# use const_soft_float::soft_f32::SoftF32;
const fn const_f32_add(a: f32, b: f32) -> f32 {
    (SoftF32(a) + SoftF32(b)).to_f32()
}
# }
# }

with const_mut_refs usage (requires nightly):

# cfg_if::cfg_if! {
# if #[cfg(nightly)] {
# #![feature(const_trait_impl)]
# #![feature(const_mut_refs)]
# use const_soft_float::soft_f32::SoftF32;
const fn const_f32_add(a: f32, b: f32) -> f32 {
    let mut x = SoftF32(a);
    x += SoftF32(b);
    x.to_f32()
}
# }
# }

No runtime deps