real_number.hpp - Generators of Modules#
-
class RealNumber#
A (possibly transcendental) real number.
Real numbers can be generated with factory functions (see below) and then be used as the generators of a Module, i.e., a submodule of the real numbers over some base ring.
#include <exact-real/module.hpp> #include <exact-real/real_number.hpp> #include <exact-real/element.hpp> #include <exact-real/integer_ring.hpp> #include <exact-real/rational_field.hpp> #include <exact-real/number_field.hpp> auto M = exactreal::Module<exactreal::IntegerRing>::make({ exactreal::RealNumber::rational(1), exactreal::RealNumber::random()}); *M // -> ℤ-Module(1, ℝ(<...>))
Comparison Operators
There are operators
<
,<=
,==
,!=
,>
,>=
with other real numbers, primitive integer types, GMP integers and rationals, and Arf floating points numbers.Note that many of these operators are provided by boost operators and not listed explicitly below.
const auto x = exactreal::RealNumber::rational(mpq_class{1, 3}); *x < 1 // -> true *x < mpz_class{1} // -> true *x == mpq_class{1, 3} // -> true *x == x->arf(64) // -> false
-
bool operator<(const RealNumber&) const#
-
bool operator==(const RealNumber&) const#
-
template<typename Integer>
std::enable_if_t<std::is_integral_v<Integer>, bool> operator<(Integer) const noexcept#
-
template<typename Integer>
std::enable_if_t<std::is_integral_v<Integer>, bool> operator>(Integer) const noexcept#
-
template<typename Integer>
std::enable_if_t<std::is_integral_v<Integer>, bool> operator==(Integer) const noexcept#
-
bool operator<(const mpq_class&) const#
-
bool operator>(const mpq_class&) const#
-
bool operator==(const mpq_class&) const#
-
bool operator<(const mpz_class&) const noexcept#
-
bool operator>(const mpz_class&) const noexcept#
-
bool operator==(const mpz_class&) const noexcept#
Factory Functions
-
static std::shared_ptr<const RealNumber> random()#
Return a random real in the range [0, 1]
-
static std::shared_ptr<const RealNumber> random(Seed seed)#
Return a random real whose random digits are taken from a fixed
seed
.
-
static std::shared_ptr<const RealNumber> random(const Arf &a, const Arf &b)#
Return a random real in the range [a, b].
-
static std::shared_ptr<const RealNumber> random(const Arf &a, const Arf &b, Seed seed)#
Return a random real in the range [a, b] whose random digits are taken from a fixed
seed
.
-
static std::shared_ptr<const RealNumber> random(const double d)#
Return a random real number, close to
d
.This is mostly useful to port code from doubles that are meant to resemble random reals.
const auto x = exactreal::RealNumber::random(1.); *x // -> ℝ(1=<...> + ℝ(<...>)p-64)
-
static std::shared_ptr<const RealNumber> random(const double d, Seed seed)#
Return a random real number, close to
d
whose random digits are taken from a fixedseed
.
-
static std::shared_ptr<const RealNumber> rational(const mpq_class&)#
Return a real number that is an exact rational.
Public Functions
-
virtual ~RealNumber()#
-
explicit operator double() const#
Return the closest double to this real; ties are rounded to even.
const auto x = exactreal::RealNumber::rational(1); static_cast<double>(*x) // -> 1
-
explicit operator bool() const#
Return whether this real number is non-zero.
const auto x = exactreal::RealNumber::rational(0); static_cast<bool>(*x) // -> false
-
virtual explicit operator std::optional<mpq_class>() const = 0#
Return this real number as a rational number if possible.
const auto x = exactreal::RealNumber::rational(1); static_cast<std::optional<mpq_class>>(*x).value() // -> 1 const auto y = exactreal::RealNumber::random(); static_cast<std::optional<mpq_class>>(*y).has_value() // -> false
-
virtual Arf arf(long prec) const = 0#
Return an approximation of this number as an Arf float with
prec
bits of relative accuracy, i.e., if \( e = \frac{|x - ~x|}{|x|} \) is the relative error, then \( \log_2(1/e) \ge \mathrm{prec} \).For example, for a random real number, this method just returns the first prec binary digits after the first non-zero digit.
#include <exact-real/arf.hpp> const auto x = exactreal::RealNumber::rational(mpq_class{1, 3}); x->arf(3) // -> 0.3125=5p-4 x->arf(64) // -> 0.333333=12297829382473034411p-65
-
Arb arb(long prec) const#
Return an Arb with
prec
bits of relative accuracy which contains this number, i.e., the returned value satisfiesarb_rel_accuracy_bits(x.arb_t()) == prec
.#include <exact-real/arb.hpp> const auto x = exactreal::RealNumber::rational(mpq_class{1, 3}); x->arb(3) // -> [0.312500 +/- 0.0157]
Note that the printed output here for the radius can be misleading since printing via
std::cout
(which is used here) normally only requests a small number of digits of precision from the underlyingarb_get_str()
.x->arb(64) // -> [0.333333 +/- 3.34e-7] std::cout << std::setprecision(32) << x->arb(64); // -> [0.33333333333333333334236835143738 +/- 6.78e-21]
-
void refine(Arb &arb, long prec) const#
Shrink the ball
arb
such that contains this number and hasprec
bits of relative accuracy, i.e.,arb_rel_accuracy_bits(arb.arb_t()) >= / prec
.const auto x = exactreal::RealNumber::rational(mpq_class{1, 3}); exactreal::Arb a(std::pair{exactreal::Arf(0), exactreal::Arf(1)}); a // -> [0.500000 +/- 0.501] x->refine(a, 2); a // -> [0.375000 +/- 0.0313] x->refine(a, 16); a // -> [0.333336 +/- 2.04e-6]
-
bool deglex(const RealNumber &rhs) const#
Return whether this real number, interpreted as a multivariate monomial is smaller than
rhs
; This interprets most real numbers as indeterminates and their products as products of these basic indeterminates. This is used internally for the operator/. The indeterminates, i.e., real numbers, are ordered consistently (by some internal identifiers.)
-
virtual std::shared_ptr<const RealNumber> operator*(const RealNumber &rhs) const#
Return the product of this real number and
rhs
.const auto x = exactreal::RealNumber::rational(mpq_class{1, 3}); const auto xx = *x * *x; *xx // -> 1/9 const auto y = exactreal::RealNumber::random(); const auto yy = *y * *y; *yy // -> ℝ(<...>)^2 const auto z = exactreal::RealNumber::random(); const auto yz = *y * *z; *yz // -> ℝ(<...>)*ℝ(<...>)
-
virtual std::optional<std::shared_ptr<const RealNumber>> operator/(const RealNumber &rhs) const#
Return the exact quotient of this real number divided by
rhs
.Returns an
std::nullopt
when the exact quotient cannot be represented.const auto y = exactreal::RealNumber::random(); const auto z = (*y) * (*y); auto quo = *z / *z; *quo.value() // -> 1 quo = *z / *y; *quo.value() // -> ℝ(<...>) quo = *y / *z; quo.has_value() // -> false
-
virtual RealNumber const &operator>>(std::ostream&) const = 0#
Write this real to the output stream.
This is a helper function for the friend
operator<<
to allow implementations to override how printing works.
Public Static Functions
Friends
-
friend std::ostream &operator<<(std::ostream&, const RealNumber&)#
-
bool operator<(const RealNumber&) const#