Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add creating reals from any numeral and getting f64 from reals #287

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions z3/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,17 @@ impl<'ctx> Real<'ctx> {
}

pub fn from_real_str(ctx: &'ctx Context, num: &str, den: &str) -> Option<Real<'ctx>> {
let fraction_string = format!("{num:} / {den:}");
Self::from_str(ctx, &fraction_string)
}

/// The string may be of the form \[num\]*\[.\[num\]*\]\[E\[+|-\]\[num\]...+\].
/// Or a rational, that is, a string of the form \[num\]* / \[num\]*
pub fn from_str(ctx: &'ctx Context, numeral: &str) -> Option<Real<'ctx>> {
let sort = Sort::real(ctx);
let ast = unsafe {
let fraction_cstring = CString::new(format!("{num:} / {den:}")).unwrap();
let numeral_ptr = Z3_mk_numeral(ctx.z3_ctx, fraction_cstring.as_ptr(), sort.z3_sort);
let c_numeral = CString::new(numeral).unwrap();
let numeral_ptr = Z3_mk_numeral(ctx.z3_ctx, c_numeral.as_ptr(), sort.z3_sort);
if numeral_ptr.is_null() {
return None;
}
Expand Down Expand Up @@ -930,6 +937,10 @@ impl<'ctx> Real<'ctx> {
}
}

pub fn as_f64(&self) -> f64 {
unsafe { Z3_get_numeral_double(self.ctx.z3_ctx, self.z3_ast) }
}

pub fn from_int(ast: &Int<'ctx>) -> Real<'ctx> {
unsafe { Self::wrap(ast.ctx, Z3_mk_int2real(ast.ctx.z3_ctx, ast.z3_ast)) }
}
Expand Down
Loading