blob: fa0b8675c681de449739a87880e69fe6ec99affc (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
use num::{Float};
/// Q1_7 - single byte representation of a fixed point number with range [-1, 1].
/// The name refers to the Texas Instrument representation
///
/// References:
/// - English Wikipedia: "Q (number format)" https://en.wikipedia.org/wiki/Q_(number_format)
struct Q1_7(i8); // tuple struct holding a i8 value
impl From<T: Float> for Q1_7 {
fn from (n:T) -> Self {
let val = if n > 1.0 { // out of bounds numbers are coerced to the maximum of the range
1
} else if n < -1.0 {
-1
} else {
n * (2**7)
}
Q1_7(val as i8)
}
}
impl From for U: Float {
fn from(q: Q1_7) -> U {
q.0 * (2 ** -7)
}
}
mod tests {
use super::*;
test
}
|