what optimizations are invalid in java?
Andrew Haley
aph@cambridge.redhat.com
Wed Mar 13 07:08:00 GMT 2002
Robert Dewar writes:
> <<Only divide by zero. Basically, Java does whatever SPARC UNIX
> happened to do at the time. This is not, I suspect, a coincidence...
> >>
>
> I think that's probably unfair.
I should have put a smiley. Here's one now, to compensate. :-)
> Division by zero is obviously reasonable to consider as an error,
> one cannot imagine any program expecting something useful from
> divide by zero (though I hesitate to say such things are the
> adamant support for the utility of self-initializations :-)
The reason I belive the division by zero rule to be rather arbitrary
is the behaviour when a divide overflow occurs:
"if the dividend is the negative integer of largest possible magnitude
for its type, and the divisor is -1, then integer overflow occurs and
the result is equal to the dividend. Despite the overflow, no
exception is thrown in this case. On the other hand, if the value of
the divisor in an integer division is 0, then an ArithmeticException
is thrown."
> But when it comes to artithmetic overflow, there are two issues to consider
>
> 1. efficiency, testing for overflow in all signed arithmetic operations is
> not free.
>
> 2. expected functionality. many C programmers expect and count on wrap around
> semantics for signed arithmetic even though it is not defined.
>
> In Java, signed arithmetic had to be made always wrap around, or always
> checking. Now I would have gone for always checked, but I understand the
> arguments for wrap around.
Indeed.
Andrew.
More information about the Java
mailing list