Fixed Point

This library has been modified from cubit library by influenceth and adjusted to match with other fixed point implementations.

This API provides basic some operations for signed fixed point numbers. Fixed point numbers are represented as a struct with a magnitude and a sign.

The magnitude represents the absolute value of the number, and the sign indicates whether the number is positive or negative.

struct FP8x23 {
    mag: u32,
    sign: bool
}

Data types

Orion supports currently these fixed point types:

Fixed Trait

use orion::numbers::fixed_point::core::FixedTrait;

Fixed trait defines the operations that can be performed on a fixed point.

Arithmetic & Comparison operators

FixedType implements arithmetic and comparison traits. This allows you to perform basic arithmetic operations using the associated operators. (+,+= -,-= *,*= / , /= ), as well as relational operators (>, >= ,< , <= , ==, != ).

Examples

fn add_fp_example() {
    // We instantiate two fixed point from here.
    // a = 1
    // b = 2
    let a = Fixed::new_unscaled(1, false);
    let b = Fixed::new_unscaled(2, false);

    // We can add two fixed point as follows.
    let result = a + b;

    assert(result == Fixed::new_unscaled(3), 'invalid result');
}
fn compare_fp_example() -> bool {
    // We instantiate two fixed point from here.
    // a = 42
    // b = -10
    let a = Fixed::new_unscaled(42, false);
    let b = Fixed::new_unscaled(10, true);

    // We can compare two fixed point as follows.
    return a > b;
}
>>> true

Last updated