[m-rev.] diff: fix pringint of special float values in C# grade

Julien Fischer jfischer at opturion.com
Mon Jun 30 17:46:18 AEST 2014


Fix printing of special float values in C# grade.

library/string.m:
 	In the C# grade, do not append ".0" to a special float
 	value when converting it to a string.

tests/hard_coded/write_special_float.exp3:
 	Alternative expected output for this for use with the
 	Java and C# grades.

NEWS:
 	Announce the fix.

Julien.

diff --git a/NEWS b/NEWS
index 2c998db..ed2121e 100644
--- a/NEWS
+++ b/NEWS
@@ -24,7 +24,7 @@ This is a bug-fix release.
  * 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.
+  has been fixed.  This bug affected the C and C# backends.

  Changes to the Mercury compiler:

diff --git a/library/string.m b/library/string.m
index b91b3f8..2688991 100644
--- a/library/string.m
+++ b/library/string.m
@@ -4198,16 +4198,19 @@ string.from_float(Flt) = string.float_to_string(Flt).
  "
      Str = Flt.ToString(""R"");

-    /* Append '.0' if there is no 'e' or '.' in the string. */
-    bool contains = false;
-    foreach (char c in Str) {
-        if (c == 'e' || c == 'E' || c == '.') {
-            contains = true;
-            break;
+    if (!System.Double.IsNaN(Flt) && !System.Double.IsInfinity(Flt)) {
+
+        /* Append '.0' if there is no 'e' or '.' in the string. */
+        bool contains = false;
+        foreach (char c in Str) {
+            if (c == 'e' || c == 'E' || c == '.') {
+                contains = true;
+                break;
+            }
+        }
+        if (!contains) {
+            Str = Str + "".0"";
          }
-    }
-    if (!contains) {
-        Str = Str + "".0"";
      }
  ").

diff --git a/tests/hard_coded/write_float_special.exp3 b/tests/hard_coded/write_float_special.exp3
new file mode 100644
index 0000000..207df85
--- /dev/null
+++ b/tests/hard_coded/write_float_special.exp3
@@ -0,0 +1,3 @@
+Inf: Infinity
+-Inf: -Infinity
+NaN: NaN



More information about the reviews mailing list