Reputation: 143
Is there a way to specify that the generic type T
can only be either f32
or f64
? Otherwise I end up imposing a lot of boilerplate type constraints: Copy
, Add<Output=T>
, Sub<Output=T>
, Mul
..., Div
..., Zero
, One
, etc., which gets tedious after a while. Also I am afraid this list will grow to include Sin
, Cos
, Tan
, etc. in the future.
Upvotes: 4
Views: 554
Reputation: 8529
You want num::traits::Float
, available in crate num
.
Every type satisfying Float
is guaranteed to be Num + Copy + NumCast + PartialOrd + Neg<Output=Self>
. In turn, PartialEq + Zero + One + Add + Sub + Mul + Div + Rem
are required for Num
. This answer your first requests for basic arithmetic. Other operations like sin()
, cos()
are available as member functions in Float
.
Finally, f32
and f64
are supplied out-of-the-box as implementors of Float
.
Upvotes: 6