Skip to content

Commit 0b6105f

Browse files
committed
Adds an ability to specify a floating point rounding mode.
Either using an option, or explicitly. Closes #63.
1 parent 24f074d commit 0b6105f

9 files changed

+363
-95
lines changed

src/org/sosy_lab/java_smt/SolverContextFactory.java

+23-7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.sosy_lab.common.configuration.Options;
3636
import org.sosy_lab.common.io.PathCounterTemplate;
3737
import org.sosy_lab.common.log.LogManager;
38+
import org.sosy_lab.java_smt.api.FloatingPointRoundingMode;
3839
import org.sosy_lab.java_smt.api.SolverContext;
3940
import org.sosy_lab.java_smt.logging.LoggingSolverContext;
4041
import org.sosy_lab.java_smt.solvers.mathsat5.Mathsat5SolverContext;
@@ -86,9 +87,13 @@ public enum Solvers {
8687
@Option(secure = true, description = "Which SMT solver to use.")
8788
private Solvers solver = Solvers.SMTINTERPOL;
8889

89-
@Option(secure = true, name = "useLogger", description = "Log solver actions, this may be slow!")
90+
@Option(secure = true, description = "Log solver actions, this may be slow!")
9091
private boolean useLogger = false;
9192

93+
@Option(secure = true, description = "Default rounding mode for floating point operations.")
94+
private FloatingPointRoundingMode floatingPointRoundingMode =
95+
FloatingPointRoundingMode.NEAREST_TIES_TO_EVEN;
96+
9297
private final LogManager logger;
9398
private final ShutdownNotifier shutdownNotifier;
9499
private final Configuration config;
@@ -147,17 +152,20 @@ private SolverContext generateContext0(Solvers solverToCreate)
147152
// Loading SmtInterpol is difficult as it requires its own class
148153
// loader for fiddling with Java_CUP versions.
149154
return getFactoryForSolver(createSmtInterpolClassLoader(logger), SMTINTERPOL_FACTORY_CLASS)
150-
.create(config, logger, shutdownNotifier, logfile, randomSeed);
155+
.create(
156+
config, logger, shutdownNotifier, logfile, randomSeed, floatingPointRoundingMode);
151157

152158
case MATHSAT5:
153-
return Mathsat5SolverContext.create(logger, config, shutdownNotifier, logfile, randomSeed);
159+
return Mathsat5SolverContext.create(
160+
logger, config, shutdownNotifier, logfile, randomSeed, floatingPointRoundingMode);
154161

155162
case Z3:
156163

157164
// Z3 requires its own custom class loader to perform trickery with the
158165
// java.library.path without affecting the main class loader.
159166
return getFactoryForSolver(z3ClassLoader, Z3_FACTORY_CLASS)
160-
.create(config, logger, shutdownNotifier, logfile, randomSeed);
167+
.create(
168+
config, logger, shutdownNotifier, logfile, randomSeed, floatingPointRoundingMode);
161169

162170
case PRINCESS:
163171
// TODO: pass randomSeed to Princess
@@ -225,13 +233,20 @@ public SolverContext create(
225233
LogManager logger,
226234
ShutdownNotifier pShutdownNotifier,
227235
@Nullable PathCounterTemplate solverLogfile,
228-
long randomSeed)
236+
long randomSeed,
237+
FloatingPointRoundingMode pFloatingPointRoundingMode)
229238
throws InvalidConfigurationException {
230239
final Thread currentThread = Thread.currentThread();
231240
final ClassLoader contextClassLoader = currentThread.getContextClassLoader();
232241
try {
233242
currentThread.setContextClassLoader(this.getClass().getClassLoader());
234-
return generateSolverContext(config, logger, pShutdownNotifier, solverLogfile, randomSeed);
243+
return generateSolverContext(
244+
config,
245+
logger,
246+
pShutdownNotifier,
247+
solverLogfile,
248+
randomSeed,
249+
pFloatingPointRoundingMode);
235250
} finally {
236251
currentThread.setContextClassLoader(contextClassLoader);
237252
}
@@ -242,7 +257,8 @@ protected abstract SolverContext generateSolverContext(
242257
LogManager logger,
243258
ShutdownNotifier pShutdownNotifier,
244259
@Nullable PathCounterTemplate solverLogfile,
245-
long randomSeed)
260+
long randomSeed,
261+
FloatingPointRoundingMode pFloatingPointRoundingMode)
246262
throws InvalidConfigurationException;
247263
}
248264

src/org/sosy_lab/java_smt/api/FloatingPointFormulaManager.java

+60-9
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,94 @@
2020
package org.sosy_lab.java_smt.api;
2121

2222
import org.sosy_lab.common.rationals.Rational;
23+
import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType;
2324

2425
import java.math.BigDecimal;
2526

27+
/**
28+
* Floating point operations.
29+
*
30+
* Most operations are overloaded: there is an option of either using the default
31+
* rounding mode (set via the option {@code solver.floatingPointRoundingMode}),
32+
* or providing the rounding mode explicitly.
33+
*/
2634
public interface FloatingPointFormulaManager {
27-
FloatingPointFormula makeNumber(double n, FormulaType.FloatingPointType type);
2835

29-
FloatingPointFormula makeNumber(BigDecimal n, FormulaType.FloatingPointType type);
36+
FloatingPointFormula makeNumber(double n, FloatingPointType type);
37+
38+
FloatingPointFormula makeNumber(
39+
double n, FloatingPointType type, FloatingPointRoundingMode pFloatingPointRoundingMode);
40+
41+
FloatingPointFormula makeNumber(BigDecimal n, FloatingPointType type);
42+
43+
FloatingPointFormula makeNumber(
44+
BigDecimal n, FloatingPointType type, FloatingPointRoundingMode pFloatingPointRoundingMode);
45+
46+
FloatingPointFormula makeNumber(String n, FloatingPointType type);
47+
48+
FloatingPointFormula makeNumber(
49+
String n, FloatingPointType type, FloatingPointRoundingMode pFloatingPointRoundingMode);
3050

31-
FloatingPointFormula makeNumber(String n, FormulaType.FloatingPointType type);
51+
FloatingPointFormula makeNumber(Rational n, FloatingPointType type);
3252

33-
FloatingPointFormula makeNumber(Rational n, FormulaType.FloatingPointType type);
53+
FloatingPointFormula makeNumber(
54+
Rational n, FloatingPointType type, FloatingPointRoundingMode pFloatingPointRoundingMode);
3455

35-
FloatingPointFormula makeVariable(String pVar, FormulaType.FloatingPointType type);
56+
FloatingPointFormula makeVariable(String pVar, FloatingPointType type);
3657

37-
FloatingPointFormula makePlusInfinity(FormulaType.FloatingPointType type);
58+
FloatingPointFormula makePlusInfinity(FloatingPointType type);
3859

39-
FloatingPointFormula makeMinusInfinity(FormulaType.FloatingPointType type);
60+
FloatingPointFormula makeMinusInfinity(FloatingPointType type);
4061

41-
FloatingPointFormula makeNaN(FormulaType.FloatingPointType type);
62+
FloatingPointFormula makeNaN(FloatingPointType type);
4263

4364
<T extends Formula> T castTo(FloatingPointFormula number, FormulaType<T> targetType);
4465

66+
<T extends Formula> T castTo(
67+
FloatingPointFormula number,
68+
FormulaType<T> targetType,
69+
FloatingPointRoundingMode pFloatingPointRoundingMode);
70+
71+
FloatingPointFormula castFrom(Formula number, boolean signed, FloatingPointType targetType);
72+
4573
FloatingPointFormula castFrom(
46-
Formula number, boolean signed, FormulaType.FloatingPointType targetType);
74+
Formula number,
75+
boolean signed,
76+
FloatingPointType targetType,
77+
FloatingPointRoundingMode pFloatingPointRoundingMode);
4778

4879
// ----------------- Arithmetic relations, return type NumeralFormula -----------------
4980

5081
FloatingPointFormula negate(FloatingPointFormula number);
5182

5283
FloatingPointFormula add(FloatingPointFormula number1, FloatingPointFormula number2);
5384

85+
FloatingPointFormula add(
86+
FloatingPointFormula number1,
87+
FloatingPointFormula number2,
88+
FloatingPointRoundingMode pFloatingPointRoundingMode);
89+
5490
FloatingPointFormula subtract(FloatingPointFormula number1, FloatingPointFormula number2);
5591

92+
FloatingPointFormula subtract(
93+
FloatingPointFormula number1,
94+
FloatingPointFormula number2,
95+
FloatingPointRoundingMode pFloatingPointRoundingMode);
96+
5697
FloatingPointFormula divide(FloatingPointFormula number1, FloatingPointFormula number2);
5798

99+
FloatingPointFormula divide(
100+
FloatingPointFormula number1,
101+
FloatingPointFormula number2,
102+
FloatingPointRoundingMode pFloatingPointRoundingMode);
103+
58104
FloatingPointFormula multiply(FloatingPointFormula number1, FloatingPointFormula number2);
59105

106+
FloatingPointFormula multiply(
107+
FloatingPointFormula number1,
108+
FloatingPointFormula number2,
109+
FloatingPointRoundingMode pFloatingPointRoundingMode);
110+
60111
// ----------------- Numeric relations, return type BooleanFormula -----------------
61112

62113
/**

0 commit comments

Comments
 (0)