From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,e01fe1b326df26d4 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!newscon02.news.prodigy.com!newscon06.news.prodigy.com!prodigy.net!newsfeed-00.mathworks.com!nntp.TheWorld.com!not-for-mail From: Robert A Duff Newsgroups: comp.lang.ada Subject: Re: Division by zero Date: 12 Jun 2005 09:10:53 -0400 Organization: The World Public Access UNIX, Brookline, MA Message-ID: References: <79ahr0jozmqb$.10jzllrcjpmsv.dlg@40tude.net> NNTP-Posting-Host: shell01.theworld.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: pcls4.std.com 1118581853 28084 192.74.137.71 (12 Jun 2005 13:10:53 GMT) X-Complaints-To: abuse@TheWorld.com NNTP-Posting-Date: Sun, 12 Jun 2005 13:10:53 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: g2news1.google.com comp.lang.ada:11299 Date: 2005-06-12T09:10:53-04:00 List-Id: "Dmitry A. Kazakov" 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