Function malachite_base::num::exhaustive::exhaustive_primitive_floats_with_sci_exponent_and_precision
source · pub fn exhaustive_primitive_floats_with_sci_exponent_and_precision<T: PrimitiveFloat>(
sci_exponent: i64,
precision: u64,
) -> ConstantPrecisionPrimitiveFloats<T> ⓘ
Expand description
Generates all finite positive primitive floats with a specified sci_exponent
and precision.
Positive and negative zero are both excluded.
A finite positive primitive float may be uniquely expressed as $x = m_s2^e_s$, where $1 \leq m_s < 2$ and $e_s$ is an integer; then $e_s$ is the sci-exponent. An integer $e_s$ occurs as the sci-exponent of a float iff $2-2^{E-1}-M \leq e_s < 2^{E-1}$.
In the above equation, $m$ is a dyadic rational. Let $p$ be the smallest integer such that $m2^{p-1}$ is an integer. Then $p$ is the float’s precision. It is also the number of significant bits.
For example, consider the float $100.0$. It may be written as $\frac{25}{16}2^6$, so $m=\frac{25}{16}$ and $e=6$. We can write $m$ in binary as $1.1001_2$. Thus, the sci-exponent is 6 and the precision is 5.
If $p$ is 1, the output length is 1; otherwise, it is $2^{p-2}$.
§Complexity per iteration
Constant time and additional memory.
§Panics
Panics if the sci-exponent is less than
MIN_EXPONENT
or greater than
MAX_EXPONENT
, or if the precision is
zero or too large for the given sci-exponent (this can be checked using
max_precision_for_sci_exponent
).
§Examples
use itertools::Itertools;
use malachite_base::num::exhaustive::*;
use malachite_base::num::float::NiceFloat;
assert_eq!(
exhaustive_primitive_floats_with_sci_exponent_and_precision::<f32>(0, 3)
.map(NiceFloat)
.collect_vec(),
[1.25, 1.75].iter().copied().map(NiceFloat).collect_vec()
);
assert_eq!(
exhaustive_primitive_floats_with_sci_exponent_and_precision::<f32>(0, 5)
.map(NiceFloat)
.collect_vec(),
[1.0625, 1.1875, 1.3125, 1.4375, 1.5625, 1.6875, 1.8125, 1.9375]
.iter()
.copied()
.map(NiceFloat)
.collect_vec()
);
assert_eq!(
exhaustive_primitive_floats_with_sci_exponent_and_precision::<f32>(6, 5)
.map(NiceFloat)
.collect_vec(),
[68.0, 76.0, 84.0, 92.0, 100.0, 108.0, 116.0, 124.0]
.iter()
.copied()
.map(NiceFloat)
.collect_vec()
);