#numbers #exponent #type #posit #unum #quire

no-std softposit

Implementation of Posit numbers

27 releases

0.4.0 Dec 14, 2022
0.3.9 Jul 17, 2019
0.3.5 Jun 30, 2019
0.2.7 Jun 9, 2019
0.1.7 May 17, 2019

#275 in Math

Download history 195/week @ 2024-07-20 355/week @ 2024-07-27 349/week @ 2024-08-03 264/week @ 2024-08-10 319/week @ 2024-08-17 258/week @ 2024-08-24 97/week @ 2024-08-31 125/week @ 2024-09-07 217/week @ 2024-09-14 339/week @ 2024-09-21 341/week @ 2024-09-28 204/week @ 2024-10-05 217/week @ 2024-10-12 139/week @ 2024-10-19 319/week @ 2024-10-26 328/week @ 2024-11-02

1,048 downloads per month

MIT/Apache

465KB
13K SLoC

crates.io crates.io

softposit-rs

Rust port of SoftPosit repository.

Such types are supported now:

  • P32E2 or P32: 32-bit posit with exponent size ES=2;
  • P16E1 or P16: 16-bit posit with ES=1;
  • P8E0 or P8: 8-bit posit without exponent bits.

Initial support of generic types PxE1{N} and PxE2{N}. Requires rust 1.51.

Examples

Convert from f64, Add

use softposit::P32;

fn main() {
    let a = P32::from(12.3);
    let b = P32::from(154.);
    let c = a + b;
    println!("c = {0} or c = {0:?}", c);
}

Quire

Each Posit type has correspondent Quire type (Q32, Q16, Q8). These types support AddAssign and SubAssign operations. For example:

use softposit::{P32, Q32};

// c == 12.3*0.4 - 6.3*8.4 == -48;
fn main() {
    let mut q = Q32::init();
    q += (P32::from(12.3), P32::from(0.4));
    q -= (P32::from(6.3), P32::from(8.4));
    let c = q.to_posit();
    println!("c = {0} or c = {0:?}", c);
}

Math

Math functions, like trigonomentic, are partially implemented for P32E2 type. For complex computations use num::Complex type.

Linear algebra

You can also use matrix operations, defined in nalgebra crate by using linalg feature:

use softposit::P32;

use nalgebra::{Dynamic, Matrix, VecStorage};
type DMatrix = Matrix<P32, Dynamic, Dynamic, VecStorage<P32, Dynamic, Dynamic>>;

fn main() {
    let a = DMatrix::new_random(5, 5);
    println!("Matrix A = {}", a);
    let decomp = &a.clone().lu();
    if let Some(c) = decomp.try_inverse() {
        println!("Inverse matrix Aˉ¹ = {}", c);
        println!("Check Identity");
        let ones = a * c;
        println!("A × Aˉ¹ = {}", ones);
    } else {
        println!("Matrix A can't be inverted");
    }
}
cargo build --features="linalg,rand"

Benchmarking

cargo bench
Type Operation i3-2310M @ 2.10GHz i5-3470 @ 3.20GHz
P32E2 + 47 MPOPS 82 MPOPS
- 45 MPOPS 83 MPOPS
* 56 MPOPS 99 MPOPS
/ 25 MPOPS 42 MPOPS
√⎺⎺ 48 MPOPS 92 MPOPS
round 89 MPOPS 159 MPOPS

Dependencies

~0.1–1.3MB
~25K SLoC