[m-rev.] diff: fix bug #342 -- string.format broken in non-C grades

Julien Fischer jfischer at opturion.com
Wed Jun 25 16:35:13 AEST 2014


Branches: 14.01, master

Fix bug #342.

The Mercury implementation of string formatting (used by the non-C grades) was
incorrectly formatting some numbers when using the 'g' conversion specifier,
for example:

     string.format("%6.3g\n", [f(16.0)]) = "10"

The cause was an off-by-one error introduced when calls to string.substring
were changed to calls string.between (in commit
b1af59cb29ac31db9fc5b3422fced505facabad1).

library/string.m:
 	Fix the off-by-one error above.

NEWS:
 	Announce the fix.

Julien.

diff --git a/NEWS b/NEWS
index c5aacac..706bb42 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,9 @@ This is a bug-fix release.
  * We have added workarounds for problems with (arguably broken)
    system headers on MinGW and MinGW64 systems.
  * The MinGW port now builds in the absence of POSIX threads library.
+* A problem that caused string.format/[23] to sometimes return incorrect
+  results when formatting floats with the 'g' conversion specifier has
+  been fixed.  This bug only affected the non-C backends.  (Bug #342)

  Changes to the Mercury compiler:

diff --git a/library/string.m b/library/string.m
index 0a27063..36d39fe 100644
--- a/library/string.m
+++ b/library/string.m
@@ -4034,7 +4034,7 @@ calculate_base_unsafe(Float, Prec) = Exp :-
              DecimalPos + Prec + 1)
      ; Place > 0 ->
          ExpMantissaStr = string.between(MantissaStr, 0, 1),
-        FirstHalfOfFractionStr = string.between(MantissaStr, 1, Place),
+        FirstHalfOfFractionStr = string.between(MantissaStr, 1, Place + 1),
          ExpFractionStr = FirstHalfOfFractionStr ++ FractionStr
      ;
          ExpMantissaStr = MantissaStr,



More information about the reviews mailing list