[m-rev.] diff: fix a bug with io.write_float/4

Julien Fischer jfischer at opturion.com
Mon Dec 22 13:03:46 AEDT 2014


Fix a bug with io.write_float/4.

In the Java grade io.write_float/4 was incorrectly printing infinity as
"Infinity" and -infinity as "-Infinity".  (io.write_float/3 implemented
the correct behaviour.)

The recently added string_to_float_overflow test case also acts as
a test case for this.

library/io.m:
     Make io.write_float/4 print infinities correctly.

Julien.

diff --git a/library/io.m b/library/io.m
index 3e2cb48..3b51a64 100644
--- a/library/io.m
+++ b/library/io.m
@@ -8428,7 +8428,19 @@ io.flush_binary_output(binary_output_stream(Stream), !IO) :-
      io.write_float_2(Stream::in, Val::in, _IO0::di, _IO::uo),
      [may_call_mercury, promise_pure, tabled_for_io, thread_safe, terminates],
  "
-    ((io.MR_TextOutputFile) Stream).write_or_throw(String.valueOf(Val));
+    io.MR_TextOutputFile stream = (io.MR_TextOutputFile) Stream;
+
+    if (Double.isNaN(Val)) {
+        stream.write_or_throw(""nan"");
+    } else if (Double.isInfinite(Val)) {
+        if (Val < 0.0) {
+            stream.write_or_throw(""-infinity"");
+        } else {
+            stream.write_or_throw(""infinity"");
+        }
+    } else {
+        stream.write_or_throw(Double.toString(Val));
+    }
  ").

  :- pragma foreign_proc("Java",



More information about the reviews mailing list