Skip to content

Commit 7fed483

Browse files
committed
#433: add more documentation and tests for FP numbers.
1 parent 1e9dacc commit 7fed483

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package org.sosy_lab.java_smt.api;
1010

11+
import static org.sosy_lab.java_smt.api.FormulaType.getFloatingPointType;
12+
1113
import java.math.BigDecimal;
1214
import java.math.BigInteger;
1315
import org.sosy_lab.common.rationals.Rational;
@@ -59,12 +61,18 @@ FloatingPointFormula makeNumber(
5961

6062
/**
6163
* Creates a floating point formula representing the given string value with the specified type.
64+
*
65+
* <p>The string can be any valid floating-point number, e.g., "1.0", "1.0e-3", but also special
66+
* values like "NaN", "Infinity", or "-0.0", etc. A leading "+" sign or "-" sign is allowed.
6267
*/
6368
FloatingPointFormula makeNumber(String n, FloatingPointType type);
6469

6570
/**
6671
* Creates a floating point formula representing the given string value with the specified type
6772
* and rounding mode.
73+
*
74+
* <p>The string can be any valid floating-point number, e.g., "1.0", "1.0e-3", but also special
75+
* values like "NaN", "Infinity", or "-0.0", etc. A leading "+" sign or "-" sign is allowed.
6876
*/
6977
FloatingPointFormula makeNumber(
7078
String n, FloatingPointType type, FloatingPointRoundingMode pFloatingPointRoundingMode);
@@ -91,7 +99,7 @@ default FloatingPointFormula makeNumber(FloatingPointNumber number) {
9199
number.getExponent(),
92100
number.getMantissa(),
93101
number.getSign(),
94-
FloatingPointType.getFloatingPointType(number.getExponentSize(), number.getMantissaSize()));
102+
getFloatingPointType(number.getExponentSize(), number.getMantissaSize()));
95103
}
96104

97105
/**

src/org/sosy_lab/java_smt/basicimpl/AbstractFloatingPointFormulaManager.java

+6
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ protected static boolean isNegativeZero(Double pN) {
149149
return Double.valueOf("-0.0").equals(pN);
150150
}
151151

152+
/**
153+
* Parses the provided string and converts it into a floating-point formula.
154+
*
155+
* <p>The input string must represent a valid finite floating-point number. Values such as NaN,
156+
* Infinity, or -Infinity are not allowed and should be handled before calling this method.
157+
*/
152158
protected abstract TFormulaInfo makeNumberAndRound(
153159
String pN, FloatingPointType pType, TFormulaInfo pFloatingPointRoundingMode);
154160

src/org/sosy_lab/java_smt/test/FloatingPointFormulaManagerTest.java

+62-3
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,58 @@ public void negative() throws SolverException, InterruptedException {
105105

106106
@Test
107107
public void parser() throws SolverException, InterruptedException {
108-
for (String s : new String[] {"-1", "-Infinity", "-0", "-0.0", "-0.000"}) {
108+
for (String s :
109+
new String[] {
110+
"-1",
111+
"-Infinity",
112+
"-0",
113+
"-0.0",
114+
"-0.000",
115+
"-0e5",
116+
"-0.00e-5",
117+
"-12e34",
118+
"-12e-34",
119+
"-12.34E56",
120+
"-12.34e-56"
121+
}) {
109122
FloatingPointFormula formula = fpmgr.makeNumber(s, singlePrecType);
110123
assertThatFormula(fpmgr.isNegative(formula)).isTautological();
111124
assertThatFormula(fpmgr.isNegative(fpmgr.negate(formula))).isUnsatisfiable();
112125
assertEqualsAsFp(fpmgr.negate(formula), fpmgr.abs(formula));
113126
}
114-
for (String s : new String[] {"1", "Infinity", "0", "0.0", "0.000"}) {
127+
for (String s :
128+
new String[] {
129+
"1",
130+
"Infinity",
131+
"0",
132+
"0.0",
133+
"0.000",
134+
"0e5",
135+
"0.00e-5",
136+
"12e34",
137+
"12e-34",
138+
"12.34E56",
139+
"12.34e-56"
140+
}) {
115141
FloatingPointFormula formula = fpmgr.makeNumber(s, singlePrecType);
116142
assertThatFormula(fpmgr.isNegative(formula)).isUnsatisfiable();
117143
assertThatFormula(fpmgr.isNegative(fpmgr.negate(formula))).isTautological();
118144
assertEqualsAsFp(formula, fpmgr.abs(formula));
119145
}
120-
for (String s : new String[] {"+1", "+Infinity", "+0", "+0.0", "+0.000"}) {
146+
for (String s :
147+
new String[] {
148+
"+1",
149+
"+Infinity",
150+
"+0",
151+
"+0.0",
152+
"+0.000",
153+
"+0e5",
154+
"+0.00e-5",
155+
"+12e34",
156+
"+12e-34",
157+
"+12.34E56",
158+
"+12.34e-56"
159+
}) {
121160
FloatingPointFormula formula = fpmgr.makeNumber(s, singlePrecType);
122161
assertThatFormula(fpmgr.isNegative(formula)).isUnsatisfiable();
123162
assertThatFormula(fpmgr.isNegative(fpmgr.negate(formula))).isTautological();
@@ -791,6 +830,26 @@ private <T> void proveForAll(List<T> args, Function<T, BooleanFormula> f)
791830
}
792831
}
793832

833+
@Test
834+
public void checkString2FpConversion32() throws SolverException, InterruptedException {
835+
proveForAll(
836+
getListOfFloats(),
837+
pFloat ->
838+
fpmgr.equalWithFPSemantics(
839+
fpmgr.makeNumber(pFloat, singlePrecType),
840+
fpmgr.makeNumber(Float.toString(pFloat), singlePrecType)));
841+
}
842+
843+
@Test
844+
public void checkString2FpConversion64() throws SolverException, InterruptedException {
845+
proveForAll(
846+
getListOfDoubles(),
847+
pDouble ->
848+
fpmgr.equalWithFPSemantics(
849+
fpmgr.makeNumber(pDouble, doublePrecType),
850+
fpmgr.makeNumber(Double.toString(pDouble), doublePrecType)));
851+
}
852+
794853
@Test
795854
public void checkIeeeBv2FpConversion32() throws SolverException, InterruptedException {
796855
proveForAll(

0 commit comments

Comments
 (0)