mirror of
				https://github.com/Z3Prover/z3
				synced 2025-11-04 05:19:11 +00:00 
			
		
		
		
	FPA API: bugfixes and examples for .NET and Java
Signed-off-by: Christoph M. Wintersteiger <cwinter@microsoft.com>
This commit is contained in:
		
							parent
							
								
									fa26e2423e
								
							
						
					
					
						commit
						0faf329054
					
				
					 5 changed files with 165 additions and 48 deletions
				
			
		| 
						 | 
				
			
			@ -1095,7 +1095,7 @@ class JavaExample
 | 
			
		|||
 | 
			
		||||
    // / Shows how to use Solver(logic)
 | 
			
		||||
 | 
			
		||||
    // / <param name="ctx"></param>
 | 
			
		||||
    // / @param ctx 
 | 
			
		||||
    void logicExample(Context ctx) throws Z3Exception, TestFailedException
 | 
			
		||||
    {
 | 
			
		||||
        System.out.println("LogicTest");
 | 
			
		||||
| 
						 | 
				
			
			@ -2157,6 +2157,86 @@ class JavaExample
 | 
			
		|||
        // System.out.println(ctx.mkEq(s1, t1));
 | 
			
		||||
    }    
 | 
			
		||||
 | 
			
		||||
    public void floatingPointExample1(Context ctx) throws Z3Exception, TestFailedException
 | 
			
		||||
    {
 | 
			
		||||
        System.out.println("FloatingPointExample1");
 | 
			
		||||
        Log.append("FloatingPointExample1");
 | 
			
		||||
        
 | 
			
		||||
        FPSort s = ctx.mkFPSort(11, 53);
 | 
			
		||||
        System.out.println("Sort: " + s);
 | 
			
		||||
 | 
			
		||||
        FPNum x = (FPNum)ctx.mkNumeral("-1e1", s); /* -1 * 10^1 = -10 */
 | 
			
		||||
        FPNum y = (FPNum)ctx.mkNumeral("-10", s); /* -10 */
 | 
			
		||||
        FPNum z = (FPNum)ctx.mkNumeral("-1.25p3", s); /* -1.25 * 2^3 = -1.25 * 8 = -10 */
 | 
			
		||||
        System.out.println("x=" + x.toString()  + 
 | 
			
		||||
        				   "; y=" + y.toString() + 
 | 
			
		||||
        				   "; z=" + z.toString());
 | 
			
		||||
        
 | 
			
		||||
        BoolExpr a = ctx.mkAnd(ctx.mkFPEq(x, y), ctx.mkFPEq(y, z));
 | 
			
		||||
        check(ctx, ctx.mkNot(a), Status.UNSATISFIABLE);
 | 
			
		||||
 | 
			
		||||
        /* nothing is equal to NaN according to floating-point 
 | 
			
		||||
         * equality, so NaN == k should be unsatisfiable. */
 | 
			
		||||
        FPExpr k = (FPExpr)ctx.mkConst("x", s);
 | 
			
		||||
        FPExpr nan = ctx.mkFPNaN(s);
 | 
			
		||||
 | 
			
		||||
        /* solver that runs the default tactic for QF_FP. */
 | 
			
		||||
        Solver slvr = ctx.mkSolver("QF_FP");
 | 
			
		||||
        slvr.add(ctx.mkFPEq(nan, k));
 | 
			
		||||
        if (slvr.check() != Status.UNSATISFIABLE)
 | 
			
		||||
            throw new TestFailedException();
 | 
			
		||||
        System.out.println("OK, unsat:" + System.getProperty("line.separator") + slvr);
 | 
			
		||||
 | 
			
		||||
        /* NaN is equal to NaN according to normal equality. */
 | 
			
		||||
        slvr = ctx.mkSolver("QF_FP");
 | 
			
		||||
        slvr.add(ctx.mkEq(nan, nan));
 | 
			
		||||
        if (slvr.check() != Status.SATISFIABLE)
 | 
			
		||||
            throw new TestFailedException();
 | 
			
		||||
        System.out.println("OK, sat:" + System.getProperty("line.separator")  + slvr);
 | 
			
		||||
 | 
			
		||||
        /* Let's prove -1e1 * -1.25e3 == +100 */
 | 
			
		||||
        x = (FPNum)ctx.mkNumeral("-1e1", s);
 | 
			
		||||
        y = (FPNum)ctx.mkNumeral("-1.25p3", s);
 | 
			
		||||
        FPExpr x_plus_y = (FPExpr)ctx.mkConst("x_plus_y", s);
 | 
			
		||||
        FPNum r = (FPNum)ctx.mkNumeral("100", s);
 | 
			
		||||
        slvr = ctx.mkSolver("QF_FP");
 | 
			
		||||
 | 
			
		||||
        slvr.add(ctx.mkEq(x_plus_y, ctx.mkFPMul(ctx.mkFPRoundNearestTiesToAway(), x, y)));
 | 
			
		||||
        slvr.add(ctx.mkNot(ctx.mkFPEq(x_plus_y, r)));
 | 
			
		||||
        if (slvr.check() != Status.UNSATISFIABLE)
 | 
			
		||||
            throw new TestFailedException();
 | 
			
		||||
        System.out.println("OK, unsat:" + System.getProperty("line.separator")  + slvr);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void floatingPointExample2(Context ctx) throws Z3Exception, TestFailedException
 | 
			
		||||
    {
 | 
			
		||||
        System.out.println("FloatingPointExample2");
 | 
			
		||||
        Log.append("FloatingPointExample2");
 | 
			
		||||
        
 | 
			
		||||
        FPSort double_sort = ctx.mkFPSort(11, 53);
 | 
			
		||||
        FPRMSort rm_sort = ctx.mkFPRoundingModeSort();
 | 
			
		||||
        
 | 
			
		||||
        FPRMExpr rm = (FPRMExpr)ctx.mkConst(ctx.mkSymbol("rm"), rm_sort);
 | 
			
		||||
        BitVecExpr x = (BitVecExpr)ctx.mkConst(ctx.mkSymbol("x"), ctx.mkBitVecSort(64));
 | 
			
		||||
        RealExpr real_val = ctx.mkReal(42);
 | 
			
		||||
        FPExpr fp_val = ctx.mkFP(42, double_sort);
 | 
			
		||||
 | 
			
		||||
        BoolExpr c1 = ctx.mkEq(x, ctx.mkFPToIEEEBV(fp_val));
 | 
			
		||||
        BoolExpr c2 = ctx.mkEq(x, ctx.mkBV(42, 64));
 | 
			
		||||
        BoolExpr c3 = ctx.mkEq(fp_val, ctx.mkFPToFP(rm, real_val, double_sort));
 | 
			
		||||
        BoolExpr c4 = ctx.mkAnd(c1, c2);
 | 
			
		||||
        System.out.println("c3 = " + c3);
 | 
			
		||||
 | 
			
		||||
        /* Generic solver */
 | 
			
		||||
        Solver s = ctx.mkSolver();
 | 
			
		||||
        s.add(c3);
 | 
			
		||||
 | 
			
		||||
        if (s.check() != Status.SATISFIABLE)
 | 
			
		||||
            throw new TestFailedException();
 | 
			
		||||
 | 
			
		||||
        System.out.println("OK, model: " + s.getModel().toString());
 | 
			
		||||
    }
 | 
			
		||||
    
 | 
			
		||||
    public static void main(String[] args)
 | 
			
		||||
    {
 | 
			
		||||
        JavaExample p = new JavaExample();
 | 
			
		||||
| 
						 | 
				
			
			@ -2200,6 +2280,8 @@ class JavaExample
 | 
			
		|||
                p.findSmallModelExample(ctx);
 | 
			
		||||
                p.simplifierExample(ctx);
 | 
			
		||||
                p.finiteDomainExample(ctx);
 | 
			
		||||
                p.floatingPointExample1(ctx);
 | 
			
		||||
                p.floatingPointExample2(ctx);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            { // These examples need proof generation turned on.
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue