add from_fixed to trait Float
This commit is contained in:
parent
9c22e037c0
commit
ed3ed75093
|
@ -45,11 +45,13 @@ numeric primitives are implemented. That is, you can use [`From`] or
|
|||
* The new methods [`to_fixed`], [`to_float`], [`checked_to_fixed`],
|
||||
[`checked_to_int`] and [`checked_to_float`] were added.
|
||||
* The methods [`from_fixed`][`Int::from_fixed`] and
|
||||
[`to_fixed`][`Int::to_fixed`] and thier checked versions were
|
||||
[`to_fixed`][`Int::to_fixed`], and thier checked versions, were
|
||||
added to the [`Int`] trait.
|
||||
* The method [`to_fixed`][`Float::to_fixed`] and its checked
|
||||
versions were added to the [`Float`] trait.
|
||||
* The method [`from_fixed`][`Float::from_fixed`], and the method
|
||||
[`to_fixed`][`Float::to_fixed`] and its checked versions, were
|
||||
added to the [`Float`] trait.
|
||||
|
||||
[`Float::from_fixed`]: https://docs.rs/fixed/0.3.0/fixed/sealed/trait.Float.html#method.from_fixed
|
||||
[`Float::to_fixed`]: https://docs.rs/fixed/0.3.0/fixed/sealed/trait.Float.html#method.to_fixed
|
||||
[`Float`]: https://docs.rs/fixed/0.3.0/fixed/sealed/trait.Float.html
|
||||
[`Int::from_fixed`]: https://docs.rs/fixed/0.3.0/fixed/sealed/trait.Int.html#method.from_fixed
|
||||
|
|
|
@ -11,10 +11,10 @@ Version 0.3.0 (unreleased)
|
|||
* The return type of `to_int` is now generic.
|
||||
* The new methods `to_fixed`, `to_float`, `checked_to_fixed`,
|
||||
`checked_to_int` and `checked_to_float` were added.
|
||||
* The methods `from_fixed` and `to_fixed` and their checked versions
|
||||
were added to the `Int` trait.
|
||||
* The method `to_fixed` and its checked versions were added to the
|
||||
`Float` trait.
|
||||
* The methods `from_fixed` and `to_fixed`, and their checked
|
||||
versions, were added to the `Int` trait.
|
||||
* The method `from_fixed`, and the method `to_fixed` and its checked
|
||||
versions, were added to the `Float` trait.
|
||||
|
||||
Version 0.2.1 (2019-01-29)
|
||||
==========================
|
||||
|
|
|
@ -313,6 +313,27 @@ pub trait Int: SealedInt {
|
|||
/// [`f64`]: https://doc.rust-lang.org/nightly/std/primitive.f64.html
|
||||
/// [`f16` feature]: ../index.html#optional-features
|
||||
pub trait Float: SealedFloat {
|
||||
/// Converts from a fixed-point number.
|
||||
///
|
||||
/// This method rounds to the nearest, with ties rounding to even.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use fixed::sealed::Float;
|
||||
/// type Fix = fixed::FixedI16<fixed::frac::U8>;
|
||||
/// // 1.625 is 1.101 in binary
|
||||
/// let fix = Fix::from_bits(0b1101 << (8 - 3));
|
||||
/// assert_eq!(f32::from_fixed(fix), 1.625);
|
||||
/// ```
|
||||
#[inline]
|
||||
fn from_fixed<F>(val: F) -> Self
|
||||
where
|
||||
F: Fixed,
|
||||
{
|
||||
val.to_float()
|
||||
}
|
||||
|
||||
/// Converts to a fixed-point number.
|
||||
///
|
||||
/// This method rounds to the nearest, with ties rounding to even.
|
||||
|
|
|
@ -49,6 +49,9 @@ pub trait SealedFixed: Copy + Debug + Display {
|
|||
F: Fixed;
|
||||
|
||||
fn overflowing_from_float<F>(float: F) -> (Self, bool)
|
||||
where
|
||||
F: Float;
|
||||
fn to_float<F>(self) -> F
|
||||
where
|
||||
F: Float;
|
||||
|
||||
|
@ -107,6 +110,14 @@ macro_rules! sealed_fixed {
|
|||
$Fixed::overflowing_from_float(float)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn to_float<F>(self) -> F
|
||||
where
|
||||
F: Float,
|
||||
{
|
||||
$Fixed::to_float(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn from_bits(bits: Self::Bits) -> Self {
|
||||
$Fixed::from_bits(bits)
|
||||
|
|
Loading…
Reference in New Issue