summaryrefslogtreecommitdiff
path: root/examples/api/java/QuickStart.java
diff options
context:
space:
mode:
authorAndrew Reynolds <andrew.j.reynolds@gmail.com>2021-12-14 13:35:53 -0600
committerGitHub <noreply@github.com>2021-12-14 13:35:53 -0600
commite16ab44a2b4622bb5745633cbafd43a0023a518c (patch)
treed980bdc3dc771abfc8101036d1e2aaebc8020134 /examples/api/java/QuickStart.java
parentad34df900d79aad64558b354a866870715bfd007 (diff)
parenteffb0d47ba5bfaebae17dcd06153489dccd90eff (diff)
Merge branch 'master' into cav22-stringscav22-strings
Diffstat (limited to 'examples/api/java/QuickStart.java')
-rw-r--r--examples/api/java/QuickStart.java31
1 files changed, 26 insertions, 5 deletions
diff --git a/examples/api/java/QuickStart.java b/examples/api/java/QuickStart.java
index a79263cbf..c815278cc 100644
--- a/examples/api/java/QuickStart.java
+++ b/examples/api/java/QuickStart.java
@@ -106,11 +106,32 @@ public class QuickStart
Term xMinusY = solver.mkTerm(Kind.MINUS, x, y);
Term xMinusYVal = solver.getValue(xMinusY);
- // Further, we can convert the values to java types,
- Pair<BigInteger, BigInteger> xRational = xVal.getRealValue();
- Pair<BigInteger, BigInteger> yRational = yVal.getRealValue();
- System.out.println("value for x: " + xRational.first + "/" + xRational.second);
- System.out.println("value for y: " + yRational.first + "/" + yRational.second);
+ // Further, we can convert the values to java types
+ Pair<BigInteger, BigInteger> xPair = xVal.getRealValue();
+ Pair<BigInteger, BigInteger> yPair = yVal.getRealValue();
+ Pair<BigInteger, BigInteger> xMinusYPair = xMinusYVal.getRealValue();
+
+ System.out.println("value for x: " + xPair.first + "/" + xPair.second);
+ System.out.println("value for y: " + yPair.first + "/" + yPair.second);
+ System.out.println("value for x - y: " + xMinusYPair.first + "/" + xMinusYPair.second);
+
+ // Another way to independently compute the value of x - y would be
+ // to perform the (rational) arithmetic manually.
+ // However, for more complex terms,
+ // it is easier to let the solver do the evaluation.
+ Pair<BigInteger, BigInteger> xMinusYComputed =
+ new Pair(xPair.first.multiply(yPair.second).subtract(xPair.second.multiply(yPair.first)),
+ xPair.second.multiply(yPair.second));
+ BigInteger g = xMinusYComputed.first.gcd(xMinusYComputed.second);
+ xMinusYComputed = new Pair(xMinusYComputed.first.divide(g), xMinusYComputed.second.divide(g));
+ if (xMinusYComputed.equals(xMinusYPair))
+ {
+ System.out.println("computed correctly");
+ }
+ else
+ {
+ System.out.println("computed incorrectly");
+ }
// Next, we will check satisfiability of the same formula,
// only this time over integer variables a and b.
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback