Skip to content

Commit 8febc81

Browse files
author
Vojtěch Forejt
authored
Merge pull request #4700 from forejtv/forejtv/float-zero
Float 0 to string implementation
2 parents 10fa912 + 0599403 commit 8febc81

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed
Binary file not shown.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class Test {
2+
3+
static void checkStringInputValue(float f) {
4+
String s = Float.toString(f);
5+
// the 1/f is needed to distinguish -0 from 0, '==' disregards the
6+
// difference, while the division yields +Infinity or -Infinity.
7+
if (f == 0.0f && 1/f > 0) {
8+
assert(s.equals("0.0"));
9+
} else if (f == -0.0f && 1/f < 0) {
10+
assert(s.equals("-0.0"));
11+
} else if (f == 1.0f ) {
12+
assert(s.equals("1.0"));
13+
}
14+
}
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
Test.class
3+
--function Test.checkStringInputValue --cp `../../../../scripts/format_classpath.sh . ../../../lib/java-models-library/target/core-models.jar`
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
assertion.1.*SUCCESS
7+
assertion.2.*SUCCESS
8+
assertion.3.*SUCCESS

jbmc/src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,22 @@ code_blockt java_string_library_preprocesst::make_float_to_string_code(
950950
string_literal_to_string_expr("-Infinity", loc, symbol_table, code);
951951
string_expr_list.push_back(minus_infinity);
952952

953+
// Case of 0.0
954+
// Note: for zeros we must use equal_exprt and not ieee_float_equal_exprt,
955+
// the latter disregards the sign
956+
condition_list.push_back(equal_exprt(arg, zero));
957+
const refined_string_exprt zero_string =
958+
string_literal_to_string_expr("0.0", loc, symbol_table, code);
959+
string_expr_list.push_back(zero_string);
960+
961+
// Case of -0.0
962+
ieee_floatt minus_zero_float(float_spec);
963+
minus_zero_float.from_float(-0.0f);
964+
condition_list.push_back(equal_exprt(arg, minus_zero_float.to_expr()));
965+
const refined_string_exprt minus_zero_string =
966+
string_literal_to_string_expr("-0.0", loc, symbol_table, code);
967+
string_expr_list.push_back(minus_zero_string);
968+
953969
// Case of simple notation
954970
ieee_floatt bound_inf_float(float_spec);
955971
ieee_floatt bound_sup_float(float_spec);

0 commit comments

Comments
 (0)