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>
impl<N, const D: u64> DivInt<N, D>
Sourcepub const fn from_numerator(n: N) -> Self
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>
impl<N: FromF64Approx, const D: u64> DivInt<N, D>
Sourcepub fn from_f64_approx(val: f64) -> Option<Self>
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, const D: u64> DivInt<N, D>
impl<N: Copy, const D: u64> DivInt<N, D>
Sourcepub const fn numerator(self) -> N
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);
Sourcepub const fn denominator(self) -> u64
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>
impl<N: WrappingAdd, const D: u64> DivInt<N, D>
Sourcepub fn wrapping_add(self, other: Self) -> Self
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>
impl<N: WrappingSub, const D: u64> DivInt<N, D>
Sourcepub fn wrapping_sub(self, other: Self) -> Self
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>
impl<N: WrappingNeg, const D: u64> DivInt<N, D>
Sourcepub fn wrapping_neg(self) -> Self
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>
impl<N: CheckedAdd, const D: u64> DivInt<N, D>
Sourcepub fn checked_add(self, other: Self) -> Option<Self>
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>
impl<N: CheckedSub, const D: u64> DivInt<N, D>
Sourcepub fn checked_sub(self, other: Self) -> Option<Self>
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>
impl<N: CheckedNeg, const D: u64> DivInt<N, D>
Sourcepub fn checked_neg(self) -> Option<Self>
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>
impl<N: FromBytes, const D: u64> DivInt<N, D>
Sourcepub fn from_be_bytes(bytes: &N::Bytes) -> Self
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));
Sourcepub fn from_le_bytes(bytes: &N::Bytes) -> Self
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));
Sourcepub fn from_ne_bytes(bytes: &N::Bytes) -> Self
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>
impl<N: Signed, const D: u64> DivInt<N, D>
Sourcepub fn abs(&self) -> Self
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));
Sourcepub fn is_positive(&self) -> bool
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);
Sourcepub fn is_negative(&self) -> bool
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: AddAssign, const D: u64> AddAssign for DivInt<N, D>
impl<N: AddAssign, const D: u64> AddAssign for DivInt<N, D>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moreSource§impl<N: CheckedAdd, const D: u64> CheckedAdd for DivInt<N, D>
impl<N: CheckedAdd, const D: u64> CheckedAdd for DivInt<N, D>
Source§fn checked_add(&self, v: &Self) -> Option<Self>
fn checked_add(&self, v: &Self) -> Option<Self>
None
is
returned.Source§impl<N: CheckedNeg, const D: u64> CheckedNeg for DivInt<N, D>
impl<N: CheckedNeg, const D: u64> CheckedNeg for DivInt<N, D>
Source§fn checked_neg(&self) -> Option<Self>
fn checked_neg(&self) -> Option<Self>
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 moreSource§impl<N: CheckedSub, const D: u64> CheckedSub for DivInt<N, D>
impl<N: CheckedSub, const D: u64> CheckedSub for DivInt<N, D>
Source§fn checked_sub(&self, v: &Self) -> Option<Self>
fn checked_sub(&self, v: &Self) -> Option<Self>
None
is returned.Source§impl<'de, N: FromF64Approx, const D: u64> Deserialize<'de> for DivInt<N, D>
Available on crate feature serde
only.
impl<'de, N: FromF64Approx, const D: u64> Deserialize<'de> for DivInt<N, D>
serde
only.Source§fn deserialize<De>(deserializer: De) -> Result<Self, De::Error>where
De: Deserializer<'de>,
fn deserialize<De>(deserializer: De) -> Result<Self, De::Error>where
De: Deserializer<'de>,
Source§impl<N: FromBytes, const D: u64> FromBytes for DivInt<N, D>
impl<N: FromBytes, const D: u64> FromBytes for DivInt<N, D>
type Bytes = <N as FromBytes>::Bytes
Source§fn from_be_bytes(bytes: &Self::Bytes) -> Self
fn from_be_bytes(bytes: &Self::Bytes) -> Self
Source§fn from_le_bytes(bytes: &Self::Bytes) -> Self
fn from_le_bytes(bytes: &Self::Bytes) -> Self
Source§fn from_ne_bytes(bytes: &Self::Bytes) -> Self
fn from_ne_bytes(bytes: &Self::Bytes) -> Self
Source§impl<N: Ord, const D: u64> Ord for DivInt<N, D>
impl<N: Ord, const D: u64> Ord for DivInt<N, D>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<N: PartialOrd, const D: u64> PartialOrd for DivInt<N, D>
impl<N: PartialOrd, const D: u64> PartialOrd for DivInt<N, D>
Source§impl<N: Copy + Into<f64>, const D: u64> Serialize for DivInt<N, D>
Available on crate feature serde
only.
impl<N: Copy + Into<f64>, const D: u64> Serialize for DivInt<N, D>
serde
only.Source§impl<N: SubAssign, const D: u64> SubAssign for DivInt<N, D>
impl<N: SubAssign, const D: u64> SubAssign for DivInt<N, D>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read moreSource§impl<N: WrappingAdd, const D: u64> WrappingAdd for DivInt<N, D>
impl<N: WrappingAdd, const D: u64> WrappingAdd for DivInt<N, D>
Source§fn wrapping_add(&self, v: &Self) -> Self
fn wrapping_add(&self, v: &Self) -> Self
self + other
, wrapping around at the boundary of
the type.Source§impl<N: WrappingNeg, const D: u64> WrappingNeg for DivInt<N, D>
impl<N: WrappingNeg, const D: u64> WrappingNeg for DivInt<N, D>
Source§fn wrapping_neg(&self) -> Self
fn wrapping_neg(&self) -> Self
-self
,
wrapping around at the boundary of the type. Read moreSource§impl<N: WrappingSub, const D: u64> WrappingSub for DivInt<N, D>
impl<N: WrappingSub, const D: u64> WrappingSub for DivInt<N, D>
Source§fn wrapping_sub(&self, v: &Self) -> Self
fn wrapping_sub(&self, v: &Self) -> Self
self - other
, wrapping around at the boundary
of the type.