[m-rev.] for review: mercury implementation of float_to_string

Fergus Henderson fjh at cs.mu.OZ.AU
Sat Nov 23 04:27:11 AEDT 2002


On 22-Nov-2002, Peter Ross <pro at missioncriticalit.com> wrote:
> On Sat, Nov 23, 2002 at 02:44:33AM +1100, Simon Taylor wrote:
> > You should document the difference between lowlevel_float_to_string
> > and float_to_string, explaining why both are needed.
> 
> Estimated hours taken: 2
> Branches: main
> 
> Provide a mercury implementation of string__float_to_string.  This is
> needed so that string__float_to_string returns the same output on all
> backends.
> 
> library/string.m:
> 	Implementation of string__float_to_string in Mercury by using
> 	string__format.
> 
> inter diff:
> 
> diff -u library/string.m library/string.m
> --- library/string.m
> +++ library/string.m
> @@ -96,6 +96,9 @@
>  :- pred string__float_to_string(float, string).
>  :- 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.

This comment doesn't match the code, since the code uses 15 digits to
represent "1.0", which doesn't require any more than 1 digit.

>  :- pred string__first_char(string, char, string).
>  :- mode string__first_char(in, in, in) is semidet.	% implied
> @@ -1866,6 +1869,12 @@
>  :- func max_precision = int.
>  max_precision = 17.
>  
> +% 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__format uses to get the initial string representation of a float.

s/in a foreign language/without calling string__format,/

or perhaps s/in a foreign language/without calling string__format (e.g.
by invoking some foreign language routine to do the conversion),/

> +% 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".

s/formated/formatted/
In what ways is it allowed to differ?

> +string__float_to_string_2(Prec, Float) = String :-
> +	string__format("%#." ++ int_to_string(Prec) ++ "g", [f(Float)], Tmp),
> +	( Prec = max_precision ->
> +		String = Tmp
> +	;
> +		( string__to_float(Tmp, Float) ->
> +			String = Tmp
> +		;
> +			String = float_to_string_2(Prec + 1, Float)

For consistency, s/float_to_string_2/string__float_to_string_2/

> +	% We assume that on non-C backends that we are using double
> +	% precision floats.
> +:- func min_precision = int.
> +min_precision = 15.
> +
> +:- func max_precision = int.
> +max_precision = 17.

I think this assumption could be avoided by writing that as

	min_precision = floor_to_int(mantissa_digits * log2(base_radix) /
		log2(10)).

	max_precision = min_precision + 2.

However, the result would be less efficient.
Maybe we should leave the code as you wrote it, but put the
more general definition above in a comment.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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