float_format/
components.rs

1use crate::*;
2
3/// Components of a floating point number.
4#[derive(Clone, PartialEq, Eq, Hash)]
5pub struct Components {
6    /// The sign of the number, `Some(true)` if negative, `None` if the format is unsigned.
7    pub sign: Option<bool>,
8
9    /// The exponent bit pattern of the number, assumed to be `String` with '1's and '0's.
10    pub exp: BitPattern,
11
12    /// The mantissa bit pattern of the number, assumed to be `String` with '1's and '0's.
13    pub mant: BitPattern,
14}
15
16impl Components {
17    /// Create from the given values for `sign`, `exp`, and `mant`.
18    /// The radix of `exp` and `mant` is deduced from the first 2 chars.
19    /// '0b' => binary, '0x' => hexadecimal, '0o' => octal, '0d' => decimal.
20    pub fn new(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
21        Ok(Components {
22            sign,
23            exp: BitPattern::from_str(exp)?,
24            mant: BitPattern::from_str(mant)?,
25        })
26    }
27
28    /// Create from the given values for `sign`, `exp`, and `mant`.
29    /// The `exp` and `mant` should be string with '1's and '0's.
30    /// Any characters other than '0' and '1' are ignored.
31    pub fn new_bin(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
32        Ok(Components {
33            sign,
34            exp: BitPattern::from_bin_str(exp),
35            mant: BitPattern::from_bin_str(mant),
36        })
37    }
38
39    /// Create from the given values for `sign`, `exp`, and `mant`.
40    /// The `exp` and `mant` should be string with octal digits.
41    /// Any characters other than octal digits are ignored.
42    pub fn new_oct(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
43        Ok(Components {
44            sign,
45            exp: BitPattern::from_oct_str(exp),
46            mant: BitPattern::from_oct_str(mant),
47        })
48    }
49
50    /// Create from the given values for `sign`, `exp`, and `mant`.
51    /// The `exp` and `mant` should be string with decimal digits.
52    /// Any characters other than decimal digits are ignored.
53    pub fn new_dec(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
54        Ok(Components {
55            sign,
56            exp: BitPattern::from_dec_str(exp),
57            mant: BitPattern::from_dec_str(mant),
58        })
59    }
60
61    /// Create from the given values for `sign`, `exp`, and `mant`.
62    /// The `exp` and `mant` should be string with hexadecimal digits.
63    /// Any characters other than hexadecimal digits are ignored.
64    pub fn new_hex(sign: Option<bool>, exp: &str, mant: &str) -> Result<Self, error::Error> {
65        Ok(Components {
66            sign,
67            exp: BitPattern::from_hex_str(exp),
68            mant: BitPattern::from_hex_str(mant),
69        })
70    }
71
72    /// Get the format of the components with the given `excess`.
73    pub fn format_with_excess(&self, excess: u32) -> Format {
74        Format::new_with_sign(
75            self.sign.is_some(),
76            self.exp.len() as u8,
77            self.mant.len(),
78            excess,
79        )
80    }
81
82    /// Get the format of the components.
83    /// The excess value is default to 0.
84    pub fn format(&self) -> Format {
85        self.format_with_excess(0)
86    }
87
88    /// Get the number of bits for the format.
89    pub fn len(&self) -> usize {
90        self.sign.is_some() as usize + self.exp.len() + self.mant.len()
91    }
92
93    /// Get the sign bit as a str of ether '1' or '0'.
94    pub fn get_sign(&self) -> Option<&str> {
95        self.sign.as_ref().map(|s| if *s { "1" } else { "0" })
96    }
97}
98
99impl std::fmt::Debug for Components {
100    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
101        f.debug_struct("Components")
102            .field("sign", &format_args!("{}", &self.sign.map(|b| if b { "-" } else { "+" }).unwrap_or("None")))
103            .field("exp", &format_args!("{}", &self.exp.to_bin_string()))
104            .field("mant", &format_args!("{}", &self.mant.to_bin_string()))
105            .finish()
106    }
107}