This crate helps you to ensure the kind of floats you are using, without panic! (except if the unsafe function new_unchecked is used in an unsound way).
zero overhead: everything is checked at compile time.
(only new and try_from adds a little overhead at runtime)
NaN is rejected by all types.
This crate is for you if:
-
If you want to know at compile time if a float can be negative, positive, zero, finite and ensure it is not
NaN, withoutpanic!. -
If you need
core::cmp::Ord,core::cmp::Eqorcore::hash::Hashon (non-NaN) floats.
And their positive and negative counterparts:
Positive,PositiveFinite,StrictlyPositive,StrictlyPositiveFiniteNegative,NegativeFinite,StrictlyNegative,StrictlyNegativeFinite
| Type | -β |
]-β; -0.0[ |
-0.0 |
+0.0 |
]+0.0; +β[ |
+β |
NaN |
|---|---|---|---|---|---|---|---|
NonNaN |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | β |
NonNaNFinite |
β | βοΈ | βοΈ | βοΈ | βοΈ | β | β |
NonZeroNonNaN |
βοΈ | βοΈ | β | β | βοΈ | βοΈ | β |
NonZeroNonNaNFinite |
β | βοΈ | β | β | βοΈ | β | β |
Positive |
β | β | β | βοΈ | βοΈ | βοΈ | β |
PositiveFinite |
β | β | β | βοΈ | βοΈ | β | β |
StrictlyPositive |
β | β | β | β | βοΈ | βοΈ | β |
StrictlyPositiveFinite |
β | β | β | β | βοΈ | β | β |
Negative |
βοΈ | βοΈ | βοΈ | β | β | β | β |
NegativeFinite |
β | βοΈ | βοΈ | β | β | β | β |
StrictlyNegative |
βοΈ | βοΈ | β | β | β | β | β |
StrictlyNegativeFinite |
β | βοΈ | β | β | β | β | β |
To avoid specifying the kind of float (e.g. like Positive<f32>), you can use the modules tf64 and tf32 which expose aliases.
When you handle floats, this crate can help you to ensure that you are not using NaN or Infinity by mistake. Methods and functions implemented returns a type as strict as possible, so you know when you really have to check for NaN or Infinity.
Using one of the type provided by this crate in your public API can help your users to avoid mistakes and limits the checks your functions have to do.
It also helps to make API simpler as you don't have to handle and document all the possible cases with NaN and Infinity for example.
E.g. the following function:
fn fast_inv_sqrt(x: StrictlyPositiveFinite) -> StrictlyPositive;It ensures:
- For the person implementing the API: the parameter
xis neitherNaNnorInfinity, and is strictly positive - For the user: the result is not
NaNand is strictly positive but may beInfinity
In that example:
- the person implementing the API doesn't have to check for
NaN,Infinity, or<= 0for the parameterx - the user only have to check the result for
Infinityif they want to handle it differently and can't call the function with an invalid parameter.
Most methods and traits available on the underlying type are available on the types of this crate.
Most constants are also available, with the most appropriate typed float type (except NAN for obvious reasons) in the tf64 and tf32 modules (in tf64::consts and tf32::consts respectively when the constant comes from core::f64::consts or core::f32::consts). Those modules are named that way to avoid conflicts or confusion with the primitives f32 and f64.
f32 and f64,-0.0 == +0.0 is true for all types of this crate.
To facilitate comparisons, the methods is_positive_zero and is_negative_zero are added.
Conversions: core::convert::From / core::convert::TryFrom
- Between all the types of this crate (of the same kind,
f32orf64) - From
f32andf64 - From integers types (except
u128andi128) - From
NonZero*(core::num::NonZeroU8,core::num::NonZeroU16,core::num::NonZeroU32,core::num::NonZeroU64,core::num::NonZeroI8,core::num::NonZeroI16,core::num::NonZeroI32,core::num::NonZeroI64)
(The traits From and TryFrom are implemented depending on the situation)
Comparisons: core::cmp::PartialOrd and core::cmp::PartialEq
| π | f32/f64 |
NonNaN |
NonNaNFinite |
NonZeroNonNaN |
NonZeroNonNaNFinite |
Positive |
PositiveFinite |
StrictlyPositive |
StrictlyPositiveFinite |
Negative |
NegativeFinite |
StrictlyNegative |
StrictlyNegativeFinite |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
f32/f64 |
N/A | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
NonNaN |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
NonNaNFinite |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
NonZeroNonNaN |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
NonZeroNonNaNFinite |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
Positive |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
PositiveFinite |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
StrictlyPositive |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
StrictlyPositiveFinite |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
Negative |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
NegativeFinite |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
StrictlyNegative |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
StrictlyNegativeFinite |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
| Trait | NonNaN |
NonNaNFinite |
NonZeroNonNaN |
NonZeroNonNaNFinite |
Positive |
PositiveFinite |
StrictlyPositive |
StrictlyPositiveFinite |
Negative |
NegativeFinite |
StrictlyNegative |
StrictlyNegativeFinite |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
core::cmp::Eq |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
core::cmp::Ord |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
core::hash::Hash |
βοΈΒΉ | βοΈΒΉ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
core::default::Default |
0.0 |
0.0 |
β | β | 0.0 |
0.0 |
β | β | -0.0 |
-0.0 |
β | β |
ΒΉ: there is a (small) overhead because they accept 0.0 and -0.0 (which are equal) so they must core::hash::Hash to the same value.
All 12 types implement the methods available on f32 and f64 except:
- deprecated and nightly-only methods
total_cmp(&self, other: &f64) -> Orderingsin_cos(self) -> (f64, f64)mul_add(self, a: f64, b: f64) -> f64clamp(self, min: f64, max: f64) -> f64LowerExpUpperExpProductSumto_int_uncheckedto*_bitsfrom*_bits
The only method that can panic! is the unsafe method new_unchecked when used in an invalid way.
A panic! triggered in any other way is considered a security bug and should be reported.
This crate is designed to have a minimal overhead at runtime, in terms of memory, speed and binary size.
It can even be faster than using primitives f32 and f64 directly, as it may avoids some checks by using compiler hints and can use some faster implementations in some cases.
The only methods that adds a little overhead are try_from because of the checks they do at runtime, compared to the unsafe method new_unchecked.
In debug mode, a little overhead is present, both to check the validity of the values and because inline may not be respected.
Any other overhead is considered a bug and should be reported.
The compiler hints are enabled by default to enable compiler optimization when possible.
Also, some methods are faster than the default implementation. For example:
- When possible
Eqis implemented by comparing the bits of the two floats instead of the slower default implementation, that had special cases forNaN(to handleNaN != NaN) and-0.0(to handle-0.0 == 0.0). (8% faster) Ordis implemented by directly comparing the bits of the two floats instead of the slower default implementation for negatives and positives types. (4% faster)signumdoesn't needs to check forNaN. (35% faster)- types others than
NonNaNandNonNaNFiniteuse a faster implementation ofEq, opening the door to Jump Threading optimisations.
std: enabled by default, gives allf32andf64methods.serde: implementsSerializeandDeserializefor all 12 types.libm: use theFloattrait fromnum-traitsandlibmto implement the missing methods when thestdfeature is disabled. When bothstdandlibmfeatures are enabled, thestdimplementation is used.compiler_hints: enabled by default, will addcore::hint::unreachable_uncheckedafter alldebug_assert.ensure_no_undefined_behavior: Willpanic!in release mode instead of risking undefined behavior. This will override thecompiler_hintsfeature, and adds a little overhead tonew_unchecked. This feature can be enabled by any parent crate to ensure no undefined behavior.
For each operation, at compile time crate determine the most strict type possible for the result.
For example, if you multiply a PositiveFinite and a StrictlyNegativeFinite, the result will be a Negative.
Methods that takes another float as parameter will also return the most strict type possible depending on the both types. For the methods where a trait is not available to specify the return type depending on the parameter type, a new trait is created:
Hypot, Min, Max, Copysign, DivEuclid and Atan2.
- Doesn't fix the floating point quirks such as
0.0 == -0.0 - Doesn't fix the odd methods such as:
sqrt(-0.0)returning-0.0instead ofNaNmin(-0.0, 0.0)possibly returning0.0instead of-0.0(same formax)frac(-0.0)returning0.0instead of-0.0
Because that would introduce a runtime overhead and may introduce some incompatibilities with existing code.
This crate is tested when a new version is release with:
- Rust beta
- Rust stable
- Rust 1.70.0
Also, tests on nightly, beta and stable are run monthly on GitHub actions.
The minimum supported Rust version (MSRV) is 1.70.0 because of the use of dep: in Cargo.toml.
A change in the MSRV will be treated as a breaking change.
Tests are run on different architectures on GitHub actions and CircleCI.
- checked-float A crate for making invariant-enforcing floating point wrappers
- decorum Decorum is a Rust library that provides total ordering, equivalence, hashing, and constraints for floating-point representations. Decorum does not require std.
- eq-float Float wrappers with a total order (by setting NAN == NAN).
- fix_float Fixed floating types that allows useful trait implementations and datastructures on float numbers
- float-derive A crate that allows deriving Eq and Hash for types that contain floating points.
- float-ord A total ordering for floating-point numbers.
- nanbox NaN boxing implementation.
- noisy_float Contains floating point types that panic if they are set to an illegal value, such as NaN.
- num-order Numerically consistent
core::cmp::Eq,core::cmp::Ordandcore::hash::Hashimplementations for variousnumtypes (u32,f64,num_bigint::BigInt, etc.) - ordered-float Provides several wrapper types for Ord and Eq implementations on f64 and friends.
- partial-min-max
minandmaxfunctions that work withPartialOrd. - real_float Floating point types that check for correctness and implement total ordering.
- result_float Floating point type that cannot store NaN.
- totally-ordered No dependency, no-std totally ordered f32/f64
- unsigned-f64 A wrapper around f64 that guarantees that the value is always non-negative on the type level.
Features provided/checked by those crates:
βοΈ: provided, β: not provided, β: unknown
(you may need to scroll to the right to see all the columns: "Production ready", "Avoid panic!", "Minimal overhead", "Eq/Ord", "Hash", "NaN", "Inf", "Zero", "Positive", "Negative")
| Crates | Production ready | Avoid panic! |
Minimal overhead | Eq/Ord | Hash | NaN | Inf | Zero | Positive | Negative |
|---|---|---|---|---|---|---|---|---|---|---|
typed_floats |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ |
checked-float |
βοΈ | βοΈ | β | βοΈ | β | βοΈΒΉ | βοΈΒΉ | βοΈΒΉ | βοΈΒΉ | βοΈΒΉ |
decorum |
βοΈ | β | β | β | β | βοΈΒΉ | βοΈΒΉ | βοΈΒΉ | βοΈΒΉ | βοΈΒΉ |
eq-float |
β | βοΈ | βοΈ | βοΈ | β | β | β | β | β | β |
fix_float |
βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | βοΈ | β | β | β |
float-derive |
β | β | β | βοΈ | βοΈ | β | β | β | β | β |
float-ord |
βοΈ | βοΈ | β | βοΈ | β | β | β | β | β | β |
nanbox |
β | β | β | β | β | βοΈ | β | β | β | β |
noisy_float |
βοΈ | β | β | β | β | βοΈ | βοΈ | β | β | β |
num-order |
βοΈ | βοΈ | β | βοΈ | βοΈ | β | β | β | β | β |
ordered-float |
βοΈ | β | β | βοΈ | β | βοΈ | β | β | β | β |
partial-min-max |
β | βοΈ | βοΈ | β | β | β | β | β | β | β |
real_float |
βοΈ | β | β | βοΈ | β | βοΈ | βοΈ | β | βοΈ | β |
result_float |
βοΈ | βοΈ | β | βοΈ | β | βοΈ | β | β | β | β |
totally-ordered |
βοΈ | βοΈ | β | βοΈ | βοΈ | β | β | β | β | β |
unsigned-f64 |
β | βοΈ | βοΈ | β | β | β | β | β | βοΈ | β |
(N.B. "Production ready" is a subjective measure)
ΒΉ: Can be manually checked
Is on docs.rs.