mirror of
https://github.com/Z3Prover/z3
synced 2025-04-23 17:15:31 +00:00
Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
parent
5d9ed5b0a9
commit
67c4777514
11 changed files with 251 additions and 126 deletions
|
@ -85,3 +85,48 @@ void rational::finalize() {
|
|||
DEALLOC_MUTEX(g_powers_of_two);
|
||||
}
|
||||
|
||||
bool rational::limit_denominator(rational &num, rational const& limit) {
|
||||
rational n, d;
|
||||
n = numerator(num);
|
||||
d = denominator(num);
|
||||
if (d < limit) return false;
|
||||
|
||||
/*
|
||||
Iteratively computes approximation using continuous fraction
|
||||
decomposition
|
||||
|
||||
p(-1) = 0, p(0) = 1
|
||||
p(j) = t(j)*p(j-1) + p(j-2)
|
||||
|
||||
q(-1) = 1, q(0) = 0
|
||||
q(j) = t(j)*q(j-1) + q(j-2)
|
||||
|
||||
cf[t1; t2, ..., tr] = p(r) / q(r) for r >= 1
|
||||
reference: https://www.math.u-bordeaux.fr/~pjaming/M1/exposes/MA2.pdf
|
||||
*/
|
||||
|
||||
rational p0(0), p1(1);
|
||||
rational q0(1), q1(0);
|
||||
|
||||
while (!d.is_zero()) {
|
||||
rational tj(0), rem(0);
|
||||
rational p2(0), q2(0);
|
||||
tj = div(n, d);
|
||||
|
||||
q2 = tj * q1 + q0;
|
||||
p2 = tj * p1 + p0;
|
||||
if (q2 >= limit) {
|
||||
num = p2/q2;
|
||||
return true;
|
||||
}
|
||||
rem = n - tj * d;
|
||||
p0 = p1;
|
||||
p1 = p2;
|
||||
q0 = q1;
|
||||
q1 = q2;
|
||||
n = d;
|
||||
d = rem;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -421,6 +421,8 @@ public:
|
|||
}
|
||||
return num_bits;
|
||||
}
|
||||
|
||||
static bool limit_denominator(rational &num, rational const& limit);
|
||||
};
|
||||
|
||||
inline bool operator!=(rational const & r1, rational const & r2) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue