1 #include "../numbers.h"
7 #include <boost/numeric/interval.hpp>
13 cln::cl_RA rationalize<cln::cl_RA>(
double n) {
14 switch (std::fpclassify(n)) {
18 static_assert(
sizeof(n) == 8,
"double is assumed to be eight bytes wide.");
19 auto significandBits =
reinterpret_cast<sint>(&n);
20 significandBits = (significandBits << 12) >> 12;
22 significandBits = -significandBits;
36 cln::cl_RA rationalize<cln::cl_RA>(
float n)
38 switch (std::fpclassify(n))
43 static_assert(
sizeof(n) == 4,
"float is assumed to be four bytes wide.");
44 auto significandBits =
reinterpret_cast<sint>(&n);
45 significandBits = (significandBits << 9) >> 9;
47 significandBits = -significandBits;
60 bool sqrt_exact(
const cln::cl_RA& a, cln::cl_RA& b)
62 if( a < 0 )
return false;
63 return cln::sqrtp( a, &b );
66 cln::cl_RA
sqrt(
const cln::cl_RA& a)
69 return (r.first + r.second) / 2;
72 cln::cl_RA scaleByPowerOfTwo(
const cln::cl_RA& a,
int exp) {
74 return cln::cl_RA(cln::numerator(a) <<
exp) / cln::denominator(a);
76 return cln::cl_RA(cln::numerator(a)) / (cln::denominator(a) << -
exp);
81 std::pair<cln::cl_RA, cln::cl_RA>
sqrt_safe(
const cln::cl_RA& a)
84 cln::cl_RA exact_root;
85 if (cln::sqrtp(a, &exact_root)) {
87 return std::make_pair(exact_root, exact_root);
89 auto factor = int(cln::integer_length(cln::denominator(a))) - int(cln::integer_length(cln::numerator(a)));
90 if (cln::oddp(factor)) factor += 1;
91 cln::cl_RA n = scaleByPowerOfTwo(a, factor);
94 boost::numeric::interval<double> i;
100 assert(n <= 2*n-nra);
104 std::nexttoward(i.lower(), -std::numeric_limits<double>::infinity()),
105 std::nexttoward(i.upper(), std::numeric_limits<double>::infinity())
110 assert(lower*lower <= a);
111 assert(a <= upper*upper);
112 return std::make_pair(lower, upper);
116 std::pair<cln::cl_RA, cln::cl_RA>
sqrt_fast(
const cln::cl_RA& a)
119 cln::cl_RA exact_root;
120 if (cln::sqrtp(a, &exact_root)) {
122 return std::make_pair(exact_root, exact_root);
128 cln::cl_I upper = lower + 1;
129 assert(cln::expt_pos(lower,2) < a);
130 assert(cln::expt_pos(upper,2) > a);
131 return std::make_pair(lower, upper);
135 std::pair<cln::cl_RA, cln::cl_RA>
root_safe(
const cln::cl_RA& a,
uint n) {
136 auto res = cln::realpart(cln::expt(a, cln::cl_RA(1)/n));
137 return std::make_pair(cln::cl_RA(cln::floor1(res)), cln::cl_RA(cln::ceiling1(res)));
141 cln::cl_I parse<cln::cl_I>(
const std::string& n) {
149 bool try_parse<cln::cl_I>(
const std::string& n, cln::cl_I& res) {
154 cln::cl_RA parse<cln::cl_RA>(
const std::string& n) {
162 bool try_parse<cln::cl_RA>(
const std::string& n, cln::cl_RA& res) {
166 std::string
toString(
const cln::cl_RA& _number,
bool _infix)
169 bool negative = (_number < cln::cl_RA(0));
170 if(negative) s <<
"(-" << (_infix ?
"" :
" ");
183 std::string
toString(
const cln::cl_I& _number,
bool _infix)
186 bool negative = (_number < cln::cl_I(0));
187 if(negative) s <<
"(-" << (_infix ?
"" :
" ");
carl is the main namespace for the library.
T rationalize(const PreventConversion< mpq_class > &)
std::pair< cln::cl_RA, cln::cl_RA > sqrt_fast(const cln::cl_RA &a)
Compute square root in a fast but less precise way.
std::string toString(Relation r)
Interval< Number > abs(const Interval< Number > &_in)
Method which returns the absolute value of the passed number.
bool sqrt_exact(const cln::cl_RA &a, cln::cl_RA &b)
Calculate the square root of a fraction if possible.
static const cln::cl_RA ONE_DIVIDED_BY_10_TO_THE_POWER_OF_23
std::pair< cln::cl_RA, cln::cl_RA > root_safe(const cln::cl_RA &a, uint n)
Interval< Number > exp(const Interval< Number > &i)
mpq_class sqrt(const mpq_class &a)
cln::cl_I get_num(const cln::cl_RA &n)
Extract the numerator from a fraction.
Interval< Number > sqrt(const Interval< Number > &i)
cln::cl_LF to_lf(const cln::cl_RA &n)
Convert a cln fraction to a cln long float.
static const cln::cl_RA ONE_DIVIDED_BY_10_TO_THE_POWER_OF_52
cln::cl_I get_denom(const cln::cl_RA &n)
Extract the denominator from a fraction.
double to_double(const cln::cl_RA &n)
Converts the given fraction to a double.
std::pair< cln::cl_RA, cln::cl_RA > sqrt_safe(const cln::cl_RA &a)
Calculate the square root of a fraction.
bool parseDecimal(const std::string &input, T &output)
bool parseRational(const std::string &input, T &output)