[m-rev.] for review: fix float_to_string so that roundtripping works

Peter Ross pro at missioncriticalit.com
Thu Nov 21 02:30:25 AEDT 2002


On Wed, Nov 20, 2002 at 03:04:22PM +0100, Peter Ross wrote:
> Index: library/string.m
> ===================================================================
> RCS file: /home/staff/zs/imp/mercury/library/string.m,v
> retrieving revision 1.184
> diff -u -r1.184 string.m
> --- library/string.m	15 Nov 2002 04:50:37 -0000	1.184
> +++ library/string.m	20 Nov 2002 13:46:46 -0000
> @@ -1824,76 +1824,70 @@
>  %-----------------------------------------------------------------------------%
>  
>  :- pragma foreign_proc("C",
> -	string__float_to_string(FloatVal::in, FloatString::uo),
> +	string__float_to_string(Flt::in, Str::uo),
>  		[will_not_call_mercury, promise_pure, thread_safe], "{
> -	char buf[500];
> -	sprintf(buf, ""%#.15g"", FloatVal);
> -	MR_allocate_aligned_string_msg(FloatString, strlen(buf), MR_PROC_LABEL);
> -	strcpy(FloatString, buf);
> +#ifdef MR_USE_SINGLE_PREC_FLOAT
> +	#define MIN_PRECISION	7
> +	const char *format = ""%f"";
> +#else
> +	#define MIN_PRECISION	15
> +	const char *format = ""%lf"";
> +#endif
> +	int i = MIN_PRECISION;
> +	MR_Float round;
> +
> +		/*
> +		 * Round-trip the float to ensure the precision was 
> +		 * sufficient, and if not then try with the next precision.
> +		*/
> +	Str = MR_make_string(MR_PROC_LABEL, ""%#.*g"", i, Flt);
> +	sscanf(Str, format, &round);
> +
> +	while (round != Flt) {

This test has been changed to (round != Flt && i < 2) to avoid an infinite
loop.

> +		i++;
> +		Str = MR_make_string(MR_PROC_LABEL, ""#%.*g"", i, Flt);
> +		sscanf(Str, format, &round);
> +	}
>  }").

--------------------------------------------------------------------------
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