trying to simplify an algorithm

Bennett Todd bet at rahul.net
Tue Mar 1 05:52:47 PST 2005


> > More to the point though I think technically its broken. Im
> > fairly sure C only defines 0 to be false. *all* other values are
> > true, therefore you are relying on the compiler to use 1.

This comment bugs me every time it comes by.

Yes, when interpreting values in as booleans, C defines zero ==
false, nonzero == true.

But relational operators that _create_ booleans --- ==, !=, <, >, et
al --- create 0 or 1, strictly.

So without commenting on the overall correctness or desireability
of this construction, I think the above claim is not correct for
standard C. The use of relops seems correct to me.

And decomposing the expression, it really looks strictly correct to
me:

	( a+b+c == 180 ) * (a>0) * (b>0) * (c>0) *
	( 1 + (a==b) + (a==c) + (b==c) );

The whole thing is a product of 5 terms. The four on the first line
are all relops, 0 or 1, if any of 'em are zero the whole expression
is zero, and they constitute sanity-checking the inputs. It's a
triangle if all those conditions are true, i.e. all == 1.

The final term, on the second line, is 1, 2, or 4, as no angles
are equal, one pair of angles are equal, or all angles are equal,
respectively.

Type-punning booleans with arithmentic integers may be naughty in
higher-level languages that have distinct boolean types, but in C
relops produce integers, with deterministic values.

-Bennett
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.linuxfromscratch.org/pipermail/lfs-chat/attachments/20050301/66db098f/attachment.sig>


More information about the lfs-chat mailing list