pub struct Num<I, const N: usize>(/* private fields */)
where
I: FixedWidthUnsignedInteger;
Expand description
A fixed point number represented using I
with N
bits of fractional precision
Implementations§
source§impl<I, const N: usize> Num<I, N>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> Num<I, N>where I: FixedWidthUnsignedInteger,
sourcepub fn change_base<J, const M: usize>(self) -> Num<J, M>where
J: FixedWidthUnsignedInteger + From<I>,
pub fn change_base<J, const M: usize>(self) -> Num<J, M>where J: FixedWidthUnsignedInteger + From<I>,
Performs the conversion between two integer types and between two different fractional precisions
sourcepub fn try_change_base<J, const M: usize>(self) -> Option<Num<J, M>>where
J: FixedWidthUnsignedInteger + TryFrom<I>,
pub fn try_change_base<J, const M: usize>(self) -> Option<Num<J, M>>where J: FixedWidthUnsignedInteger + TryFrom<I>,
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 from_f32(input: f32) -> Num<I, N>
pub fn from_f32(input: f32) -> Num<I, N>
Lossily transforms an f32 into a fixed point representation. This is not const
because you cannot currently do floating point operations in const contexts, so
you should use the num!
macro from agb-macros if you want a const from_f32/f64
sourcepub fn from_f64(input: f64) -> Num<I, N>
pub fn from_f64(input: f64) -> Num<I, N>
Lossily transforms an f64 into a fixed point representation. This is not const
because you cannot currently do floating point operations in const contexts, so
you should use the num!
macro from agb-macros if you want a const from_f32/f64
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: Num<I, N>) -> Num<I, N>
pub fn rem_euclid(self, rhs: Num<I, N>) -> Num<I, N>
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, const N: usize> Num<I, N>where
I: FixedWidthSignedInteger,
impl<I, const N: usize> Num<I, N>where I: FixedWidthSignedInteger,
sourcepub fn abs(self) -> Num<I, N>
pub fn abs(self) -> Num<I, N>
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) -> Num<I, N>
pub fn cos(self) -> Num<I, N>
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) -> Num<I, N>
pub fn sin(self) -> Num<I, N>
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)
+=
operation. Read moresource§impl<I, const N: usize> Clone for Num<I, N>where
I: Clone + FixedWidthUnsignedInteger,
impl<I, const N: usize> Clone for Num<I, N>where I: Clone + FixedWidthUnsignedInteger,
source§impl<I, const N: usize> Debug for Num<I, N>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> Debug for Num<I, N>where I: FixedWidthUnsignedInteger,
source§impl<I, const N: usize> Default for Num<I, N>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> Default for Num<I, N>where I: FixedWidthUnsignedInteger,
source§impl<I, const N: usize> Display for Num<I, N>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> Display for Num<I, N>where I: FixedWidthUnsignedInteger,
source§impl<I, const N: usize> Div<I> for Num<I, N>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> Div<I> for Num<I, N>where I: FixedWidthUnsignedInteger,
source§impl<I, const N: usize> Div for Num<I, N>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> Div for Num<I, N>where I: FixedWidthUnsignedInteger,
source§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)
/=
operation. Read moresource§impl<I, const N: usize> From<I> for Num<I, N>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> From<I> for Num<I, N>where I: FixedWidthUnsignedInteger,
source§impl<I, const N: usize> Hash for Num<I, N>where
I: Hash + FixedWidthUnsignedInteger,
impl<I, const N: usize> Hash for Num<I, N>where I: Hash + FixedWidthUnsignedInteger,
source§impl<I, const N: usize> Mul<I> for Num<I, N>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> Mul<I> for Num<I, N>where I: FixedWidthUnsignedInteger,
source§impl<I, const N: usize> Mul for Num<I, N>where
I: FixedWidthUnsignedInteger,
impl<I, const N: usize> Mul for Num<I, N>where I: FixedWidthUnsignedInteger,
source§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)
*=
operation. Read moresource§impl<I, const N: usize> Neg for Num<I, N>where
I: FixedWidthSignedInteger,
impl<I, const N: usize> Neg for Num<I, N>where I: FixedWidthSignedInteger,
source§impl<I, const N: usize> Ord for Num<I, N>where
I: Ord + FixedWidthUnsignedInteger,
impl<I, const N: usize> Ord for Num<I, N>where I: Ord + FixedWidthUnsignedInteger,
source§impl<I, const N: usize> PartialEq for Num<I, N>where
I: PartialEq + FixedWidthUnsignedInteger,
impl<I, const N: usize> PartialEq for Num<I, N>where I: PartialEq + FixedWidthUnsignedInteger,
source§impl<I, const N: usize> PartialOrd for Num<I, N>where
I: PartialOrd + FixedWidthUnsignedInteger,
impl<I, const N: usize> PartialOrd for Num<I, N>where I: PartialOrd + FixedWidthUnsignedInteger,
source§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)
%=
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)
-=
operation. Read more