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

Peter Moulder pmoulder at csse.monash.edu.au
Fri Nov 22 09:50:26 AEDT 2002


On Thu, Nov 21, 2002 at 04:46:36PM +0100, Peter Ross wrote:

> +	% returns a float which is round-tripable, ie to full precision needed.

I believe that should be trippable (double-p), as in shippable
(egrep '[aeiou]pp?able$' /usr/share/dict/words).
Also I've a mild preference for "i.e. to the full ...".

This string occurs in two places in the diff.

> -:- pred is_denormal(string :: in) is semidet.
> -is_denormal("NaN").
> -is_denormal("Inf").
> +:- pred is_special_value(float::in) is semidet.
> +
> +is_special_value(Float) :-
> +	( is_nan(Float)
> +	; is_inf(Float)
> +	).

is_inf_or_nan is both shorter and clearer than is_special_value.

One might even add this (or its negation, as in C99) to float.m.  C99 has an
isfinite macro (returning non-zero iff its argument is neither NaN nor
infinite).  Not to be confused with BSD's `finite' function, which 
is like !isinf(x).

> +	call bool [mscorlib]System.Double::IsInfinity(float64)

Does this return true for -Inf too?

Some good test cases are that
is_inf(Inf), is_inf(-Inf), not is_inf(NaN), not is_inf(0.0),
is_nan(NaN), not is_nan(Inf), not is_nan(-Inf), not is_nan(0.0).


Slightly off-topic:

The documentation for SUCCESS_INDICATOR says that `a truth value' should
be assigned, but doesn't specify what values are acceptable as `true'.
I'd guess that the correct answer is `any MR_Integer value other than 0'.

(If the acceptable values are more restricted than this, then the calls to isnan
and isinf should explicitly test `!= 0'.)

Can someone please update this documentation accordingly?  Note that
SUCCESS_INDICATOR is documented in two different nodes (for foreign_proc and
c_code).

> +get_hex_int(Int) = HexStr :-
> +	( Int < integer(10) ->
> +		HexStr = integer__to_string(Int)
> +	; Int = integer(10) ->
> +		HexStr = "a"
> +	; Int = integer(11) ->
> [...]

If writing in C, the obvious code would be something like

  (val < 10
   ? '0' + val
   : 'a' - 10 + val)

(after converting the integer Int to an int).  I've written some untested
suggested replacement code below; though you may well decide that it
isn't worth bothering over.

	Short = integer__int(Int),
	( Short < 10 ->
		char__to_int('0', BaseVal)
	;
		char__to_int('a', AVal),
		BaseVal is AVal - 10
	),
	( char__to_int(HexChar0, BaseVal + Short) ->
		HexChar = HexChar0
	;
		error("bug: char__to_int(out, in) failed.")
	),
	HexStr = string__char_to_string(HexChar).

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