pub trait RawMantissaAndExponent<M, E, T = Self>: Sized {
// Required methods
fn raw_mantissa_and_exponent(self) -> (M, E);
fn from_raw_mantissa_and_exponent(raw_mantissa: M, raw_exponent: E) -> T;
// Provided methods
fn raw_mantissa(self) -> M { ... }
fn raw_exponent(self) -> E { ... }
}
Expand description
Converts a number to or from a raw mantissa and exponent.
See here for a definition of raw mantissa and exponent.
Required Methods§
Sourcefn raw_mantissa_and_exponent(self) -> (M, E)
fn raw_mantissa_and_exponent(self) -> (M, E)
Extracts the raw mantissa and exponent from a number.
Sourcefn from_raw_mantissa_and_exponent(raw_mantissa: M, raw_exponent: E) -> T
fn from_raw_mantissa_and_exponent(raw_mantissa: M, raw_exponent: E) -> T
Constructs a number from its raw mantissa and exponent.
Provided Methods§
Sourcefn raw_mantissa(self) -> M
fn raw_mantissa(self) -> M
Extracts the raw mantissa from a number.
Sourcefn raw_exponent(self) -> E
fn raw_exponent(self) -> E
Extracts the raw exponent from a number.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl RawMantissaAndExponent<u64, u64> for f32
impl RawMantissaAndExponent<u64, u64> for f32
Source§fn raw_mantissa_and_exponent(self) -> (u64, u64)
fn raw_mantissa_and_exponent(self) -> (u64, u64)
Returns the raw mantissa and exponent.
The raw exponent and raw mantissa are the actual bit patterns used to represent the
components of self
. When self
is nonzero and finite, the raw exponent $e_r$ is
an integer in $[0, 2^E-2]$ and the raw mantissa $m_r$ is an integer in $[0, 2^M-1]$.
When self
is nonzero and finite, $f(x) = (m_r, e_r)$, where
$$
m_r = \begin{cases}
2^{M+2^{E-1}-2}|x| & \text{if} \quad |x| < 2^{2-2^{E-1},} \\
2^M \left ( \frac{|x|}{2^{\lfloor \log_2 |x| \rfloor}}-1\right ) &
\textrm{otherwise},
\end{cases}
$$
and
$$
e_r = \begin{cases}
0 & \text{if} \quad |x| < 2^{2-2^{E-1}} \\
\lfloor \log_2 |x| \rfloor + 2^{E-1} - 1 & \textrm{otherwise}.
\end{cases}
$$
and $M$ and $E$ are the mantissa width and exponent width, respectively.
For zeros, infinities, or NaN
, refer to IEEE
754 or look at the examples below.
The inverse operation is Self::from_raw_mantissa_and_exponent
.
§Worst-case complexity
Constant time and additional memory.
§Examples
See here.
Source§fn raw_mantissa(self) -> u64
fn raw_mantissa(self) -> u64
Returns the raw mantissa.
The raw mantissa is the actual bit pattern used to represent the mantissa of self
.
When self
is nonzero and finite, it is an integer in $[0, 2^M-1]$.
When self
is nonzero and finite,
$$
f(x) = \begin{cases}
2^{M+2^{E-1}-2}|x| & \text{if} \quad |x| < 2^{2-2^{E-1}}, \\
2^M \left ( \frac{|x|}{2^{\lfloor \log_2 |x| \rfloor}}-1\right )
& \textrm{otherwise},
\end{cases}
$$
where $M$ and $E$ are the mantissa width and exponent width, respectively.
For zeros, infinities, or NaN
, refer to IEEE
754 or look at the examples below.
§Worst-case complexity
Constant time and additional memory.
§Examples
See here.
Source§fn raw_exponent(self) -> u64
fn raw_exponent(self) -> u64
Returns the raw exponent.
The raw exponent is the actual bit pattern used to represent the exponent of self
.
When self
is nonzero and finite, it is an integer in $[0, 2^E-2]$.
When self
is nonzero and finite,
$$
f(x) = \begin{cases}
0 & \text{if} \quad |x| < 2^{2-2^{E-1}}, \\
\lfloor \log_2 |x| \rfloor + 2^{E-1} - 1 & \textrm{otherwise},
\end{cases}
$$
where $M$ and $E$ are the mantissa width and exponent width, respectively.
For zeros, infinities, or NaN
, refer to IEEE
754 or look at the examples below.
§Worst-case complexity
Constant time and additional memory.
§Examples
See here.
Source§fn from_raw_mantissa_and_exponent(raw_mantissa: u64, raw_exponent: u64) -> f32
fn from_raw_mantissa_and_exponent(raw_mantissa: u64, raw_exponent: u64) -> f32
Constructs a float from its raw mantissa and exponent.
The raw exponent and raw mantissa are the actual bit patterns used to represent the components of a float. When the float is nonzero and finite, the raw exponent $e_r$ is an integer in $[0, 2^E-2]$ and the raw mantissa $m_r$ is an integer in $[0, 2^M-1]$.
When the exponent is not 2^E-1
,
$$
f(m_r, e_r) = \begin{cases}
2^{2-2^{E-1}-M}m_r & \text{if} \quad e_r = 0, \\
2^{e_r-2^{E-1}+1}(2^{-M}m_r+1) & \textrm{otherwise},
\end{cases}
$$
where $M$ and $E$ are the mantissa width and exponent width, respectively.
For zeros, infinities, or NaN
, refer to IEEE
754 or look at the examples below.
This function only outputs a single, canonical, NaN
.
§Worst-case complexity
Constant time and additional memory.
§Examples
See here.
Source§impl RawMantissaAndExponent<u64, u64> for f64
impl RawMantissaAndExponent<u64, u64> for f64
Source§fn raw_mantissa_and_exponent(self) -> (u64, u64)
fn raw_mantissa_and_exponent(self) -> (u64, u64)
Returns the raw mantissa and exponent.
The raw exponent and raw mantissa are the actual bit patterns used to represent the
components of self
. When self
is nonzero and finite, the raw exponent $e_r$ is
an integer in $[0, 2^E-2]$ and the raw mantissa $m_r$ is an integer in $[0, 2^M-1]$.
When self
is nonzero and finite, $f(x) = (m_r, e_r)$, where
$$
m_r = \begin{cases}
2^{M+2^{E-1}-2}|x| & \text{if} \quad |x| < 2^{2-2^{E-1},} \\
2^M \left ( \frac{|x|}{2^{\lfloor \log_2 |x| \rfloor}}-1\right ) &
\textrm{otherwise},
\end{cases}
$$
and
$$
e_r = \begin{cases}
0 & \text{if} \quad |x| < 2^{2-2^{E-1}} \\
\lfloor \log_2 |x| \rfloor + 2^{E-1} - 1 & \textrm{otherwise}.
\end{cases}
$$
and $M$ and $E$ are the mantissa width and exponent width, respectively.
For zeros, infinities, or NaN
, refer to IEEE
754 or look at the examples below.
The inverse operation is Self::from_raw_mantissa_and_exponent
.
§Worst-case complexity
Constant time and additional memory.
§Examples
See here.
Source§fn raw_mantissa(self) -> u64
fn raw_mantissa(self) -> u64
Returns the raw mantissa.
The raw mantissa is the actual bit pattern used to represent the mantissa of self
.
When self
is nonzero and finite, it is an integer in $[0, 2^M-1]$.
When self
is nonzero and finite,
$$
f(x) = \begin{cases}
2^{M+2^{E-1}-2}|x| & \text{if} \quad |x| < 2^{2-2^{E-1}}, \\
2^M \left ( \frac{|x|}{2^{\lfloor \log_2 |x| \rfloor}}-1\right )
& \textrm{otherwise},
\end{cases}
$$
where $M$ and $E$ are the mantissa width and exponent width, respectively.
For zeros, infinities, or NaN
, refer to IEEE
754 or look at the examples below.
§Worst-case complexity
Constant time and additional memory.
§Examples
See here.
Source§fn raw_exponent(self) -> u64
fn raw_exponent(self) -> u64
Returns the raw exponent.
The raw exponent is the actual bit pattern used to represent the exponent of self
.
When self
is nonzero and finite, it is an integer in $[0, 2^E-2]$.
When self
is nonzero and finite,
$$
f(x) = \begin{cases}
0 & \text{if} \quad |x| < 2^{2-2^{E-1}}, \\
\lfloor \log_2 |x| \rfloor + 2^{E-1} - 1 & \textrm{otherwise},
\end{cases}
$$
where $M$ and $E$ are the mantissa width and exponent width, respectively.
For zeros, infinities, or NaN
, refer to IEEE
754 or look at the examples below.
§Worst-case complexity
Constant time and additional memory.
§Examples
See here.
Source§fn from_raw_mantissa_and_exponent(raw_mantissa: u64, raw_exponent: u64) -> f64
fn from_raw_mantissa_and_exponent(raw_mantissa: u64, raw_exponent: u64) -> f64
Constructs a float from its raw mantissa and exponent.
The raw exponent and raw mantissa are the actual bit patterns used to represent the components of a float. When the float is nonzero and finite, the raw exponent $e_r$ is an integer in $[0, 2^E-2]$ and the raw mantissa $m_r$ is an integer in $[0, 2^M-1]$.
When the exponent is not 2^E-1
,
$$
f(m_r, e_r) = \begin{cases}
2^{2-2^{E-1}-M}m_r & \text{if} \quad e_r = 0, \\
2^{e_r-2^{E-1}+1}(2^{-M}m_r+1) & \textrm{otherwise},
\end{cases}
$$
where $M$ and $E$ are the mantissa width and exponent width, respectively.
For zeros, infinities, or NaN
, refer to IEEE
754 or look at the examples below.
This function only outputs a single, canonical, NaN
.
§Worst-case complexity
Constant time and additional memory.
§Examples
See here.