Struct DivInt

Source
pub struct DivInt<N, const D: u64>(/* private fields */);
Expand description

Rational number with a compile-time denominator.

Implementations§

Source§

impl<N, const D: u64> DivInt<N, D>

Source

pub const fn from_numerator(n: N) -> Self

Constructs the type using the provided number as the numerator.

The effective value of the result is therefore D times smaller than the provided number.

Consider using the convenience macro div_int! instead.

Source§

impl<N: FromF64Approx, const D: u64> DivInt<N, D>

Source

pub fn from_f64_approx(val: f64) -> Option<Self>

Constructs a DivInt by approximating a floating-point number.

This function will return None if the provided number is outside the value range of the DivInt.

§Examples
use div_int::{DivInt, div_int};

assert_eq!(DivInt::<u8, 2>::from_f64_approx(1.5), Some(div_int!(3 / 2)));
assert_eq!(DivInt::<u8, 2>::from_f64_approx(128.0), None);
Source§

impl<N: Copy + Into<f64>, const D: u64> DivInt<N, D>

Source

pub fn to_f64(self) -> f64

Floating-point value of this DivInt.

§Examples
use div_int::DivInt;

assert_eq!(DivInt::<u16, 200>::from_numerator(150).to_f64(), 0.75);
Source§

impl<N: Copy, const D: u64> DivInt<N, D>

Source

pub const fn numerator(self) -> N

Numerator of this Ratio struct.

§Examples
use div_int::div_int;

assert_eq!(div_int!(100 / 1024).numerator(), 100);
Source

pub const fn denominator(self) -> u64

Denominator of this DivInt.

This is a convenience function, as this value can be extracted from the type itself.

§Examples
use div_int::div_int;

assert_eq!(div_int!(100 / 1024).denominator(), 1024);
Source§

impl<N: WrappingAdd, const D: u64> DivInt<N, D>

Source

pub fn wrapping_add(self, other: Self) -> Self

Wrapping (modular) addition. Computes self + rhs, wrapping around at the boundary of the type.

§Examples
use div_int::div_int;

assert_eq!(div_int!(10u8 / 5).wrapping_add(div_int!(3u8 / 5)), div_int!(13u8 / 5));
assert_eq!(div_int!(10u8 / 5).wrapping_add(div_int!(250u8 / 5)), div_int!(4u8 / 5));
Source§

impl<N: WrappingSub, const D: u64> DivInt<N, D>

Source

pub fn wrapping_sub(self, other: Self) -> Self

Wrapping (modular) subtraction. Computes self - rhs, wrapping around at the boundary of the type.

§Examples
use div_int::div_int;

assert_eq!(div_int!(10u8 / 5).wrapping_sub(div_int!(3u8 / 5)), div_int!(7u8 / 5));
assert_eq!(div_int!(10u8 / 5).wrapping_sub(div_int!(20u8 / 5)), div_int!(246u8 / 5));
Source§

impl<N: WrappingNeg, const D: u64> DivInt<N, D>

Source

pub fn wrapping_neg(self) -> Self

Wrapping negation. Computes -self, wrapping around at the boundary of the numerator type.

§Examples
use div_int::div_int;

assert_eq!(div_int!(10i8 / 5).wrapping_neg(), div_int!(-10i8 / 5));
assert_eq!(div_int!(-128i8 / 5).wrapping_neg(), div_int!(-128i8 / 5));
Source§

impl<N: CheckedAdd, const D: u64> DivInt<N, D>

Source

pub fn checked_add(self, other: Self) -> Option<Self>

Checked addition. Computes self + rhs, returning None if the result exceeds the boundary of the numerator type.

§Examples
use div_int::div_int;

assert_eq!(div_int!(50u8 / 5).checked_add(div_int!(100u8 / _)), Some(div_int!(150u8 / _)));
assert_eq!(div_int!(50u8 / 5).checked_add(div_int!(250u8 / _)), None);
Source§

impl<N: CheckedSub, const D: u64> DivInt<N, D>

Source

pub fn checked_sub(self, other: Self) -> Option<Self>

Checked subtraction. Computes self - rhs, returning None if the result exceeds the boundary of the numerator type.

§Examples
use div_int::div_int;

assert_eq!(div_int!(50u8 / 5).checked_sub(div_int!(40u8 / _)), Some(div_int!(10u8 / _)));
assert_eq!(div_int!(50u8 / 5).checked_sub(div_int!(60u8 / _)), None);
Source§

impl<N: CheckedNeg, const D: u64> DivInt<N, D>

Source

pub fn checked_neg(self) -> Option<Self>

Checked negation. Computes -self, returning None if the result exceeds the boundary of the numerator type.

§Examples
use div_int::div_int;

assert_eq!(div_int!(50u8 / 5).checked_neg(), None);
assert_eq!(div_int!(50i8 / 5).checked_neg(), Some(div_int!(-50i8 / 5)));
assert_eq!(div_int!(-128i8 / 5).checked_neg(), None);
assert_eq!(div_int!(127i8 / 5).checked_neg(), Some(div_int!(-127i8 / 5)) );
Source§

impl<N: FromBytes, const D: u64> DivInt<N, D>

Source

pub fn from_be_bytes(bytes: &N::Bytes) -> Self

Creates a DivInt from its representation as a byte array in big endian.

§Examples
use div_int::{DivInt, div_int};

assert_eq!(DivInt::<u16, 50>::from_be_bytes(&[1, 2]), div_int!(258u16 / 50));
Source

pub fn from_le_bytes(bytes: &N::Bytes) -> Self

Creates a DivInt from its representation as a byte array in little endian.

§Examples
use div_int::{DivInt, div_int};

assert_eq!(DivInt::<u16, 50>::from_le_bytes(&[1, 2]), div_int!(513u16 / 50));
Source

pub fn from_ne_bytes(bytes: &N::Bytes) -> Self

Creates a DivInt from its representation as a byte array in native endianness.

Source§

impl<N: Signed, const D: u64> DivInt<N, D>

Source

pub fn abs(&self) -> Self

Computes the absolute value of self.

§Examples
use div_int::div_int;

assert_eq!(div_int!(5i8 / 50).abs(), div_int!(5i8 / 50));
assert_eq!(div_int!(-5i8 / 50).abs(), div_int!(5i8 / 50));
Source

pub fn is_positive(&self) -> bool

Returns true if self is positive and false if the numerator is zero or negative.

§Examples
use div_int::div_int;

assert_eq!(div_int!(5i8 / 50).is_positive(), true);
assert_eq!(div_int!(-10i8 / 50).is_positive(), false);
Source

pub fn is_negative(&self) -> bool

Returns true if self is negative and false if the numerator is zero or positive.

§Examples
use div_int::div_int;

assert_eq!(div_int!(5i8 / 50).is_negative(), false);
assert_eq!(div_int!(-10i8 / 50).is_negative(), true);

Trait Implementations§

Source§

impl<N: Add, const D: u64> Add for DivInt<N, D>

Source§

type Output = DivInt<<N as Add>::Output, D>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<N: AddAssign, const D: u64> AddAssign for DivInt<N, D>

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<N: CheckedAdd, const D: u64> CheckedAdd for DivInt<N, D>

Source§

fn checked_add(&self, v: &Self) -> Option<Self>

Adds two numbers, checking for overflow. If overflow happens, None is returned.
Source§

impl<N: CheckedNeg, const D: u64> CheckedNeg for DivInt<N, D>

Source§

fn checked_neg(&self) -> Option<Self>

Negates a number, returning None for results that can’t be represented, like signed MIN values that can’t be positive, or non-zero unsigned values that can’t be negative. Read more
Source§

impl<N: CheckedSub, const D: u64> CheckedSub for DivInt<N, D>

Source§

fn checked_sub(&self, v: &Self) -> Option<Self>

Subtracts two numbers, checking for underflow. If underflow happens, None is returned.
Source§

impl<N: Clone, const D: u64> Clone for DivInt<N, D>

Source§

fn clone(&self) -> DivInt<N, D>

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<N: Debug, const D: u64> Debug for DivInt<N, D>

Source§

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

Implements the Debug trait.

§Example
use div_int::DivInt;

assert_eq!(format!("{:?}", DivInt::<_, 100>::from_numerator(5)), "div_int!(5 / 100)");
Source§

impl<N: Default, const D: u64> Default for DivInt<N, D>

Source§

fn default() -> DivInt<N, D>

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

impl<'de, N: FromF64Approx, const D: u64> Deserialize<'de> for DivInt<N, D>

Available on crate feature serde only.
Source§

fn deserialize<De>(deserializer: De) -> Result<Self, De::Error>
where De: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<N: Copy + Into<f64>, const D: u64> Display for DivInt<N, D>

Source§

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

Implements the Display trait.

§Example
use div_int::div_int;

assert_eq!(format!("{}", div_int!(10 / 50)), "0.2");
Source§

impl<N: Into<f64>, const D: u64> From<DivInt<N, D>> for f64

Source§

fn from(value: DivInt<N, D>) -> Self

Converts to this type from the input type.
Source§

impl<N: FromBytes, const D: u64> FromBytes for DivInt<N, D>

Source§

type Bytes = <N as FromBytes>::Bytes

Source§

fn from_be_bytes(bytes: &Self::Bytes) -> Self

Create a number from its representation as a byte array in big endian. Read more
Source§

fn from_le_bytes(bytes: &Self::Bytes) -> Self

Create a number from its representation as a byte array in little endian. Read more
Source§

fn from_ne_bytes(bytes: &Self::Bytes) -> Self

Create a number from its memory representation as a byte array in native endianness. Read more
Source§

impl<N: Hash, const D: u64> Hash for DivInt<N, D>

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<N: Neg, const D: u64> Neg for DivInt<N, D>

Source§

type Output = DivInt<<N as Neg>::Output, D>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<N: Ord, const D: u64> Ord for DivInt<N, D>

Source§

fn cmp(&self, other: &DivInt<N, D>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<N: PartialEq, const D: u64> PartialEq for DivInt<N, D>

Source§

fn eq(&self, other: &DivInt<N, D>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<N: PartialOrd, const D: u64> PartialOrd for DivInt<N, D>

Source§

fn partial_cmp(&self, other: &DivInt<N, D>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<N: Copy + Into<f64>, const D: u64> Serialize for DivInt<N, D>

Available on crate feature serde only.
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<N: Shl, const D: u64> Shl for DivInt<N, D>

Source§

type Output = DivInt<<N as Shl>::Output, D>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: Self) -> Self::Output

Performs the << operation. Read more
Source§

impl<N: Shr, const D: u64> Shr for DivInt<N, D>

Source§

type Output = DivInt<<N as Shr>::Output, D>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: Self) -> Self::Output

Performs the >> operation. Read more
Source§

impl<N: Sub, const D: u64> Sub for DivInt<N, D>

Source§

type Output = DivInt<<N as Sub>::Output, D>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<N: SubAssign, const D: u64> SubAssign for DivInt<N, D>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<N: WrappingAdd, const D: u64> WrappingAdd for DivInt<N, D>

Source§

fn wrapping_add(&self, v: &Self) -> Self

Wrapping (modular) addition. Computes self + other, wrapping around at the boundary of the type.
Source§

impl<N: WrappingNeg, const D: u64> WrappingNeg for DivInt<N, D>

Source§

fn wrapping_neg(&self) -> Self

Wrapping (modular) negation. Computes -self, wrapping around at the boundary of the type. Read more
Source§

impl<N: WrappingSub, const D: u64> WrappingSub for DivInt<N, D>

Source§

fn wrapping_sub(&self, v: &Self) -> Self

Wrapping (modular) subtraction. Computes self - other, wrapping around at the boundary of the type.
Source§

impl<N: Copy, const D: u64> Copy for DivInt<N, D>

Source§

impl<N: Eq, const D: u64> Eq for DivInt<N, D>

Source§

impl<N, const D: u64> StructuralPartialEq for DivInt<N, D>

Auto Trait Implementations§

§

impl<N, const D: u64> Freeze for DivInt<N, D>
where N: Freeze,

§

impl<N, const D: u64> RefUnwindSafe for DivInt<N, D>
where N: RefUnwindSafe,

§

impl<N, const D: u64> Send for DivInt<N, D>
where N: Send,

§

impl<N, const D: u64> Sync for DivInt<N, D>
where N: Sync,

§

impl<N, const D: u64> Unpin for DivInt<N, D>
where N: Unpin,

§

impl<N, const D: u64> UnwindSafe for DivInt<N, D>
where N: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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 T
where T: Clone,

Source§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,