[erlang-questions] Floor Function?

Richard O'Keefe <>
Tue Mar 17 00:15:28 CET 2009


I wrote:
> There doesn't seem to be any reason to compute X-T.
>
On 17 Mar 2009, at 12:44 am, Hynek Vychodil wrote:
> Are you sure that there is big difference between compute X-T and  
> decide X<T or which one is less expensive? I'm not! If you haven't  
> measured ...
>
This misses the point.
The existing version requires 40 tokens,
the simplified version requires 26.
Never mind the computer, it's a heck of a lot easier for PEOPLE
to think and to read.  Again, the existing version does one
subtraction and three comparisons, while the simplified version
does one comparison.  Never mind the computer, it's a heck of a
lot easier for PEOPLE to think and to read.

In general, there is a *correctness* issue in using
X - Y when you really mean X <relop> Y.  Just last week
I had occasion to point out to someone that using
	static int intcmp(void const *x, void const *y) {
	    return *(int const *)x - *(int const *)y;
	}
as a comparison function in C's qsort() is *guaranteed* to
give wrong answers.  Thanks to bignums -- I don't know who
decided that Erlang would have bignums but it was a really
excellent decision -- Erlang doesn't have that particular
problem, but there is a similar problem involving floats.
That doesn't apply in this case either, but it _is_ enough
of a problem that we do NOT want people to get into the
habit of "comparing" numbers by subtracting them.  It's FAR
too error-prone.

There are other reasons (simplicity, error-proneness) to
prefer one approach to another than just machine efficiency.




More information about the erlang-questions mailing list