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

Conversation

lucascool12
Copy link
Contributor

Adds the ability to create Reals from any numeral using Z3_mk_numeral.
Also adds the method as_f64 to Real which uses Z3_get_numberal_double.

This is useful if you want to represent a real as a f64. Currently you would have to calculate the numerator and denominator in order to create a z3 real from a f64. With this merge request all you would need to do is create a string from your f64 and pass this to from_str.

Copy link
Contributor

@waywardmonkeys waywardmonkeys left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can make the suggested change and help get this to pass the relevant parts of CI, that'd be great.

z3/src/ast.rs Outdated
Self::from_str(ctx, &fraction_string)
}

/// The string may be of the form \[num\]*\[.\[num\]*\]\[E\[+|-\]\[num\]+\].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put ... around the \[num\]...+\] ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@lucascool12 lucascool12 force-pushed the feat-str-decimal-reals branch from 1d38944 to 7d2698a Compare June 3, 2024 12:30
@lucascool12 lucascool12 reopened this Jul 17, 2024
@lucascool12
Copy link
Contributor Author

I fixed the relevant parts of the CI, and then accidentally clicked close pull request, whoops.

@dragazo
Copy link
Contributor

dragazo commented Jul 25, 2024

Z3_get_numeral_double fails (by returning zero) if the result cannot be stored perfectly in a 64-bit floating-point value. For example, the irrational number sqrt(2) would have this issue. If that's the intended behavior in this PR, we might want to call it something like as_exact_f64 or exact_f64. As a user, I'd also want it to return Option<f64> instead of f64 with a default 0.0 for failure - although I don't know if Z3 exposes a way to check if that operation succeeds, at least not directly. But we could always check an integer or rational conversion to resolve the 0.0 ambiguous case after the fact.

But if that's not the intended behavior, I have a separate PR that works even for irrational numbers: #304, in which case we could drop the as_f64 from this PR and both PRs can be merged eventually.

@lucascool12
Copy link
Contributor Author

I didn't know Z3_get_numeral_double could fail. #304 is pretty much what I need for my use case.
Except that it is missing the ability to create a Real from a numeral string, I'll put this in a different PR.
Still we could keep this PR alive for exact f64 as you said.
The documentation specifies that the precondition of Z3_get_numeral_double is Z3_get_ast_kind(c, a) == Z3_NUMERAL_AST || Z3_is_algebraic_number(c, a) , I tried this but got unexpected results.
The method @dragazo described does seem to work.
I'll change to title of the PR.

@lucascool12
Copy link
Contributor Author

I'll change to title of the PR.
Scratch that I'll close this one and create 2 new ones.

@lucascool12 lucascool12 closed this Aug 2, 2024
@lucascool12 lucascool12 mentioned this pull request Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants