Enum kserd::Number[][src]

pub enum Number {
    Uint(u128),
    Int(i128),
    Float(f64),
}

A numerical value.

Number captures Rust numerical primitives: unsigned integers, signed integers, and floating point decimal numbers. The data is stored inside an enum housing the maximum size of each numerical type (128 bits for integers, 64 bits for floats). The numbers are also canonicalized, that is Eq and Ord are implemented and comparisons can be made between integers and floats.

TODO: Operations (add, subtract, etc) are to be added in the future.

The number line extends from negative infinity, through zero, to positive infinity. Nan is above positive infinity. All zeroes are treated equally (-0 == +0), as well as all Nans.

[ -∞, .., 0, .., +∞, NaN ]

Examples

Number can be constructed straight from any of the Rust numbers using the From trait.

let n: Number = 123456u32.into();
assert_eq!(n, Number::Uint(123456));

Comparisons can be made between different number types.

let n = Number::from(100u8);
assert_eq!(n, Number::from(100.0f32));
assert_eq!(n, Number::from(100i32));
assert_ne!(n, Number::from(99.99f64));

PartialEq is also implemented for each Rust number.

let n = Number::from(100u8);
assert_eq!(n, 100.0f32);
assert_eq!(n, 100i32);
assert_ne!(n, 99.99);

Ordering is likewise implemented.

let n = Number::from(100u8);
assert!(n < Number::from(101));
assert!(n > Number::from(-100));
assert!(n > Number::from(99.99));
// Likewise with literal numbers
assert!(n < 101);
assert!(n > -100);
assert!(n > 99.99);

An exmaple of the canonicalized ordering.

use std::f64::{INFINITY, NEG_INFINITY, NAN};

// use an ordered set
let mut set = std::collections::BTreeSet::new();

set.insert(Number::from(0));
set.insert((-0.0).into());
set.insert((-1.0).into());
set.insert(0.5.into());
set.insert(INFINITY.into());
set.insert((-100).into());
set.insert(NAN.into());
set.insert(NAN.into());
set.insert((-0.0).into());
set.insert(NEG_INFINITY.into());
set.insert(100.0.into());

let expected: Vec<Number> = vec![
     NEG_INFINITY,
     -100.0,
     -1.0,
     0.0,
     0.5,
     100.0,
     INFINITY,
     NAN,
].into_iter().map(Number::from).collect();

assert_eq!(set.into_iter().collect::<Vec<_>>(), expected);

Variants

Uint(u128)
Int(i128)
Float(f64)

Implementations

impl Number[src]

pub fn as_u128(&self) -> Result<u128, IntoIntError>[src]

Represent Number as an unsigned integer.

Example

use kserd::ds::IntoIntError;
use std::f64::{INFINITY, NEG_INFINITY, NAN};

// generally the conversion will work
assert_eq!(Number::from(100i32).as_u128(), Ok(100));
assert_eq!(Number::from(100.0).as_u128(), Ok(100));
// can also work if fractional part of float is smaller than valid amount
assert_eq!(Number::from(3.0 + 5e-11).as_u128(), Ok(3));
// can fail if outside
assert_eq!(Number::from(-100i32).as_u128(), Err(IntoIntError));
assert_eq!(Number::from(0.5).as_u128(), Err(IntoIntError));
assert_eq!(Number::from(INFINITY).as_u128(), Err(IntoIntError));
assert_eq!(Number::from(NEG_INFINITY).as_u128(), Err(IntoIntError));
assert_eq!(Number::from(NAN).as_u128(), Err(IntoIntError));

pub fn as_i128(&self) -> Result<i128, IntoIntError>[src]

Represent Number as a signed integer.

Example

use kserd::ds::IntoIntError;
use std::f64::{INFINITY, NEG_INFINITY, NAN};

// generally the conversion will work
assert_eq!(Number::from(100u32).as_i128(), Ok(100));
assert_eq!(Number::from(100.0).as_i128(), Ok(100));
// can also work if fractional part of float is smaller than valid amount
assert_eq!(Number::from(3.0 + 5e-11).as_i128(), Ok(3));
// can fail if outside
assert_eq!(Number::from(0.5).as_i128(), Err(IntoIntError));
assert_eq!(Number::from(INFINITY).as_i128(), Err(IntoIntError));
assert_eq!(Number::from(NEG_INFINITY).as_i128(), Err(IntoIntError));
assert_eq!(Number::from(NAN).as_i128(), Err(IntoIntError));

pub fn as_f64(&self) -> f64[src]

Represent Number as a floating point decimal. Does not fail, but is a lossy conversion if an integer.

Examples

assert_eq!(Number::from(100u8).as_f64(), 100.0);
assert_eq!(Number::from(-100).as_f64(), -100.0);
// lossy conversion if integers get large.
assert_eq!(Number::from(u128::max_value()).as_f64(), 3402823669209385e23);
assert_eq!(Number::from(i128::max_value()).as_f64(), 17014118346046923e22);
assert_eq!(Number::from(i128::min_value()).as_f64(), -17014118346046923e22);

Trait Implementations

impl Clone for Number[src]

impl Copy for Number[src]

impl Debug for Number[src]

impl Display for Number[src]

impl Eq for Number[src]

impl From<f32> for Number[src]

impl From<f64> for Number[src]

impl From<i128> for Number[src]

impl From<i16> for Number[src]

impl From<i32> for Number[src]

impl From<i64> for Number[src]

impl From<i8> for Number[src]

impl From<isize> for Number[src]

impl From<u128> for Number[src]

impl From<u16> for Number[src]

impl From<u32> for Number[src]

impl From<u64> for Number[src]

impl From<u8> for Number[src]

impl From<usize> for Number[src]

impl FromStr for Number[src]

type Err = IntoNumberErr

The associated error which can be returned from parsing.

impl Hash for Number[src]

impl Ord for Number[src]

impl PartialEq<Number> for Number[src]

impl PartialEq<f32> for Number[src]

impl PartialEq<f64> for Number[src]

impl PartialEq<i128> for Number[src]

impl PartialEq<i16> for Number[src]

impl PartialEq<i32> for Number[src]

impl PartialEq<i64> for Number[src]

impl PartialEq<i8> for Number[src]

impl PartialEq<isize> for Number[src]

impl PartialEq<u128> for Number[src]

impl PartialEq<u16> for Number[src]

impl PartialEq<u32> for Number[src]

impl PartialEq<u64> for Number[src]

impl PartialEq<u8> for Number[src]

impl PartialEq<usize> for Number[src]

impl PartialOrd<Number> for Number[src]

impl PartialOrd<f32> for Number[src]

impl PartialOrd<f64> for Number[src]

impl PartialOrd<i128> for Number[src]

impl PartialOrd<i16> for Number[src]

impl PartialOrd<i32> for Number[src]

impl PartialOrd<i64> for Number[src]

impl PartialOrd<i8> for Number[src]

impl PartialOrd<isize> for Number[src]

impl PartialOrd<u128> for Number[src]

impl PartialOrd<u16> for Number[src]

impl PartialOrd<u32> for Number[src]

impl PartialOrd<u64> for Number[src]

impl PartialOrd<u8> for Number[src]

impl PartialOrd<usize> for Number[src]

Auto Trait Implementations

impl RefUnwindSafe for Number

impl Send for Number

impl Sync for Number

impl Unpin for Number

impl UnwindSafe for Number

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Conv for T

impl<T> Conv for T

impl<T> FmtForward for T

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pipe for T where
    T: ?Sized

impl<T> Pipe for T

impl<T> PipeAsRef for T

impl<T> PipeBorrow for T

impl<T> PipeDeref for T

impl<T> PipeRef for T

impl<T> Tap for T

impl<T> Tap for T

impl<T, U> TapAsRef<U> for T where
    U: ?Sized

impl<T, U> TapBorrow<U> for T where
    U: ?Sized

impl<T> TapDeref for T

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> TryConv for T

impl<T> TryConv for T

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.