diff --git a/examples/c++/example.cpp b/examples/c++/example.cpp index 0e2cab37e0b..20bc89a461e 100644 --- a/examples/c++/example.cpp +++ b/examples/c++/example.cpp @@ -802,11 +802,19 @@ void tactic_qe() { std::cout << s.get_model() << "\n"; } -void visit(expr const & e) { +void visit(std::vector& 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 @@ -814,7 +822,7 @@ void visit(expr const & e) { std::cout << "application of " << f.name() << ": " << e << "\n"; } else if (e.is_quantifier()) { - visit(e.body()); + visit(visited, e.body()); // do something } else { @@ -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 visited; + visit(visited, f); } void tst_numeral() { + std::cout << "numeral example\n"; context c; expr x = c.real_val("1/3"); double d = 0; diff --git a/src/api/api_numeral.cpp b/src/api/api_numeral.cpp index 831b3503d57..372840b9ee8 100644 --- a/src/api/api_numeral.cpp +++ b/src/api/api_numeral.cpp @@ -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) {