Theory of Floating-Points

examples/api/cpp/floating_point_arith.cpp

  1/******************************************************************************
  2 * This file is part of the cvc5 project.
  3 *
  4 * Copyright (c) 2009-2026 by the authors listed in the file AUTHORS
  5 * in the top-level source directory and their institutional affiliations.
  6 * All rights reserved.  See the file COPYING in the top-level source
  7 * directory for licensing information.
  8 * ****************************************************************************
  9 *
 10 * An example of solving floating-point problems with cvc5's cpp API.
 11 *
 12 * This example shows to create floating-point types, variables and expressions,
 13 * and how to create rounding mode constants by solving toy problems. The
 14 * example also shows making special values (such as NaN and +oo) and converting
 15 * an IEEE 754-2008 bit-vector to a floating-point number.
 16 */
 17
 18#include <cvc5/cvc5.h>
 19
 20#include <iostream>
 21#include <cassert>
 22
 23using namespace cvc5;
 24
 25int main()
 26{
 27  TermManager tm;
 28  Solver solver(tm);
 29  solver.setOption("incremental", "true");
 30  solver.setOption("produce-models", "true");
 31
 32  // Make single precision floating-point variables
 33  Sort fpt32 = tm.mkFloatingPointSort(8, 24);
 34  Term a = tm.mkConst(fpt32, "a");
 35  Term b = tm.mkConst(fpt32, "b");
 36  Term c = tm.mkConst(fpt32, "c");
 37  Term d = tm.mkConst(fpt32, "d");
 38  Term e = tm.mkConst(fpt32, "e");
 39  // Rounding mode
 40  Term rm = tm.mkRoundingMode(RoundingMode::ROUND_NEAREST_TIES_TO_EVEN);
 41
 42  std::cout << "Show that fused multiplication and addition `(fp.fma RM a b c)`"
 43            << std::endl
 44            << "is different from `(fp.add RM (fp.mul a b) c)`:" << std::endl;
 45  solver.push(1);
 46  Term fma = tm.mkTerm(Kind::FLOATINGPOINT_FMA, {rm, a, b, c});
 47  Term mul = tm.mkTerm(Kind::FLOATINGPOINT_MULT, {rm, a, b});
 48  Term add = tm.mkTerm(Kind::FLOATINGPOINT_ADD, {rm, mul, c});
 49  solver.assertFormula(tm.mkTerm(Kind::DISTINCT, {fma, add}));
 50  Result r = solver.checkSat();  // result is sat
 51  std::cout << "Expect sat: " << r << std::endl;
 52  std::cout << "Value of `a`: " << solver.getValue(a) << std::endl;
 53  std::cout << "Value of `b`: " << solver.getValue(b) << std::endl;
 54  std::cout << "Value of `c`: " << solver.getValue(c) << std::endl;
 55  std::cout << "Value of `(fp.fma RNE a b c)`: " << solver.getValue(fma)
 56            << std::endl;
 57  std::cout << "Value of `(fp.add RNE (fp.mul a b) c)`: "
 58            << solver.getValue(add) << std::endl;
 59  std::cout << std::endl;
 60  solver.pop(1);
 61
 62  std::cout << "Show that floating-point addition is not associative:"
 63            << std::endl;
 64  std::cout << "(a + (b + c)) != ((a + b) + c)" << std::endl;
 65  solver.push(1);
 66  solver.assertFormula(tm.mkTerm(
 67      Kind::DISTINCT,
 68      {tm.mkTerm(Kind::FLOATINGPOINT_ADD,
 69                 {rm, a, tm.mkTerm(Kind::FLOATINGPOINT_ADD, {rm, b, c})}),
 70       tm.mkTerm(Kind::FLOATINGPOINT_ADD,
 71                 {rm, tm.mkTerm(Kind::FLOATINGPOINT_ADD, {rm, a, b}), c})}));
 72
 73  r = solver.checkSat();  // result is sat
 74  std::cout << "Expect sat: " << r << std::endl;
 75  assert (r.isSat());
 76
 77  std::cout << "Value of `a`: " << solver.getValue(a) << std::endl;
 78  std::cout << "Value of `b`: " << solver.getValue(b) << std::endl;
 79  std::cout << "Value of `c`: " << solver.getValue(c) << std::endl;
 80  std::cout << std::endl;
 81
 82  std::cout << "Now, restrict `a` to be either NaN or positive infinity:"
 83            << std::endl;
 84  Term nan = tm.mkFloatingPointNaN(8, 24);
 85  Term inf = tm.mkFloatingPointPosInf(8, 24);
 86  solver.assertFormula(tm.mkTerm(
 87      Kind::OR,
 88      {tm.mkTerm(Kind::EQUAL, {a, inf}), tm.mkTerm(Kind::EQUAL, {a, nan})}));
 89
 90  r = solver.checkSat();  // result is sat
 91  std::cout << "Expect sat: " << r << std::endl;
 92  assert (r.isSat());
 93
 94  std::cout << "Value of `a`: " << solver.getValue(a) << std::endl;
 95  std::cout << "Value of `b`: " << solver.getValue(b) << std::endl;
 96  std::cout << "Value of `c`: " << solver.getValue(c) << std::endl;
 97  std::cout << std::endl;
 98  solver.pop(1);
 99
100  std::cout << "Now, try to find a (normal) floating-point number that rounds"
101            << std::endl
102            << "to different integer values for different rounding modes:"
103            << std::endl;
104  solver.push(1);
105  Term rtp = tm.mkRoundingMode(RoundingMode::ROUND_TOWARD_POSITIVE);
106  Term rtn = tm.mkRoundingMode(RoundingMode::ROUND_TOWARD_NEGATIVE);
107  Op op = tm.mkOp(Kind::FLOATINGPOINT_TO_UBV, {16});  // (_ fp.to_ubv 16)
108  Term lhs = tm.mkTerm(op, {rtp, d});
109  Term rhs = tm.mkTerm(op, {rtn, d});
110  solver.assertFormula(tm.mkTerm(Kind::FLOATINGPOINT_IS_NORMAL, {d}));
111  solver.assertFormula(
112      tm.mkTerm(Kind::NOT, {tm.mkTerm(Kind::EQUAL, {lhs, rhs})}));
113
114  r = solver.checkSat();  // result is sat
115  std::cout << "Expect sat: " << r << std::endl;
116  assert (r.isSat());
117  std::cout << std::endl;
118
119  std::cout << "Get value of `d` as floating-point, bit-vector and real:"
120            << std::endl;
121  Term val = solver.getValue(d);
122  std::cout << "Value of `d`: " << val << std::endl;
123  std::cout << "Value of `((_ fp.to_ubv 16) RTP d)`: " << solver.getValue(lhs)
124            << std::endl;
125  std::cout << "Value of `((_ fp.to_ubv 16) RTN d)`: " << solver.getValue(rhs)
126            << std::endl;
127  std::cout << "Value of `(fp.to_real d)` "
128            << solver.getValue(tm.mkTerm(Kind::FLOATINGPOINT_TO_REAL, {val}))
129            << std::endl;
130  std::cout << std::endl;
131  solver.pop(1);
132
133  std::cout << "Finally, try to find a floating-point number between positive"
134            << std::endl
135            << "zero and the smallest positive floating-point number:"
136            << std::endl;
137  Term zero = tm.mkFloatingPointPosZero(8, 24);
138  Term smallest = tm.mkFloatingPoint(8, 24, tm.mkBitVector(32, 1));
139  solver.assertFormula(
140      tm.mkTerm(Kind::AND,
141                {tm.mkTerm(Kind::FLOATINGPOINT_LT, {zero, e}),
142                 tm.mkTerm(Kind::FLOATINGPOINT_LT, {e, smallest})}));
143
144  r = solver.checkSat();  // result is unsat
145  std::cout << "Expect unsat: " << r << std::endl;
146  assert(r.isUnsat());
147}