[m-rev.] for review: mercury implementation of float_to_string
Peter Ross
pro at missioncriticalit.com
Mon Nov 25 23:14:37 AEDT 2002
Estimated hours taken: 0.5
Branches: main
library/string.m:
Address the review comments of fjh about my changes to
introduce string__lowlevel_float_to_string.
Index: string.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/string.m,v
retrieving revision 1.187
diff -u -r1.187 string.m
--- string.m 22 Nov 2002 16:42:47 -0000 1.187
+++ string.m 25 Nov 2002 12:12:51 -0000
@@ -97,8 +97,11 @@
:- mode string__float_to_string(in, uo) is det.
% Convert an float to a string.
% The resulting float will be in the form that it was printed using
-% the format string "%#.<prec>g" where <prec> is the required precision
-% needed to represent the string.
+% the format string "%#.<prec>g" where <prec> is in the range p to (p+2)
+% where p = floor(mantissa_digits * log2(base_radix) / log2(10)) and
+% the precision is sufficient to allow a succesful decimal -> binary
+% conversion of the float.
+
:- pred string__first_char(string, char, string).
:- mode string__first_char(in, in, in) is semidet. % implied
@@ -1857,24 +1860,31 @@
( string__to_float(Tmp, Float) ->
String = Tmp
;
- String = float_to_string_2(Prec + 1, Float)
+ String = string__float_to_string_2(Prec + 1, Float)
)
).
- % We assume that on non-C backends that we are using double
- % precision floats.
+ % XXX For efficency reasons we assume that on non-C backends that we
+ % are using double precision floats, however the commented out code
+ % provides a general mechanisme for calculating the required precision.
:- func min_precision = int.
min_precision = 15.
+% min_precision =
+ floor_to_int(float(mantissa_digits) * log2(float(radix)) / log2(10.0)).
:- func max_precision = int.
max_precision = 17.
+% max_precision = min_precision + 2.
-% string__lowlevel_float_to_string differs from string__float_to_string in
-% that it must be implemented in a foreign language as this is the predicate
+% string__lowlevel_float_to_string differs from string__float_to_string in that
+% it must be implemented without calling string__format (e.g. by invoking some
+% foreign language routine to do the conversion) as this is the predicate
% string__format uses to get the initial string representation of a float.
% Also while its output must represent the float to sufficient precision, it
-% doesn't need to be in *exactly* the same format as if was formated with
-% "%#.<prec>g".
+% doesn't need to be in *exactly* the same format as if was formatted with
+% "%#.<prec>g". For example, it may return any of "1", "1.0", "0.1e1", "1E1",
+% "1.000000000000000" or some other representation to represent the floating
+% point number 1.0.
:- pred string__lowlevel_float_to_string(float, string).
:- mode string__lowlevel_float_to_string(in, uo) is det.
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list