Struct agb_fixnum::Num
source · #[repr(transparent)]pub struct Num<I: FixedWidthUnsignedInteger, const N: usize>(_);
Expand description
A fixed point number represented using I
with N
bits of fractional precision
Implementations§
source§impl<I: FixedWidthUnsignedInteger, const N: usize> Num<I, N>
impl<I: FixedWidthUnsignedInteger, const N: usize> Num<I, N>
sourcepub fn change_base<J: FixedWidthUnsignedInteger + From<I>, const M: usize>(
self
) -> Num<J, M>
pub fn change_base<J: FixedWidthUnsignedInteger + From<I>, const M: usize>(
self
) -> Num<J, M>
Performs the conversion between two integer types and between two different fractional precisions
sourcepub fn try_change_base<J: FixedWidthUnsignedInteger + TryFrom<I>, const M: usize>(
self
) -> Option<Num<J, M>>
pub fn try_change_base<J: FixedWidthUnsignedInteger + TryFrom<I>, const M: usize>(
self
) -> Option<Num<J, M>>
Attempts to perform the conversion between two integer types and between two different fractional precisions
let a: Num<i32, 8> = 1.into();
let b: Option<Num<u8, 4>> = a.try_change_base();
assert_eq!(b, Some(1.into()));
let a: Num<i32, 8> = 18.into();
let b: Option<Num<u8, 4>> = a.try_change_base();
assert_eq!(b, None);
sourcepub fn trunc(self) -> I
pub fn trunc(self) -> I
Truncates the fixed point number returning the integral part
let n: Num<i32, 8> = num!(5.67);
assert_eq!(n.trunc(), 5);
let n: Num<i32, 8> = num!(-5.67);
assert_eq!(n.trunc(), -5);
sourcepub fn rem_euclid(self, rhs: Self) -> Self
pub fn rem_euclid(self, rhs: Self) -> Self
Performs the equivalent to the integer rem_euclid, which is modulo numbering.
let n: Num<i32, 8> = num!(5.67);
let r: Num<i32, 8> = num!(4.);
assert_eq!(n.rem_euclid(r), num!(1.67));
let n: Num<i32, 8> = num!(-1.5);
let r: Num<i32, 8> = num!(4.);
assert_eq!(n.rem_euclid(r), num!(2.5));
sourcepub fn floor(self) -> I
pub fn floor(self) -> I
Performs rounding towards negative infinity
let n: Num<i32, 8> = num!(5.67);
assert_eq!(n.floor(), 5);
let n: Num<i32, 8> = num!(-5.67);
assert_eq!(n.floor(), -6);
source§impl<I: FixedWidthSignedInteger, const N: usize> Num<I, N>
impl<I: FixedWidthSignedInteger, const N: usize> Num<I, N>
sourcepub fn abs(self) -> Self
pub fn abs(self) -> Self
Returns the absolute value of a fixed point number
let n: Num<i32, 8> = num!(5.5);
assert_eq!(n.abs(), num!(5.5));
let n: Num<i32, 8> = num!(-5.5);
assert_eq!(n.abs(), num!(5.5));
sourcepub fn cos(self) -> Self
pub fn cos(self) -> Self
Calculates the cosine of a fixed point number with the domain of [0, 1]. Uses a fifth order polynomial.
let n: Num<i32, 8> = num!(0.); // 0 radians
assert_eq!(n.cos(), num!(1.));
let n: Num<i32, 8> = num!(0.25); // pi / 2 radians
assert_eq!(n.cos(), num!(0.));
let n: Num<i32, 8> = num!(0.5); // pi radians
assert_eq!(n.cos(), num!(-1.));
let n: Num<i32, 8> = num!(0.75); // 3pi/2 radians
assert_eq!(n.cos(), num!(0.));
let n: Num<i32, 8> = num!(1.); // 2 pi radians (whole rotation)
assert_eq!(n.cos(), num!(1.));
sourcepub fn sin(self) -> Self
pub fn sin(self) -> Self
Calculates the sine of a number with domain of [0, 1].
let n: Num<i32, 8> = num!(0.); // 0 radians
assert_eq!(n.sin(), num!(0.));
let n: Num<i32, 8> = num!(0.25); // pi / 2 radians
assert_eq!(n.sin(), num!(1.));
let n: Num<i32, 8> = num!(0.5); // pi radians
assert_eq!(n.sin(), num!(0.));
let n: Num<i32, 8> = num!(0.75); // 3pi/2 radians
assert_eq!(n.sin(), num!(-1.));
let n: Num<i32, 8> = num!(1.); // 2 pi radians (whole rotation)
assert_eq!(n.sin(), num!(0.));
Trait Implementations§
source§impl<I, T, const N: usize> Add<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
impl<I, T, const N: usize> Add<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
source§impl<I, T, const N: usize> AddAssign<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
impl<I, T, const N: usize> AddAssign<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
Performs the
+=
operation. Read moresource§impl<I, T, const N: usize> DivAssign<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
Num<I, N>: Div<T, Output = Num<I, N>>,
impl<I, T, const N: usize> DivAssign<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
Num<I, N>: Div<T, Output = Num<I, N>>,
source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
Performs the
/=
operation. Read moresource§impl<I, T, const N: usize> MulAssign<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
Num<I, N>: Mul<T, Output = Num<I, N>>,
impl<I, T, const N: usize> MulAssign<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
Num<I, N>: Mul<T, Output = Num<I, N>>,
source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
Performs the
*=
operation. Read moresource§impl<I: Ord + FixedWidthUnsignedInteger, const N: usize> Ord for Num<I, N>
impl<I: Ord + FixedWidthUnsignedInteger, const N: usize> Ord for Num<I, N>
source§impl<I: PartialEq + FixedWidthUnsignedInteger, const N: usize> PartialEq<Num<I, N>> for Num<I, N>
impl<I: PartialEq + FixedWidthUnsignedInteger, const N: usize> PartialEq<Num<I, N>> for Num<I, N>
source§impl<I: PartialOrd + FixedWidthUnsignedInteger, const N: usize> PartialOrd<Num<I, N>> for Num<I, N>
impl<I: PartialOrd + FixedWidthUnsignedInteger, const N: usize> PartialOrd<Num<I, N>> for Num<I, N>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl<I, T, const N: usize> Rem<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
impl<I, T, const N: usize> Rem<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
source§impl<I, T, const N: usize> RemAssign<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
impl<I, T, const N: usize> RemAssign<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
source§fn rem_assign(&mut self, modulus: T)
fn rem_assign(&mut self, modulus: T)
Performs the
%=
operation. Read moresource§impl<I, T, const N: usize> Sub<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
impl<I, T, const N: usize> Sub<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
source§impl<I, T, const N: usize> SubAssign<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
impl<I, T, const N: usize> SubAssign<T> for Num<I, N>where
I: FixedWidthUnsignedInteger,
T: Into<Num<I, N>>,
source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
Performs the
-=
operation. Read more