numlike
Numeric traits for generic mathematics. Less restrictive and less conservative
alternative to num-traits.
This crate has no unsafe code and no mandatory third-party dependencies, and
is no_std-compatible. Most of no_std operations on floating point numbers
still have a dependency on libm, which is
gated behind libm feature flag.
Usage
Adding dependency
[]
= { = "0.1.2" }
Comparison to other libraries
Why numlike and not num-traits?
We developed numlike primarily because we disagree with many of design
decisions in the venerable num-traits crate.
num-traits'sZeroandOnetraits respectively requireAddandMultraits to be implemented. This makes it impossible to distinguish a 0 for algebraic structures that don't implement addition (e.g. absorption magma and absorption monoid, aka. magma with zero and monoid with zero), and likewise 1 when there is no multiplication (e.g. because a naive implementation of multiplication for all elements would be inefficient).num-traitsalso requiresOutput = SelfforAddandMul, making it impossible to useZeroandOnefor statically-typed unit of measurement libraries likeuom.numlikedoes not have these problems because it does not have any supertraits for itsZeroandOne.
- Moreover,
num-traits'sZeroandOnedo not provideZEROandONEassociated constants, but instead return them from::zero()and::one()functions. These constants were only later added through new separate traits,ConstZeroandConstOne, presumably to avoid breaking changes.numlike'sZeroandOneprovideZEROandONEassociated constants.
num-traits'sBoundedtrait only returns finite minimum and maximum values. This makes no difference for integers, but e.g. for floats.max_value()returnsf32::MAX, which is actually the largest finite number, equal to3.40282347e+38, not the positive infinity.num-traitshas no interface to generically obtain positive or negative infinity as min. or max. value.numlikesolves that by providingMinExtended/MaxExtendedtraits that result in negative and positive infinities for floats, andMinFinite/MaxFinitetraits that give only finite values just as abovenum-traits'sBoundeddoes.
num-traitsprovides.signum()and.abs()methods only for types implementingSignedtrait, which excludes unsigned integer types.num-traitsdoes not provide checked mathematical operation traits,CheckedAdd,CheckedSub,CheckedMul,CheckedDiv,CheckedNeg,CheckedRem, for floats.numlikeimplements its own versions of these traits for all numeric primitives.
- Developers of
num-traitsseem to avoid breaking changes, strongly preferring to create new traits instead of making modifications to existing ones. This provides stability for users, but prevents at least some of the above issues from being solved.- Because of that, we have decided to roll our own library (this crate).
However, because it's in active development, we are lacking the stability of
num-traits-- we are much more likely to have breaking changes and bugs.
- Because of that, we have decided to roll our own library (this crate).
However, because it's in active development, we are lacking the stability of
- Furthermore,
numlikealso has its own features, such as:- Equality and order traits that fix
NaNs to be the highest value in the set, even larger than positive infinity, allowing for total order:NanPartialEq,NanEq,NanPartialOrd,NanOrd.- But if you want
core::cmptraits instead, consider usingordered-floatcrate'sOrderedFloatfloat type wrapper instead. This is obviously often the better choice, becausecore::cmptraits will work with a much larger number of existing libraries and have Rust's syntactic sugar.
- But if you want
- Equality and order traits that fix