Skip to content

Commit

Permalink
add visitor example, fix double conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Nikolaj Bjorner <[email protected]>
  • Loading branch information
NikolajBjorner committed Oct 11, 2019
1 parent 4fc64ab commit a990e7f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
21 changes: 15 additions & 6 deletions examples/c++/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,19 +802,27 @@ void tactic_qe() {
std::cout << s.get_model() << "\n";
}

void visit(expr const & e) {
void visit(std::vector<bool>& visited, expr const & e) {
if (visited.size() <= e.id()) {
visited.resize(e.id()+1, false);
}
if (visited[e.id()]) {
return;
}
visited[e.id()] = true;

if (e.is_app()) {
unsigned num = e.num_args();
for (unsigned i = 0; i < num; i++) {
visit(e.arg(i));
visit(visited, e.arg(i));
}
// do something
// Example: print the visited expression
func_decl f = e.decl();
std::cout << "application of " << f.name() << ": " << e << "\n";
}
else if (e.is_quantifier()) {
visit(e.body());
visit(visited, e.body());
// do something
}
else {
Expand All @@ -830,12 +838,13 @@ void tst_visit() {
expr x = c.int_const("x");
expr y = c.int_const("y");
expr z = c.int_const("z");
expr f = x*x - y*y >= 0;

visit(f);
expr f = x*x + x*x - y*y >= 0;
std::vector<bool> visited;
visit(visited, f);
}

void tst_numeral() {
std::cout << "numeral example\n";
context c;
expr x = c.real_val("1/3");
double d = 0;
Expand Down
20 changes: 14 additions & 6 deletions src/api/api_numeral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,21 @@ extern "C" {
expr* e = to_expr(a);
fpa_util & fu = mk_c(c)->fpautil();
scoped_mpf tmp(fu.fm());
if (!mk_c(c)->fpautil().is_numeral(e, tmp) ||
tmp.get().get_ebits() > 11 ||
tmp.get().get_sbits() > 53) {
SET_ERROR_CODE(Z3_INVALID_ARG, nullptr);
return NAN;
if (mk_c(c)->fpautil().is_numeral(e, tmp)) {
if (tmp.get().get_ebits() > 11 ||
tmp.get().get_sbits() > 53) {
SET_ERROR_CODE(Z3_INVALID_ARG, nullptr);
return NAN;
}
return fu.fm().to_double(tmp);
}
return fu.fm().to_double(tmp);
rational r;
arith_util & u = mk_c(c)->autil();
if (u.is_numeral(e, r)) {
return r.get_double();
}
SET_ERROR_CODE(Z3_INVALID_ARG, nullptr);
return 0.0;
}

Z3_string Z3_API Z3_get_numeral_decimal_string(Z3_context c, Z3_ast a, unsigned precision) {
Expand Down

0 comments on commit a990e7f

Please sign in to comment.