Struct agb::fixnum::Num

source ·
pub struct Num<I, const N: usize>(_)
where
    I: FixedWidthUnsignedInteger
;
Expand description

A fixed point number represented using I with N bits of fractional precision

Implementations§

Performs the conversion between two integer types and between two different fractional precisions

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

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

The internal representation of the fixed point number

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

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

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

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

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

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

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

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

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§

The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Formats the value using the given formatter. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
Performs the /= operation. Read more
Converts to this type from the input type.
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
Performs the *= operation. Read more
The resulting type after applying the - operator.
Performs the unary - operation. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
The resulting type after applying the % operator.
Performs the % operation. Read more
Performs the %= operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
Performs the -= operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.