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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
use crate::Float;
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::traits::{Infinity, NaN, NegativeInfinity, NegativeZero, Zero};
use malachite_base::num::logic::traits::SignificantBits;
use malachite_base::rounding_modes::RoundingMode;
use std::cmp::Ordering;
// This differs from the `precision` function provided by `PrimitiveFloat`. That function is the
// smallest precision necessary to represent the float, whereas this function returns the maximum
// precision of any float in the same binade. If the float is non-finite or zero, 1 is returned.
pub_test! {alt_precision<T: PrimitiveFloat>(x: T) -> u64 {
if x.is_finite() && x != T::ZERO {
let (mantissa, exponent) = x.raw_mantissa_and_exponent();
if exponent == 0 {
mantissa.significant_bits()
} else {
T::MANTISSA_WIDTH + 1
}
} else {
1
}
}}
impl Float {
#[doc(hidden)]
pub fn from_primitive_float_times_power_of_2<T: PrimitiveFloat>(x: T, pow: i64) -> Float {
if x.is_nan() {
Float::NAN
} else if !x.is_finite() {
if x.is_sign_positive() {
Float::INFINITY
} else {
Float::NEGATIVE_INFINITY
}
} else if x == T::ZERO {
if x.is_sign_positive() {
Float::ZERO
} else {
Float::NEGATIVE_ZERO
}
} else {
let (m, e) = x.integer_mantissa_and_exponent();
let abs = Float::from_unsigned_times_power_of_2_prec(m, e + pow, alt_precision(x)).0;
if x.is_sign_positive() {
abs
} else {
-abs
}
}
}
#[doc(hidden)]
pub fn from_primitive_float_times_power_of_2_prec_round<T: PrimitiveFloat>(
x: T,
pow: i64,
prec: u64,
rm: RoundingMode,
) -> (Float, Ordering) {
assert_ne!(prec, 0);
if x.is_nan() {
(Float::NAN, Ordering::Equal)
} else if !x.is_finite() {
if x.is_sign_positive() {
(Float::INFINITY, Ordering::Equal)
} else {
(Float::NEGATIVE_INFINITY, Ordering::Equal)
}
} else if x == T::ZERO {
if x.is_sign_positive() {
(Float::ZERO, Ordering::Equal)
} else {
(Float::NEGATIVE_ZERO, Ordering::Equal)
}
} else {
let (m, e) = x.integer_mantissa_and_exponent();
if x.is_sign_positive() {
Float::from_unsigned_times_power_of_2_prec_round(m, e + pow, prec, rm)
} else {
let (abs, o) =
Float::from_unsigned_times_power_of_2_prec_round(m, e + pow, prec, -rm);
(-abs, o.reverse())
}
}
}
#[doc(hidden)]
#[inline]
pub fn from_primitive_float_times_power_of_2_prec<T: PrimitiveFloat>(
x: T,
pow: i64,
prec: u64,
) -> (Float, Ordering) {
Float::from_primitive_float_times_power_of_2_prec_round(x, pow, prec, RoundingMode::Nearest)
}
/// Converts a primitive float to a [`Float`]. If the [`Float`] is nonzero and finite, it has
/// the specified precision. If rounding is needed, the specified rounding mode is used. An
/// [`Ordering`] is also returned, indicating whether the returned value is less than, equal
/// to, or greater than the original value. (Although a NaN is not comparable to anything,
/// converting a NaN to a NaN will also return `Ordering::Equals`, indicating an exact
/// conversion.)
///
/// If you're only using [`RoundingMode::Nearest`], try using
/// [`Float::from_primitive_float_prec`] instead.
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(prec, x.sci_exponent().abs())`.
///
/// # Examples
/// See [here](super::from_primitive_float#from_primitive_float_prec_round).
#[inline]
pub fn from_primitive_float_prec_round<T: PrimitiveFloat>(
x: T,
prec: u64,
rm: RoundingMode,
) -> (Float, Ordering) {
Float::from_primitive_float_times_power_of_2_prec_round(x, 0, prec, rm)
}
/// Converts a primitive float to a [`Float`]. If the [`Float`] is nonzero and finite, it has
/// the specified precision. An [`Ordering`] is also returned, indicating whether the returned
/// value is less than, equal to, or greater than the original value. (Although a NaN is not
/// comparable to anything, converting a NaN to a NaN will also return `Ordering::Equals`,
/// indicating an exact conversion.)
///
/// Rounding may occur, in which case [`RoundingMode::Nearest`] is used by default. To specify
/// a rounding mode as well as a precision, try [`Float::from_primitive_float_prec_round`].
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is
/// `max(prec, x.sci_exponent().abs())`.
///
/// # Examples
/// See [here](super::from_primitive_float#from_primitive_float_prec).
#[inline]
pub fn from_primitive_float_prec<T: PrimitiveFloat>(x: T, prec: u64) -> (Float, Ordering) {
Float::from_primitive_float_times_power_of_2_prec_round(x, 0, prec, RoundingMode::Nearest)
}
}
macro_rules! impl_from_primitive_float {
($t: ident) => {
impl From<$t> for Float {
/// Converts a primitive float to a [`Float`].
///
/// If the primitive float is finite and nonzero, the precision of the [`Float`] is
/// equal to the maximum precision of any primitive float in the same binade (for
/// normal `f32`s this is 24, and for normal `f64`s it is 53). If you want to specify a
/// different precision, try [`Float::from_primitive_float_prec`]. This may require
/// rounding, which uses [`RoundingMode::Nearest`] by default. To specify a rounding
/// mode as well as a precision, try [`Float::from_primitive_float_prec_round`].
///
/// # Worst-case complexity
/// $T(n) = O(n)$
///
/// $M(n) = O(n)$
///
/// where $T$ is time, $M$ is additional memory, and $n$ is `x.sci_exponent().abs()`.
///
/// # Examples
/// See [here](super::from_primitive_float#from).
#[inline]
fn from(x: $t) -> Float {
Float::from_primitive_float_times_power_of_2(x, 0)
}
}
};
}
apply_to_primitive_floats!(impl_from_primitive_float);