[m-rev.] for review: fix bug in MR_sprintf_float
Ian MacLarty
maclarty at csse.unimelb.edu.au
Fri Jul 16 17:42:38 AEST 2010
On Fri, Jul 16, 2010 at 04:31:09PM +1000, Julien Fischer wrote:
>
> On Fri, 16 Jul 2010, Ian MacLarty wrote:
>
>> Still bootchecking this, but it passes float_roundtrip and
>> test_pretty_print.
>>
>> runtime/mercury_float.c:
>> Fix a bug in MR_sprintf_float where it would convert 1.8e-10
>> into "1.80000000000000e-1" instead of "1.8e-10".
>>
>> The problem was that it was stripping the zeros off the end of the string
>> produced by sprintf without considering the case where the output
>> was in scientific notation.
>>
>> The fix is to remove the `#' from the sprintf format string and then to
>> append ".0" to the string if there is no "." or "e".
>>
>> tests/general/float_roundtrip.m:
>> tests/general/float_roundtrip.exp:
>> Add regression test.
>
> That looks fine.
>
The expected output of some tests needed to be updated. Here's the full
diff and log message (committed):
runtime/mercury_float.c:
Fix a bug in MR_sprintf_float where it would convert 1.8e-10
into "1.80000000000000e-1" instead of "1.8e-10".
The problem was that it was stripping the zeros off the end of the string
produced by sprintf without considering the case where the output
was in scientific notation.
The fix is to remove the `#' from the sprintf format string and then to
append ".0" to the string if there is no "." or "e".
tests/general/float_roundtrip.m:
tests/general/float_roundtrip.exp:
Add regression test.
tests/hard_coded/deep_copy.exp:
tests/hard_coded/expand.exp:
tests/hard_coded/float_reg.exp:
tests/hard_coded/write.exp:
tests/hard_coded/write_binary.exp:
tests/hard_coded/write_reg1.exp:
tests/hard_coded/write_xml.exp:
Update the expected output of these tests as trailing zeros are now
also removed from the base part of scientific notation float strings.
Index: runtime/mercury_float.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_float.c,v
retrieving revision 1.10
diff -u -r1.10 mercury_float.c
--- runtime/mercury_float.c 15 Feb 2007 00:41:49 -0000 1.10
+++ runtime/mercury_float.c 16 Jul 2010 07:14:54 -0000
@@ -73,7 +73,7 @@
** is round-trippable.
*/
do {
- sprintf(buf, "%#.*g", i, f);
+ sprintf(buf, "%.*g", i, f);
if (i >= MR_FLT_MAX_PRECISION) {
/*
** This should be sufficient precision to round-trip any value.
@@ -87,30 +87,18 @@
} while (round_trip != f);
/*
- ** Strip redundant trailing zeroes from the string (this behaviour
- ** for %g is suppressed by the # modifier).
+ ** Append ".0" if there is no "e" or "." in the string.
*/
- for (n = strlen(buf) - 1; n > 0; n--) {
- switch (buf[n]) {
- case '.':
- buf[n + 2] = '\0';
- return;
- case '0':
- continue;
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- buf[n + 1] = '\0';
- return;
- default:
- return;
+ while (1) {
+ if (*buf == 'e' || *buf == '.') {
+ return;
}
+ if (*buf == '\0') {
+ /* We only get here if there is no '.' or 'e' in the string. */
+ strcpy(buf, ".0");
+ return;
+ }
+ buf++;
}
return;
Index: tests/general/float_roundtrip.exp
===================================================================
RCS file: /home/mercury1/repository/tests/general/float_roundtrip.exp,v
retrieving revision 1.1
diff -u -r1.1 float_roundtrip.exp
--- tests/general/float_roundtrip.exp 21 Nov 2002 08:00:58 -0000 1.1
+++ tests/general/float_roundtrip.exp 16 Jul 2010 07:14:55 -0000
@@ -2,3 +2,4 @@
0.123573124 : success.
0.987654321012345 : success.
0.12345678901234566 : success.
+1.8e-10 : success.
Index: tests/general/float_roundtrip.m
===================================================================
RCS file: /home/mercury1/repository/tests/general/float_roundtrip.m,v
retrieving revision 1.1
diff -u -r1.1 float_roundtrip.m
--- tests/general/float_roundtrip.m 21 Nov 2002 08:00:58 -0000 1.1
+++ tests/general/float_roundtrip.m 16 Jul 2010 07:14:56 -0000
@@ -14,7 +14,8 @@
test_float(7, 0.9092974),
test_float(9, 0.123573124),
test_float(15, 0.987654321012345),
- test_float(17, 0.12345678901234566).
+ test_float(17, 0.12345678901234566),
+ test_float_roundtrippable(1.8e-10).
:- pred test_float(int::in, float::in, io::di, io::uo) is det.
@@ -35,6 +36,16 @@
io__write_string(" digits of precision.\n")
).
+:- pred test_float_roundtrippable(float::in, io::di, io::uo) is det.
+
+test_float_roundtrippable(Flt, !IO) :-
+ ( roundtrip_float(Flt) ->
+ io.format("%-20s: ", [s(string.float_to_string(Flt))], !IO),
+ io.write_string("success.\n", !IO)
+ ;
+ io.format("failed to roundtrip %f\n", [f(Flt)], !IO)
+ ).
+
% Test that when we round-trip the float that we get the same float
% back.
:- pred roundtrip_float(float::in) is semidet.
Index: tests/hard_coded/deep_copy.exp
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/deep_copy.exp,v
retrieving revision 1.5
diff -u -r1.5 deep_copy.exp
--- tests/hard_coded/deep_copy.exp 15 Feb 2007 00:41:51 -0000 1.5
+++ tests/hard_coded/deep_copy.exp 16 Jul 2010 07:14:56 -0000
@@ -75,12 +75,12 @@
3.14159
3.14159
3.14159
-1.12832498300000e-21
-1.12832498300000e-21
-1.12832498300000e-21
-2.23954899000000e+23
-2.23954899000000e+23
-2.23954899000000e+23
+1.128324983e-21
+1.128324983e-21
+1.128324983e-21
+2.23954899e+23
+2.23954899e+23
+2.23954899e+23
-65
-65
-65
Index: tests/hard_coded/expand.exp
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/expand.exp,v
retrieving revision 1.7
diff -u -r1.7 expand.exp
--- tests/hard_coded/expand.exp 15 Feb 2007 00:41:52 -0000 1.7
+++ tests/hard_coded/expand.exp 16 Jul 2010 07:14:56 -0000
@@ -125,16 +125,16 @@
expand: functor 3.14159 arity 0 arguments []
deconstruct_du failed
-1.12832498300000e-21/0
+1.128324983e-21/0
functor_number_cc failed
no arguments
-expand: functor 1.12832498300000e-21 arity 0 arguments []
+expand: functor 1.128324983e-21 arity 0 arguments []
deconstruct_du failed
-2.23954899000000e+23/0
+2.23954899e+23/0
functor_number_cc failed
no arguments
-expand: functor 2.23954899000000e+23 arity 0 arguments []
+expand: functor 2.23954899e+23 arity 0 arguments []
deconstruct_du failed
-65/0
Index: tests/hard_coded/float_reg.exp
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/float_reg.exp,v
retrieving revision 1.3
diff -u -r1.3 float_reg.exp
--- tests/hard_coded/float_reg.exp 15 Feb 2007 00:41:52 -0000 1.3
+++ tests/hard_coded/float_reg.exp 16 Jul 2010 07:14:56 -0000
@@ -1,3 +1,3 @@
-2.88000000000000e+32
-1.00000000000000e+32
+2.88e+32
+1e+32
10000000000.0
Index: tests/hard_coded/write.exp
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/write.exp,v
retrieving revision 1.11
diff -u -r1.11 write.exp
--- tests/hard_coded/write.exp 15 Feb 2007 00:41:53 -0000 1.11
+++ tests/hard_coded/write.exp 16 Jul 2010 07:14:56 -0000
@@ -31,8 +31,8 @@
'a'
'&'
3.14159
-1.12832498300000e-21
-2.23954899000000e+23
+1.128324983e-21
+2.23954899e+23
-65
4
univ_cons(["hi! I\'m a univ!"])
Index: tests/hard_coded/write_binary.exp
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/write_binary.exp,v
retrieving revision 1.2
diff -u -r1.2 write_binary.exp
--- tests/hard_coded/write_binary.exp 15 Feb 2007 00:41:53 -0000 1.2
+++ tests/hard_coded/write_binary.exp 16 Jul 2010 07:14:56 -0000
@@ -115,11 +115,11 @@
3.14159
3.14159
ok... test passed:
-1.12832498300000e-21
-1.12832498300000e-21
+1.128324983e-21
+1.128324983e-21
ok... test passed:
-2.23954899000000e+23
-2.23954899000000e+23
+2.23954899e+23
+2.23954899e+23
ok... test passed:
-65
-65
Index: tests/hard_coded/write_reg1.exp
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/write_reg1.exp,v
retrieving revision 1.8
diff -u -r1.8 write_reg1.exp
--- tests/hard_coded/write_reg1.exp 15 Feb 2007 00:41:54 -0000 1.8
+++ tests/hard_coded/write_reg1.exp 16 Jul 2010 07:14:56 -0000
@@ -23,8 +23,8 @@
'a'
'&'
3.14159
-1.12832498300000e-21
-2.23954899000000e+23
+1.128324983e-21
+2.23954899e+23
-65
4
univ_cons(["hi! I\'m a univ!"])
Index: tests/hard_coded/write_xml.exp
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/write_xml.exp,v
retrieving revision 1.5
diff -u -r1.5 write_xml.exp
--- tests/hard_coded/write_xml.exp 15 Feb 2007 00:41:54 -0000 1.5
+++ tests/hard_coded/write_xml.exp 16 Jul 2010 07:14:56 -0000
@@ -25,7 +25,7 @@
<Int type="int">123456</Int>
<Tuple--2--Tag_-123character-44-32float-125 functor="{}" type="{character, float}" arity="2">
<Char type="character">a</Char>
- <Float type="float">1.23553225220000e-97</Float>
+ <Float type="float">1.2355322522e-97</Float>
</Tuple--2--Tag_-123character-44-32float-125>
</Tuple--3--Tag_-123string-44-32int-44-32-123character-44-32float-125-125>
</a_tuple--1--write_xml-46mytype>
@@ -119,7 +119,7 @@
<String type="string" field="field1">a string</String>
<Int type="int" field="Field<2>">1</Int>
<Char type="character">c</Char>
- <Float type="float" field="another field">-1.00000000000000e-15</Float>
+ <Float type="float" field="another field">-1e-15</Float>
<yes--0--bool-46bool functor="yes" type="bool.bool" arity="0" />
</hello--5--write_xml-46mytype>
</pred-40int-41>
@@ -414,7 +414,7 @@
<Int type="int">123456</Int>
<Tuple functor="{}" type="{character, float}" arity="2">
<Char type="character">a</Char>
- <Float type="float">1.23553225220000e-97</Float>
+ <Float type="float">1.2355322522e-97</Float>
</Tuple>
</Tuple>
</a_tuple>
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to: mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions: mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------
More information about the reviews
mailing list