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

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Nov 25 11:36:00 AEDT 2002


On 22-Nov-2002, Peter Ross <pro at missioncriticalit.com> wrote:
> On Fri, Nov 22, 2002 at 10:29:14PM +1100, Fergus Henderson wrote:
> +++ library/float.m
> @@ -448,20 +448,12 @@
>  :- pragma promise_pure(is_nan/1).
>  :- pragma foreign_proc(c, is_nan(Flt::in),
>  		[will_not_call_mercury, thread_safe], "
> -#if MR_USE_SINGLE_PREC_FLOAT
> -  #ifdef MR_HAVE_ISNANF
> +#if defined(MR_USE_SINGLE_PREC_FLOAT) && defined(MR_HAVE_ISNANF)
>  	SUCCESS_INDICATOR = isnanf(Flt);
> -  #else
> -    #error ""isnanf not available. Delete this line if not IEEE fp system.""
> -	SUCCESS_INDICATOR = MR_FALSE;
> -  #endif
> +#elif defined(MR_HAVE_ISNAN)
> +	SUCCESS_INDICATOR = isnan((double) Flt);

There's no need for an explicit cast there.
Standard C guarantees that `float' will get promoted to double,
even if there is no prototype for isnan() in scope.

Explicit casts can suppress real errors,
e.g. if `Flt' happened to have type `double *',
so it's better to avoid them if they are not needed.

> @@ -478,20 +470,12 @@
>  :- pragma promise_pure(is_inf/1).
>  :- pragma foreign_proc(c, is_inf(Flt::in),
>  		[will_not_call_mercury, thread_safe], "
> -#if MR_USE_SINGLE_PREC_FLOAT
> -  #ifdef MR_HAVE_ISINFF
> +#if defined(MR_USE_SINGLE_PREC_FLOAT) && defined(MR_HAVE_ISINFF)
>  	SUCCESS_INDICATOR = isinff(Flt);
> -  #else
> -    #error ""isinff not available. Delete this line if not IEEE fp system.""
> -	SUCCESS_INDICATOR = MR_FALSE;
> -  #endif
> +#elif defined(MR_HAVE_ISINF)
> +	SUCCESS_INDICATOR = isinf((double) Flt);

Likewise here.

>  #else
> -  #ifdef MR_HAVE_ISINF
> -	SUCCESS_INDICATOR = isinf(Flt);
> -  #else
> -    #error ""isinf not available. Delete this line if not IEEE fp system.""
> -	SUCCESS_INDICATOR = MR_FALSE;
> -  #endif
> +	SUCCESS_INDICATOR = (Flt == Flt * 2.0 && Flt != 0.0);
>  #endif
>  ").

On second thoughts, it's probably better to use

	SUCCESS_INDICATOR = (Flt == Flt / 2.0 && Flt != 0.0);
					^

since the multiplication might overflow, and implementations are more like
to trap on overflow than to trap on underflow.

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