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:

Data typedtype

Q8.23

FP8x23

Q16.16

FP16x16

Q32.32

FP32x32

Q64.64

FP64x64

Fixed Trait

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

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

functiondescription

Constructs a new fixed point instance.

Creates a new fixed point instance with the specified unscaled magnitude and sign.

Creates a new fixed point instance from a felt252 value.

Returns the absolute value of the fixed point number.

Returns the smallest integer greater than or equal to the fixed point number.

Returns the value of e raised to the power of the fixed point number.

Returns the value of 2 raised to the power of the fixed point number.

Returns the largest integer less than or equal to the fixed point number.

Returns the natural logarithm of the fixed point number.

Returns the base-2 logarithm of the fixed point number.

Returns the base-10 logarithm of the fixed point number.

Returns the result of raising the fixed point number to the power of another fixed point number.

Rounds the fixed point number to the nearest whole number.

Returns the square root of the fixed point number.

Returns the arccosine (inverse of cosine) of the fixed point number.

Returns the arccosine (inverse of cosine) of the fixed point number faster with LUT.

Returns the arcsine (inverse of sine) of the fixed point number.

Returns the arcsine (inverse of sine) of the fixed point number faster with LUT.

Returns the arctangent (inverse of tangent) of the input fixed point number.

Returns the arctangent (inverse of tangent) of the input fixed point number faster with LUT.

Returns the cosine of the fixed point number.

Returns the cosine of the fixed point number fast with LUT.

Returns the sine of the fixed point number.

Returns the sine of the fixed point number faster with LUT.

Returns the tangent of the fixed point number.

Returns the tangent of the fixed point number faster with LUT.

Returns the value of the inverse hyperbolic cosine of the fixed point number.

Returns the value of the inverse hyperbolic sine of the fixed point number.

Returns the value of the inverse hyperbolic tangent of the fixed point number.

Returns the value of the hyperbolic cosine of the fixed point number.

Returns the value of the hyperbolic sine of the fixed point number.

Returns the value of the hyperbolic tangent of the fixed point number.

Returns the element-wise indication of the sign of the input fixed point number.

Returns the error function of the input fixed point number computed element-wise.

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