Enum dactyl::FloatKind

source ·
pub enum FloatKind {
    NaN,
    Zero,
    Normal(u64, u32, bool),
    Overflow(bool),
    Infinity,
}
Expand description

Float Type.

This enum provides basic float classification. It is used by NiceFloat for formatting, but may be useful in other contexts too. Enjoy!

Examples

use dactyl::FloatKind;

// Weird things.
assert_eq!(FloatKind::from(f64::NAN), FloatKind::NaN);
assert_eq!(FloatKind::from(f64::INFINITY), FloatKind::Infinity);
assert_eq!(FloatKind::from(f64::NEG_INFINITY), FloatKind::Infinity);

// Really big or small values can't be parsed out.
assert_eq!(FloatKind::from(f64::MIN), FloatKind::Overflow(true));
assert_eq!(FloatKind::from(f64::MAX), FloatKind::Overflow(false));

// Normal things.
assert_eq!(FloatKind::from(0_f32), FloatKind::Zero);
assert_eq!(FloatKind::from(123.456_f64), FloatKind::Normal(123, 45600000, false));
assert_eq!(FloatKind::from(-123.456_f64), FloatKind::Normal(123, 45600000, true));

As mentioned elsewhere, Rust floats are imprecise. Any imprecision within the original float will come through in the parsed FloatKind.

use dactyl::FloatKind;

// This is right, but wrong. Haha.
assert_eq!(
    FloatKind::from(1234.5678_f32),
    FloatKind::Normal(1234, 56774902, false),
);

Variants§

§

NaN

§

Zero

Zero.

This does not differentiate between positive and negative zero; they’re just zero…

§

Normal(u64, u32, bool)

Normal.

This holds the integer and fractional parts of the float, along with a bool indicating whether or not it was negative.

The integer range must fit within u64; larger (absolute) values will fall back to FloatKind::Overflow.

The fractional range holds up to eight digits, rounding on the ninth using a tie-to-even strategy.

§

Overflow(bool)

Overflow.

The value is normal, but is too big to be nicely split. The bool indicates whether or not the value is negative.

§

Infinity

Infinity.

This does not differentiate between positive and negative infinity; the point is the numbers go on and on and on…

Trait Implementations§

source§

impl Clone for FloatKind

source§

fn clone(&self) -> FloatKind

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for FloatKind

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for FloatKind

source§

fn default() -> FloatKind

Returns the “default value” for a type. Read more
source§

impl From<FloatKind> for NiceFloat

source§

fn from(kind: FloatKind) -> Self

Converts to this type from the input type.
source§

impl From<f32> for FloatKind

source§

fn from(num: f32) -> Self

Converts to this type from the input type.
source§

impl From<f64> for FloatKind

source§

fn from(num: f64) -> Self

Converts to this type from the input type.
source§

impl Hash for FloatKind

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<FloatKind> for FloatKind

source§

fn eq(&self, other: &FloatKind) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for FloatKind

source§

impl Eq for FloatKind

source§

impl StructuralEq for FloatKind

source§

impl StructuralPartialEq for FloatKind

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.