#range #numeric #quantization #numbers #save #smaller #bandwidth

numquant

Quantize numbers to a smaller range to save bandwidth or memory data types and back again

2 unstable releases

0.2.0 Apr 11, 2022
0.1.0 Apr 11, 2022

#6 in #bandwidth

Download history 175/week @ 2023-12-13 144/week @ 2023-12-20 139/week @ 2023-12-27 211/week @ 2024-01-03 393/week @ 2024-01-10 163/week @ 2024-01-17 128/week @ 2024-01-24 129/week @ 2024-01-31 97/week @ 2024-02-07 127/week @ 2024-02-14 180/week @ 2024-02-21 199/week @ 2024-02-28 178/week @ 2024-03-06 168/week @ 2024-03-13 367/week @ 2024-03-20 195/week @ 2024-03-27

963 downloads per month
Used in 5 crates (via adder-codec-core)

MIT/Apache

12KB
178 lines

numquant

Quantize numbers to a smaller range to save bandwidth or memory.

The input floating point value is expected within a given range. Values outside this range will be clamped. The input value will then be quantized into a given integer range.

For example, given the allowed range -1000.0 to 1000.0, and the quantized range 0 to 255 (to fit in a byte), the value -1000.0 would be quantized to 0, and 1000.0 would be quantized to 255, and values in-between are linearly interpolated between 0 and 255.

Example

This example uses the type Quantized<U8<0, 1000>> that converts any floating point number between 0.0 and 1000.0 to a byte (which has the range 0 to 255). Some precision is lost, but an approximate value can be brought back.

let original = 500.0;
// Quantize the value into a byte.
// Quantization supports inputs between 0 and 1000.
let quantized = Quantized::<U8<0, 1000>>::from_f64(original);
// Convert it back to an f64
let dequantized = quantized.to_f64();
// The conversion isn't lossless, but the dequantized value is close to the original:
approx::assert_abs_diff_eq!(original, dequantized, epsilon = U8::<0, 1000>::max_error());

Dependencies

~185KB