[m-rev.] diff: fix io.write_float with special float values in C grades

Julien Fischer jfischer at opturion.com
Mon Jun 30 15:23:18 AEST 2014


Fix the printing float special values with write_float in C grades.

runtime/mercury_float.c:
 	When printing floats, do not append ".0" to nan, inf or -inf.

tests/hard_coded/Mmakefile:
tests/hard_coded/write_float_special.{m,exp,exp2}:
 	Add a regression test for the above.

NEWS:
 	Announce the above fix.

Julien.

diff --git a/NEWS b/NEWS
index 45de13b..2c998db 100644
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,8 @@ This is a bug-fix release.
    been fixed.  This bug only affected the non-C backends.  (Bug #342)
  * string.format now handles special float values (i.e. nan, inf,  and -inf)
    correctly with the non-C backends.
+* A bug that caused io.write_float/[34] to append ".0" to float special values
+  has been fixed.

  Changes to the Mercury compiler:

diff --git a/runtime/mercury_float.c b/runtime/mercury_float.c
index 6091ff8..19a86a5 100644
--- a/runtime/mercury_float.c
+++ b/runtime/mercury_float.c
@@ -90,6 +90,13 @@ MR_sprintf_float(char *buf, MR_Float f)
      } while (round_trip != f);

      /*
+    ** Do not append ".0" to nan or (-)inf.
+    */
+    if (MR_is_nan(f) || MR_is_inf(f)) {
+        return;
+    }
+
+    /*
      ** Append ".0" if there is no "e" or "." in the string.
      */
      while (1) {
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index a73aa7d..f04ac81 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -349,6 +349,7 @@ ORDINARY_PROGS=	\
  	value_enum \
  	words_separator \
  	write \
+	write_float_special \
  	write_reg1 \
  	write_reg2 \
  	write_xml \
diff --git a/tests/hard_coded/write_float_special.exp b/tests/hard_coded/write_float_special.exp
new file mode 100644
index 0000000..cb27352
--- /dev/null
+++ b/tests/hard_coded/write_float_special.exp
@@ -0,0 +1,3 @@
+Inf: inf
+-Inf: -inf
+NaN: nan
diff --git a/tests/hard_coded/write_float_special.exp2 b/tests/hard_coded/write_float_special.exp2
new file mode 100644
index 0000000..ad476d5
--- /dev/null
+++ b/tests/hard_coded/write_float_special.exp2
@@ -0,0 +1,3 @@
+Inf: infinity
+-Inf: -infinity
+NaN: nan
diff --git a/tests/hard_coded/write_float_special.m b/tests/hard_coded/write_float_special.m
new file mode 100644
index 0000000..f7d5b7b
--- /dev/null
+++ b/tests/hard_coded/write_float_special.m
@@ -0,0 +1,37 @@
+%------------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%------------------------------------------------------------------------------%
+%
+% Regression test for problems with io.write_float handling special float
+% values.  In Mercury 14.01 and before ".0" was appended to the string printed
+% for special float values, for example:
+%
+% io.write_float(nan, !IO) ==> "nan.0"
+%
+%------------------------------------------------------------------------------%
+
+:- module write_float_special.
+:- interface.
+
+:- import_module io.
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+:- import_module float.
+:- import_module list.
+:- import_module string.
+
+main(!IO) :-
+    Inf = float.max + float.max,
+    NegInf = -Inf,
+    NaN = Inf * 0.0,
+    io.write_string("Inf: ", !IO),
+    io.write_float(Inf, !IO),
+    io.nl(!IO),
+    io.write_string("-Inf: ", !IO),
+    io.write_float(NegInf, !IO),
+    io.nl(!IO),
+    io.write_string("NaN: ", !IO),
+    io.write_float(NaN, !IO),
+    io.nl(!IO).



More information about the reviews mailing list