#![forbid(unsafe_code)]
#![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;
extern crate malachite_nz;
#[cfg(feature = "serde")]
#[macro_use]
extern crate serde;
#[cfg(feature = "test_build")]
extern crate itertools;
#[cfg(feature = "test_build")]
extern crate num;
#[cfg(feature = "test_build")]
extern crate rug;
use malachite_base::named::Named;
#[cfg(feature = "test_build")]
use malachite_base::num::arithmetic::traits::CoprimeWith;
use malachite_base::num::basic::traits::{NegativeOne, One, OneHalf, Two, Zero};
use malachite_base::num::logic::traits::SignificantBits;
use malachite_nz::natural::Natural;
#[derive(Clone, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct Rational {
#[cfg_attr(feature = "serde", serde(rename = "s"))]
pub(crate) sign: bool,
#[cfg_attr(feature = "serde", serde(rename = "n"))]
pub(crate) numerator: Natural,
#[cfg_attr(feature = "serde", serde(rename = "d"))]
pub(crate) denominator: Natural,
}
impl Rational {
#[cfg(feature = "test_build")]
pub fn is_valid(&self) -> bool {
self.denominator != 0
&& (self.sign || self.numerator != 0)
&& (&self.numerator).coprime_with(&self.denominator)
}
}
impl SignificantBits for &Rational {
fn significant_bits(self) -> u64 {
self.numerator.significant_bits() + self.denominator.significant_bits()
}
}
impl Zero for Rational {
const ZERO: Self = Self {
sign: true,
numerator: Natural::ZERO,
denominator: Natural::ONE,
};
}
impl One for Rational {
const ONE: Self = Self {
sign: true,
numerator: Natural::ONE,
denominator: Natural::ONE,
};
}
impl Two for Rational {
const TWO: Self = Self {
sign: true,
numerator: Natural::TWO,
denominator: Natural::ONE,
};
}
impl NegativeOne for Rational {
const NEGATIVE_ONE: Self = Self {
sign: false,
numerator: Natural::ONE,
denominator: Natural::ONE,
};
}
impl OneHalf for Rational {
const ONE_HALF: Self = Self {
sign: true,
numerator: Natural::ONE,
denominator: Natural::TWO,
};
}
impl Default for Rational {
fn default() -> Self {
Self::ZERO
}
}
impl_named!(Rational);
pub mod arithmetic;
pub mod comparison;
pub mod conversion;
pub mod exhaustive;
#[cfg(feature = "random")]
pub mod random;
#[cfg(feature = "test_build")]
pub mod test_util;