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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
use crate::natural::Natural;
use malachite_base::named::Named;
use malachite_base::num::arithmetic::traits::DivisibleByPowerOf2;
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::conversion::traits::{
CheckedFrom, ConvertibleFrom, ExactFrom, RawMantissaAndExponent, RoundingFrom,
SciMantissaAndExponent, WrappingFrom,
};
use malachite_base::rounding_modes::RoundingMode;
macro_rules! float_impls {
($f: ident) => {
impl<'a> RoundingFrom<&'a Natural> for $f {
/// Converts a [`Natural`] to a primitive float according to a specified
/// [`RoundingMode`](malachite_base::rounding_modes::RoundingMode).
///
/// - If the rounding mode is `Floor` or `Down`, the largest float less than or equal
/// to the [`Natural`] is returned. If the [`Natural`] is greater than the maximum
/// finite float, then the maximum finite float is returned.
/// - If the rounding mode is `Ceiling` or `Up`, the smallest float greater than or
/// equal to the [`Natural`] is returned. If the [`Natural`] is greater than the
/// maximum finite float, then positive infinity is returned.
/// - If the rounding mode is `Nearest`, then the nearest float is returned. If the
/// [`Natural`] is exactly between two floats, the float with the zero
/// least-significant bit in its representation is selected. If the [`Natural`] is
/// greater than the maximum finite float, then the maximum finite float is returned.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `value.significant_bits()`.
///
/// # Panics
/// Panics if the rounding mode is `Exact` and `value` cannot be represented exactly.
///
/// # Examples
/// See [here](super::primitive_float_from_natural#rounding_from).
fn rounding_from(value: &'a Natural, rm: RoundingMode) -> $f {
if *value == 0 {
0.0
} else {
let (mantissa, exponent) = value
.sci_mantissa_and_exponent_with_rounding(rm)
.expect("Value cannot be represented exactly as a float");
if let Some(f) =
$f::from_sci_mantissa_and_exponent(mantissa, i64::exact_from(exponent))
{
f
} else {
match rm {
RoundingMode::Exact => {
panic!("Value cannot be represented exactly as an {}", $f::NAME)
}
RoundingMode::Floor | RoundingMode::Down | RoundingMode::Nearest => {
$f::MAX_FINITE
}
_ => $f::POSITIVE_INFINITY,
}
}
}
}
}
impl<'a> From<&'a Natural> for $f {
/// Converts a [`Natural`] to a primitive float.
///
/// If there are two nearest floats, the one whose least-significant bit is zero is
/// chosen. If the [`Natural`] is larger than the maximum finite float, then the result
/// is the maximum finite float.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `value.significant_bits()`.
///
/// # Examples
/// See [here](super::primitive_float_from_natural#from).
#[inline]
fn from(value: &'a Natural) -> $f {
$f::rounding_from(value, RoundingMode::Nearest)
}
}
impl<'a> CheckedFrom<&'a Natural> for $f {
/// Converts a [`Natural`] to a primitive float.
///
/// If the input isn't exactly equal to some float, `None` is returned.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `value.significant_bits()`.
///
/// # Examples
/// See [here](super::primitive_float_from_natural#checked_from).
fn checked_from(value: &'a Natural) -> Option<$f> {
if *value == 0 {
Some(0.0)
} else {
let (mantissa, exponent) =
value.sci_mantissa_and_exponent_with_rounding(RoundingMode::Exact)?;
$f::from_sci_mantissa_and_exponent(mantissa, i64::exact_from(exponent))
}
}
}
impl<'a> ConvertibleFrom<&'a Natural> for $f {
/// Determines whether a [`Natural`] can be exactly converted to a primitive float.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(1)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `value.significant_bits()`.
///
/// # Examples
/// See [here](super::primitive_float_from_natural#convertible_from).
fn convertible_from(value: &'a Natural) -> bool {
if *value == 0 {
true
} else {
if let Some((mantissa, exponent)) =
value.sci_mantissa_and_exponent_with_rounding::<$f>(RoundingMode::Exact)
{
let exponent = i64::exact_from(exponent);
if !($f::MIN_EXPONENT..=$f::MAX_EXPONENT).contains(&exponent) {
return false;
}
let (orig_mantissa, orig_exponent) = mantissa.raw_mantissa_and_exponent();
orig_exponent == u64::wrapping_from($f::MAX_EXPONENT)
&& exponent >= $f::MIN_NORMAL_EXPONENT
|| orig_mantissa.divisible_by_power_of_2(u64::wrapping_from(
$f::MIN_NORMAL_EXPONENT - exponent,
))
} else {
false
}
}
}
}
};
}
apply_to_primitive_floats!(float_impls);