Skip to main content

feanor_math/rings/approx_real/
mod.rs

1use crate::field::Field;
2use crate::integer::IntegerRing;
3use crate::ordered::OrderedRing;
4use crate::ring::*;
5
6/// Contains [`float::Real64`] as implementation of [`ApproxRealField`]
7/// based on the primitive type [`f64`].
8pub mod float;
9
10/// Zero-sized struct that can be used as error value to indicate that
11/// the currently used [`ApproxRealField`] does not have sufficient precision
12/// to perform the demanded computation.
13#[derive(Debug, PartialEq, Eq)]
14#[stability::unstable(feature = "enable")]
15pub struct NotEnoughPrecision;
16
17/// Trait for rings that represent subfields of the reals, possibly
18/// being only approximate.
19///
20/// This should only be implemented for rings that behave in a sense
21/// like floating point approximations to the reals, with fixed precision.
22/// However, it is allowed to have the precision to be infinite, and
23/// possibly only represent subfields of the reals (e.g. it would be
24/// allowed to implement this for exact implementations of the rationals,
25/// but I don't think this would serve any purpose).
26#[stability::unstable(feature = "enable")]
27pub trait ApproxRealField: Field + OrderedRing {
28    /// Returns the closest integer to the given number.
29    ///
30    /// If the integer is not representable by `ZZ`, or the given
31    /// number is infinite, returns `None`.
32    fn round_to_integer<I>(&self, ZZ: I, x: Self::Element) -> Option<El<I>>
33    where
34        I: RingStore,
35        I::Type: IntegerRing;
36
37    /// Returns the difference between one and the next larger
38    /// representable number.
39    ///
40    /// If the precision of the ring is infinite, this should return 0.
41    fn epsilon(&self) -> &Self::Element;
42
43    /// Returns a value representing positive infinity.
44    fn infinity(&self) -> Self::Element;
45}
46
47/// Trait for rings that allow taking positive square roots of
48/// positive numbers.
49///
50/// The most common case are likely to be approximations to the
51/// real numbers, although it could also be implemented for certain
52/// infinite-degree number fields with embeddings into the reals.
53#[stability::unstable(feature = "enable")]
54pub trait SqrtRing: RingBase + OrderedRing {
55    /// Computes (possibly an approximation to) the unique real
56    /// number `y >= 0` such that `y^2 = 0`.
57    ///
58    /// If `x` is negative, this can either panic or return an
59    /// equivalent of "NaN" in the ring.
60    fn sqrt(&self, x: Self::Element) -> Self::Element;
61}