10

I'm new to Z3 and searched for the answer to my question here and on Google. Unfortunately, I was not successful.

I'm using the Z3 4.0 C/C++ API. I declared an undefined function d: (Int Int) Int, added some assertions, and computed a model. So far, that works fine.

Now, I want to extract certain values of the function d defined by the model, say d(0,0). The following statement works, but returns an expression rather than the function value, i.e., an integer, of d(0,0).

z3::expr args[] = {c.int_val(0), c.int_val(0)};
z3::expr result = m.eval(d(2, args));

The check

result.is_int();

returns true.

My (hopefully not too stupid) question is how to cast the returned expression to a C/C++ int?

Help is very appreciated. Thank you!

Dan
  • 1,539
  • 12
  • 23
  • +1 for a well written first question, you don't see that often! Unfortunately I can't help you so.. – Blindy Jul 25 '12 at 14:34

1 Answers1

7

Z3_get_numeral_int is what you're looking for.

Here is an excerpt from the docummentation:

Z3_bool Z3_get_numeral_int(__in Z3_context c, __in Z3_ast v, __out int * i)

Similar to Z3_get_numeral_string, but only succeeds if the value can fit in a 
machine int. Return Z3_TRUE if the call succeeded.

You should be careful though. Z3's integer is mathematical integer which can easily exceed the range of 32-bit int. In that sense, using Z3_get_numeral_string and parsing string to big integer is a better option.

pad
  • 41,040
  • 7
  • 92
  • 166
  • Thank you very much! That is exactly what I was looking for. And an amazingly fast reply ;-) – Dan Jul 26 '12 at 09:20