#![allow(
unstable_name_collisions,
clippy::assertions_on_constants,
clippy::cognitive_complexity,
clippy::many_single_char_names,
clippy::range_plus_one,
clippy::suspicious_arithmetic_impl,
clippy::suspicious_op_assign_impl,
clippy::too_many_arguments,
clippy::type_complexity,
clippy::upper_case_acronyms,
clippy::multiple_bound_locations
)]
#![warn(
clippy::cast_lossless,
clippy::explicit_into_iter_loop,
clippy::explicit_iter_loop,
clippy::filter_map_next,
clippy::large_digit_groups,
clippy::manual_filter_map,
clippy::manual_find_map,
clippy::map_flatten,
clippy::map_unwrap_or,
clippy::match_same_arms,
clippy::missing_const_for_fn,
clippy::mut_mut,
clippy::needless_borrow,
clippy::needless_continue,
clippy::needless_pass_by_value,
clippy::print_stdout,
clippy::redundant_closure_for_method_calls,
clippy::single_match_else,
clippy::trait_duplication_in_bounds,
clippy::type_repetition_in_bounds,
clippy::uninlined_format_args,
clippy::unused_self,
clippy::if_not_else,
clippy::manual_assert,
clippy::range_plus_one,
clippy::redundant_else,
clippy::semicolon_if_nothing_returned,
clippy::cloned_instead_of_copied,
clippy::flat_map_option,
clippy::unnecessary_wraps,
clippy::unnested_or_patterns,
clippy::use_self,
clippy::trivially_copy_pass_by_ref
)]
#![cfg_attr(
not(any(feature = "test_build", feature = "random", feature = "std")),
no_std
)]
extern crate alloc;
#[macro_use]
extern crate malachite_base;
#[cfg(feature = "test_build")]
extern crate itertools;
#[cfg(feature = "test_build")]
use crate::InnerFloat::Finite;
use core::cmp::Ordering::{self, *};
use core::ops::Deref;
#[cfg(feature = "test_build")]
use malachite_base::num::arithmetic::traits::DivisibleByPowerOf2;
use malachite_base::num::arithmetic::traits::IsPowerOf2;
use malachite_base::num::basic::floats::PrimitiveFloat;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::basic::traits::{Infinity, NegativeInfinity};
use malachite_base::num::conversion::traits::{ExactFrom, RoundingFrom, SciMantissaAndExponent};
#[cfg(feature = "test_build")]
use malachite_base::num::logic::traits::SignificantBits;
use malachite_base::rounding_modes::RoundingMode::*;
use malachite_nz::natural::Natural;
use malachite_nz::platform::Limb;
use malachite_q::Rational;
#[derive(Clone)]
pub struct Float(pub(crate) InnerFloat);
#[derive(Clone)]
pub(crate) enum InnerFloat {
NaN,
Infinity {
sign: bool,
},
Zero {
sign: bool,
},
Finite {
sign: bool,
exponent: i32,
precision: u64,
significand: Natural,
},
}
#[inline]
pub(crate) fn significand_bits(significand: &Natural) -> u64 {
significand.limb_count() << Limb::LOG_WIDTH
}
impl Float {
pub const MAX_EXPONENT: i32 = 0x3fff_ffff;
pub const MIN_EXPONENT: i32 = -Self::MAX_EXPONENT;
#[cfg(feature = "test_build")]
pub fn is_valid(&self) -> bool {
match self {
Self(Finite {
precision,
significand,
exponent,
..
}) => {
if *precision == 0
|| !significand.is_valid()
|| *exponent > Self::MAX_EXPONENT
|| *exponent < Self::MIN_EXPONENT
{
return false;
}
let bits = significand.significant_bits();
bits != 0
&& bits.divisible_by_power_of_2(Limb::LOG_WIDTH)
&& *precision <= bits
&& bits - precision < Limb::WIDTH
&& significand.divisible_by_power_of_2(bits - precision)
}
_ => true,
}
}
}
#[derive(Clone)]
pub struct ComparableFloat(pub Float);
#[derive(Clone)]
pub struct ComparableFloatRef<'a>(pub &'a Float);
impl ComparableFloat {
pub const fn as_ref(&self) -> ComparableFloatRef<'_> {
ComparableFloatRef(&self.0)
}
}
impl Deref for ComparableFloat {
type Target = Float;
fn deref(&self) -> &Float {
&self.0
}
}
impl Deref for ComparableFloatRef<'_> {
type Target = Float;
fn deref(&self) -> &Float {
self.0
}
}
#[allow(clippy::type_repetition_in_bounds)]
#[doc(hidden)]
pub fn emulate_float_to_float_fn<T: PrimitiveFloat, F: Fn(Float, u64) -> (Float, Ordering)>(
f: F,
x: T,
) -> T
where
Float: From<T> + PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
let x = Float::from(x);
let (mut result, o) = f(x.clone(), T::MANTISSA_WIDTH + 1);
if !result.is_normal() {
return T::exact_from(&result);
}
let e = i64::from(<&Float as SciMantissaAndExponent<Float, i32, _>>::sci_exponent(&result));
if e < T::MIN_NORMAL_EXPONENT {
if e < T::MIN_EXPONENT {
let rm =
if e == T::MIN_EXPONENT - 1 && result.significand_ref().unwrap().is_power_of_2() {
let down = if result > T::ZERO { Less } else { Greater };
if o == down { Up } else { Down }
} else {
Nearest
};
return T::rounding_from(&result, rm).0;
}
result = f(x, T::max_precision_for_sci_exponent(e)).0;
}
if result > T::MAX_FINITE {
T::INFINITY
} else if result < -T::MAX_FINITE {
T::NEGATIVE_INFINITY
} else {
T::exact_from(&result)
}
}
#[allow(clippy::type_repetition_in_bounds)]
#[doc(hidden)]
pub fn emulate_float_float_to_float_fn<
T: PrimitiveFloat,
F: Fn(Float, Float, u64) -> (Float, Ordering),
>(
f: F,
x: T,
y: T,
) -> T
where
Float: From<T> + PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
let x = Float::from(x);
let y = Float::from(y);
let (mut result, o) = f(x.clone(), y.clone(), T::MANTISSA_WIDTH + 1);
if !result.is_normal() {
return T::exact_from(&result);
}
let e = i64::from(<&Float as SciMantissaAndExponent<Float, i32, _>>::sci_exponent(&result));
if e < T::MIN_NORMAL_EXPONENT {
if e < T::MIN_EXPONENT {
let rm =
if e == T::MIN_EXPONENT - 1 && result.significand_ref().unwrap().is_power_of_2() {
let down = if result > T::ZERO { Less } else { Greater };
if o == down { Up } else { Down }
} else {
Nearest
};
return T::rounding_from(&result, rm).0;
}
result = f(x, y, T::max_precision_for_sci_exponent(e)).0;
}
if result > T::MAX_FINITE {
T::INFINITY
} else if result < -T::MAX_FINITE {
T::NEGATIVE_INFINITY
} else {
T::exact_from(&result)
}
}
#[allow(clippy::type_repetition_in_bounds)]
#[doc(hidden)]
pub fn emulate_rational_to_float_fn<T: PrimitiveFloat, F: Fn(&Rational, u64) -> (Float, Ordering)>(
f: F,
x: &Rational,
) -> T
where
Float: PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
let (mut result, o) = f(x, T::MANTISSA_WIDTH + 1);
if !result.is_normal() {
return T::exact_from(&result);
}
let e = i64::from(<&Float as SciMantissaAndExponent<Float, i32, _>>::sci_exponent(&result));
if e < T::MIN_NORMAL_EXPONENT {
if e < T::MIN_EXPONENT {
let rm =
if e == T::MIN_EXPONENT - 1 && result.significand_ref().unwrap().is_power_of_2() {
let down = if result > T::ZERO { Less } else { Greater };
if o == down { Up } else { Down }
} else {
Nearest
};
return T::rounding_from(&result, rm).0;
}
result = f(x, T::max_precision_for_sci_exponent(e)).0;
}
if result > T::MAX_FINITE {
T::INFINITY
} else if result < -T::MAX_FINITE {
T::NEGATIVE_INFINITY
} else {
T::exact_from(&result)
}
}
#[allow(clippy::type_repetition_in_bounds)]
#[doc(hidden)]
pub fn emulate_rational_rational_to_float_fn<
T: PrimitiveFloat,
F: Fn(&Rational, &Rational, u64) -> (Float, Ordering),
>(
f: F,
x: &Rational,
y: &Rational,
) -> T
where
Float: PartialOrd<T>,
for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
{
let (mut result, o) = f(x, y, T::MANTISSA_WIDTH + 1);
if !result.is_normal() {
return T::exact_from(&result);
}
let e = i64::from(<&Float as SciMantissaAndExponent<Float, i32, _>>::sci_exponent(&result));
if e < T::MIN_NORMAL_EXPONENT {
if e < T::MIN_EXPONENT {
let rm =
if e == T::MIN_EXPONENT - 1 && result.significand_ref().unwrap().is_power_of_2() {
let down = if result > T::ZERO { Less } else { Greater };
if o == down { Up } else { Down }
} else {
Nearest
};
return T::rounding_from(&result, rm).0;
}
result = f(x, y, T::max_precision_for_sci_exponent(e)).0;
}
if result > T::MAX_FINITE {
T::INFINITY
} else if result < -T::MAX_FINITE {
T::NEGATIVE_INFINITY
} else {
T::exact_from(&result)
}
}
pub fn test_overflow(result: &Float, o: Ordering) -> bool {
if o == Equal {
return false;
}
*result == Float::INFINITY && o == Greater
|| *result == Float::NEGATIVE_INFINITY && o == Less
|| *result > 0u32 && result.abs_is_max_finite_value_with_prec() && o == Less
|| *result < 0u32 && result.abs_is_max_finite_value_with_prec() && o == Greater
}
pub fn test_underflow(result: &Float, o: Ordering) -> bool {
if o == Equal {
return false;
}
*result == 0u32
|| *result > 0u32 && result.abs_is_min_positive_value() && o == Greater
|| *result < 0u32 && result.abs_is_min_positive_value() && o == Less
}
pub mod arithmetic;
#[macro_use]
pub mod basic;
pub mod comparison;
pub mod constants;
pub mod conversion;
pub mod exhaustive;
#[cfg(feature = "random")]
pub mod random;
#[cfg(feature = "test_build")]
pub mod test_util;