Struct agb_fixnum::Num

source ·
pub struct Num<I: FixedWidthUnsignedInteger, const N: usize>(/* private fields */);
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>

source

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

source

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);
source

pub const fn from_raw(n: I) -> Self

A bit for bit conversion from a number to a fixed num

source

pub fn to_raw(self) -> I

The internal representation of the fixed point number

source

pub fn from_f32(input: f32) -> Self

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

source

pub fn from_f64(input: f64) -> Self

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

source

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);
source

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));
source

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

pub fn frac(self) -> I

Returns the fractional component of a number as it’s integer representation

let n: Num<i32, 8> = num!(5.5);
assert_eq!(n.frac(), 1 << 7);
source

pub fn new(integral: I) -> Self

Creates an integer represented by a fixed point number

let n: Num<i32, 8> = Num::new(5);
assert_eq!(n.frac(), 0); // no fractional component
assert_eq!(n, num!(5.)); // just equals the number 5
source§

impl<const N: usize> Num<i32, N>

source

pub fn sqrt(self) -> Self

Returns the square root of a number, it is calculated a digit at a time.

let n: Num<i32, 8> = num!(16.);
assert_eq!(n.sqrt(), num!(4.));
let n: Num<i32, 8> = num!(2.25);
assert_eq!(n.sqrt(), num!(1.5));
source§

impl<I: FixedWidthSignedInteger, const N: usize> Num<I, N>

source

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));
source

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.));
source

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>>,

§

type Output = Num<I, N>

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

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)

Performs the += operation. Read more
source§

impl<I: Clone + FixedWidthUnsignedInteger, const N: usize> Clone for Num<I, N>

source§

fn clone(&self) -> Num<I, N>

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<I: FixedWidthUnsignedInteger, const N: usize> Debug for Num<I, N>

source§

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

Formats the value using the given formatter. Read more
source§

impl<I, const N: usize> Default for Num<I, N>

source§

fn default() -> Self

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

impl<I: FixedWidthUnsignedInteger, const N: usize> Display for Num<I, N>

source§

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

Formats the value using the given formatter. Read more
source§

impl<I, const N: usize> Div<I> for Num<I, N>

§

type Output = Num<I, N>

The resulting type after applying the / operator.
source§

fn div(self, rhs: I) -> Self::Output

Performs the / operation. Read more
source§

impl<I, const N: usize> Div for Num<I, N>

§

type Output = Num<I, N>

The resulting type after applying the / operator.
source§

fn div(self, rhs: Num<I, N>) -> Self::Output

Performs the / operation. Read more
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>>,

source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
source§

impl<I: FixedWidthUnsignedInteger, const N: usize> From<I> for Num<I, N>

source§

fn from(value: I) -> Self

Converts to this type from the input type.
source§

impl<I: Hash + FixedWidthUnsignedInteger, const N: usize> Hash for Num<I, N>

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<I, const N: usize> Mul<I> for Num<I, N>

§

type Output = Num<I, N>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: I) -> Self::Output

Performs the * operation. Read more
source§

impl<I, const N: usize> Mul for Num<I, N>

§

type Output = Num<I, N>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Num<I, N>) -> Self::Output

Performs the * operation. Read more
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>>,

source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
source§

impl<I: FixedWidthSignedInteger, const N: usize> Neg for Num<I, N>

§

type Output = Num<I, N>

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl<I: FixedWidthUnsignedInteger + Num, const N: usize> Num for Num<I, N>

§

type FromStrRadixErr = <f64 as Num>::FromStrRadixErr

source§

fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>

Convert from a string and radix (typically 2..=36). Read more
source§

impl<I: FixedWidthUnsignedInteger, const N: usize> One for Num<I, N>

source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
source§

fn is_one(&self) -> bool
where Self: PartialEq,

Returns true if self is equal to the multiplicative identity. Read more
source§

impl<I: Ord + FixedWidthUnsignedInteger, const N: usize> Ord for Num<I, N>

source§

fn cmp(&self, other: &Num<I, N>) -> 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 + PartialOrd,

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

impl<I: PartialEq + FixedWidthUnsignedInteger, const N: usize> PartialEq for Num<I, N>

source§

fn eq(&self, other: &Num<I, N>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<I: PartialOrd + FixedWidthUnsignedInteger, const N: usize> PartialOrd for Num<I, N>

source§

fn partial_cmp(&self, other: &Num<I, N>) -> 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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<I, T, const N: usize> Rem<T> for Num<I, N>
where I: FixedWidthUnsignedInteger, T: Into<Num<I, N>>,

§

type Output = Num<I, N>

The resulting type after applying the % operator.
source§

fn rem(self, modulus: T) -> Self::Output

Performs the % operation. Read more
source§

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)

Performs the %= operation. Read more
source§

impl<I, T, const N: usize> Sub<T> for Num<I, N>
where I: FixedWidthUnsignedInteger, T: Into<Num<I, N>>,

§

type Output = Num<I, N>

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

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)

Performs the -= operation. Read more
source§

impl<I: FixedWidthUnsignedInteger, const N: usize> Zero for Num<I, N>

source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
source§

impl<I: Copy + FixedWidthUnsignedInteger, const N: usize> Copy for Num<I, N>

source§

impl<I: Eq + FixedWidthUnsignedInteger, const N: usize> Eq for Num<I, N>

source§

impl<I: FixedWidthUnsignedInteger, const N: usize> Number for Num<I, N>

source§

impl<I: FixedWidthUnsignedInteger, const N: usize> StructuralPartialEq for Num<I, N>

Auto Trait Implementations§

§

impl<I, const N: usize> RefUnwindSafe for Num<I, N>
where I: RefUnwindSafe,

§

impl<I, const N: usize> Send for Num<I, N>
where I: Send,

§

impl<I, const N: usize> Sync for Num<I, N>
where I: Sync,

§

impl<I, const N: usize> Unpin for Num<I, N>
where I: Unpin,

§

impl<I, const N: usize> UnwindSafe for Num<I, N>
where I: 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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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> NumAssign for T
where T: Num + NumAssignOps,

source§

impl<T, Rhs> NumAssignOps<Rhs> for T
where T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,

source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,