Long Longs

KW Heuer kwh at bentley.UUCP
Wed Mar 5 03:38:15 AEST 1986


In article <1434 at brl-smoke.ARPA> jon at cit-vax.ARPA (Jon Leech) writes:
>Getting [ a C++ "class longlong" ] to generate the 'proper' code for
>	int b, c, d;
>	longlong result;
>	result = (b * c) / d;
>would be more difficult.

If you're willing to write it
	result = ((longlong)b * c) / d;
it will work, but it will invoke a more powerful function than it needs.

>Final cautionary note: [ from K&R ] "... Expressions ... may be
>rearranged arbitrarily ..."

That doesn't apply here; the only way b*c/d can be rearranged by the
compiler is to write it c*b/d (using commutativity of "*").  It can't
be written b*(c/d), for example, because that is not mathematically
correct (since x/y means floor(x/y)).  It could only do the division
first if it were a floating-point expression.

Besides, if you're talking about an overloaded C++ operator, none of
the mathematical identities are assumed: b*c/d means exactly
operator/(operator*(b,c),d); the compiler makes no assumptions
about the semantics.

Karl W. Z. Heuer (ihnp4!bentley!kwh), The Walking Lint



More information about the Comp.lang.c mailing list