Re: Division by zero
- From: Robert A Duff <bobduff@xxxxxxxxxxxxxxxxxxxx>
- Date: 12 Jun 2005 09:10:53 -0400
"Dmitry A. Kazakov" <mailbox@xxxxxxxxxxxxxxxxx> writes:
> No, it is not, because it has a defined effect: Constraint_Error. Formally
> speaking division by zero is not a contract violation and thus cannot be
> "wrong". Consider the following:
>
> A : constant Integer := 0;
> B : Integer := 1;
> begin
> B := B / A;
> Put_Line ("Hello!");
> exception
> when others =>
> Put_Line ("Good bye!");
>
> The above is a valid program, which should print "Good bye!". At the same
> time the following is *wrong*:
>
> A : constant Integer := 0;
> B : constant Integer := 1;
> C : Integer;
> begin
> C := B / A;
>
> B/A is a static expression, which has to have a value at compile-time.
>
> The difference is not in what the compiler might know, but in what it must
> know.
True, but the RM could easily define that differently. For example, in
Ada 83, "1/0" was *not* considered a run-time error -- it must raise C_E
at run time. That was changed in Ada 95.
We could just as well add a rule that "B/0" is illegal.
But the general idea is that we let compilers generate
warnings in such cases, and don't worry too much about it
in the RM.
By the way, I believe your above example is wrong.
In particular, it can print "Hello!".
See RM-11.6. (This is why I don't like 11.6 -- reasonable programmers
*think* the above should print "Good bye!", but 11.6 says
it might not.)
> > What has actually happened (outside that toy example)
> > was that there was a constant declared in one place and used in
> > many others. Someone went ahead and changed it (for a good
> > reason) to zero. However, one of the uses of that constant was
> > to divide by it.
> >
> >> Constraint_Error will be raised at runtime.
> >
> > It was. My question was - should it have been caught
> > at compile time instead?
>
> I don't think it should. A class of errors which cannot be detected at
> compile time cannot be compile-time errors. Or, in other words, if you
> cannot rely on the compiler, you should not pretend you can. (:-))
Well, in Ada 83, the "class of errors" was "divide by zero".
In Ada 95, that was split into "divide by zero in a static expression"
and "divide by zero in a nonstatic expression" -- the former is a
compile time error, whereas the latter is a run-time error.
There's nothing illogical about splitting it further:
"divide by zero when the Right operand is static" could be defined
as a compile-time error. The problem is that if you try to define
all the compile-time-detectable cases very precisely, it gets quite
complicated. And however you define it, it *has* to be conservative.
- Bob
.
- Follow-Ups:
- Re: Division by zero
- From: Jeffrey Carter
- Re: Division by zero
- References:
- Division by zero
- From: Lurker
- Re: Division by zero
- From: David C. Hoos, Sr.
- Re: Division by zero
- From: Lurker
- Re: Division by zero
- From: Dmitry A. Kazakov
- Division by zero
- Prev by Date: Re: Division by zero
- Next by Date: Re: Division by zero
- Previous by thread: Re: Division by zero
- Next by thread: Re: Division by zero
- Index(es):
Relevant Pages
|
Loading