[m-dev.] Re: pragma fact_table

Fergus Henderson fjh at cs.mu.oz.au
Thu Feb 27 15:01:29 AEDT 1997


David Matthew OVERTON, you wrote:
> 
> I've left the non-negative stuff in for now.  I didn't see much point
> in changing it while there is still disagreement about whether
> negative hash values should be allowed.

OK, we need to get together and resolve that issue.

>  %---------------------------------------------------------------------------%
> +% Compute a non-negative integer hash value for a float.
> +% Note: the hash value computed is different to that computed in the 
> +% Prolog code in float.nu.nl.

The user doesn't know what `float.nu.nl' is.
You should explain things in a way that a user can understand, e.g.

% (Note: the hash values computed by the Mercury implementation of
% this predicate are different to those computed by the Prolog
% implementation.)

> +++ float.nu.nl	1997/02/27 02:05:50
> @@ -49,4 +49,19 @@
>  
>  float__epsilon(2.2204460492503131e-16).
>  
> +	% Note:  This hash function is different to the one defined in
> +	% float.m.
> +float__hash(Float, Hash) :-
> +	float__abs(Float, Abs),
> +	( Abs = 0.0 -> Abs2 = 1e-15 ; Abs2 = Abs ),
> +	math__ln(Abs2, Log),
> +	math__ceiling(Log, TruncLog),
> +	math__exp(TruncLog, Pow),
> +	Float2 is Abs2 / Pow * 2147483647.0,   % 2147483647 = (2^31)-1
> +	float__truncate_to_int(Float2, N1),
> +	float__abs(TruncLog, Log2),
> +	float__truncate_to_int(Log2, N2),
> +	Hash0 is N1 ^ N2,
> +	int__abs(Hash0, Hash).

That code won't work.  (You did test it, didn't you?  Oh.)

Have a look at the comments at the top of library/math.m.

> +++ mercury_float.h	1997/02/26 06:56:02
> @@ -72,4 +72,8 @@
>  
>  #endif /* not BOXED_FLOAT */
>  
> +/* hash_float() is defined in mercury_float.c */
> +
> +Integer hash_float(Float);

The comment isn't necessary.
By default, anything declared in `foo.h' should be defined in `foo.c'.
It's only worth documenting where things are defined if you break that
rule.

> ===================================================================
> new file: runtime/mercury_float.c
> ===================================================================
> 
> /*
> ** Copyright (C) 1996-1997 University of Melbourne.
> ** This file may only be copied under the terms of the GNU Library General
> ** Public License - see the file COPYING.LIB in the Mercury distribution.
> */

s/1996-/

> #else /* BOXED_FLOAT -- sizeof(Float) > sizeof(Integer) */

!BOXED_FLOAT implies sizeof(Float) <= sizeof(Integer),
but BOXED_FLOAT does not imply sizeof(Float) > sizeof(Integer).

> union FloatInteger {
> 	Float f;
> 	Integer i[sizeof(Float)/sizeof(Integer)];
> };
> 
> Integer
> hash_float(Float f)
> {
> 	union FloatInteger fi;
> 	size_t i;
> 	Integer h = 0;
> 
> 	fi.f = f;
> 
> 	for (i = 0; i < sizeof(Float)/sizeof(Integer); i++)
> 		h ^= fi.i[i];

This code won't work if sizeof(Float) is not an even multiple of
sizeof(Integer).

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list