From: Robert A Duff <bobduff@shell01.TheWorld.com>
Subject: Re: Division by zero
Date: 13 Jun 2005 08:19:59 -0400
Date: 2005-06-13T08:19:59-04:00 [thread overview]
Message-ID: <wccfyvm78k0.fsf@shell01.TheWorld.com> (raw)
In-Reply-To: JNZqe.3031$NX4.2109@newsread1.news.pas.earthlink.net
Jeffrey Carter <spam@spam.com> writes:
> Robert A Duff wrote:
> > 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.
>
> The problem, it seems to me, is that the developer may deliberately
> write a division of a variable by static zero for a number of
> reasons. He may want it to raise Constraint_Error at run time,...
It's hard to come up with legitimate examples of that.
As others have pointed out, "raise C_E" is usually the
best way to raise C_E.
Here's an example that agrees with your point:
Suppose I have a constant Widgets that is different on different
operating systems. So I create two versions of the same package,
and the Windows version says "Widgets: constant Integer := 23;"
and the Unix version says "Widgets: constant Integer := 0;".
I have some configuration scripts to pick the right version
of the source code to compile. (This is a common way of dealing
with OS dependencies.)
Now I have a variable Gizmos, and I want to print out the
numbers of Gizmos per Widget (when that's meaningful).
In *portable* code, I might want to write:
if Widgets = 0 then
Put_Line("Unknown");
else
Put_Line(Image(Gizmos/Widgets));
end if;
If divide by zero were illegal at compile time, even in unreachable
code, the above would be annoying illegal on Unix. So I would have to
move the above if statement into the OS-specific files, thus increasing
their size -- a Bad Thing. Or, I would have to make Widgets nonstatic,
which might be damaging for other reasons.
(In fact, if Gizmos is static, then the above *is* illegal in Ada 95,
but not Ada 83.)
Can anybody think of a better example (i.e. one that does not involve
extra-lingual features such as OS-specific source code versions)?
>... or may
> have redefined "/" for the type of the variable to do something
> meaningful if the divisor is zero.
The rules about static expressions do not apply to user-defined
operators, which can do anything you like.
> This latter case invalidates an earlier example. The following should
> compile and output Integer'Last before terminating normally:
>
> with Ada.Text_IO;
> procedure Zero_Div is
> function "/" (Left : Integer; Right : Integer) return Integer is
> -- null;
> begin -- "/"
> if Right = 0 then
> return Integer'Last;
> else
> return Left / Right;
Careful! That looks like an infinite recursion.
You need to do some horsing around to get at the predefined "/",
so you can call it from the user-defined one.
> end if;
> end "/";
>
> A : constant Integer := 23;
> B : constant Integer := 0;
> begin -- Zero_Div
> Ada.Text_IO.Put_Line (Item => Integer'Image (A / B) );
> end Zero_Div;
- Bob
next prev parent reply other threads:[~2005-06-13 12:19 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-06-11 21:03 Division by zero Lurker
2005-06-12 2:00 ` David C. Hoos, Sr.
2005-06-12 3:04 ` Lurker
2005-06-12 8:39 ` Dmitry A. Kazakov
2005-06-12 9:43 ` Lurker
2005-06-12 10:36 ` Marius Amado Alves
2005-06-12 11:53 ` Dmitry A. Kazakov
2005-06-13 8:03 ` Ole-Hjalmar Kristensen
2005-06-12 13:10 ` Robert A Duff
2005-06-12 16:55 ` Jeffrey Carter
2005-06-13 3:22 ` Keith Thompson
2005-06-14 2:14 ` Jeffrey Carter
2005-06-13 8:47 ` Lurker
2005-06-14 2:19 ` Jeffrey Carter
2005-06-14 8:35 ` Keith Thompson
2005-06-13 12:19 ` Robert A Duff [this message]
2005-06-14 2:31 ` Jeffrey Carter
2005-06-14 8:21 ` Lurker
2005-06-14 20:22 ` Randy Brukardt
2005-06-28 21:22 ` Robert A Duff
2005-06-29 5:50 ` Lurker
2005-06-29 13:27 ` Robert A Duff
2005-06-29 13:54 ` Dmitry A. Kazakov
2005-06-29 16:03 ` Robert A Duff
2005-06-30 1:19 ` Lurker
2005-06-30 11:16 ` Stuart Palin
2005-06-29 13:50 ` Dmitry A. Kazakov
2005-06-29 16:07 ` Robert A Duff
2005-06-30 8:27 ` Dmitry A. Kazakov
2005-06-29 9:20 ` Lurker
2005-06-29 9:49 ` Christoph Grein
2005-06-29 10:40 ` Lurker
2005-06-29 11:04 ` Jeff Creem
2005-06-29 12:28 ` Martin Dowie
2005-06-29 13:40 ` Robert A Duff
[not found] ` <5sana1pm436l6vboifijqblu0irf84afkr@4ax.com>
2005-06-12 17:38 ` Simon Wright
2005-06-12 12:21 ` Robert A Duff
2005-06-12 22:53 ` Georg Bauhaus
2005-06-13 8:34 ` Lurker
2005-06-13 8:54 ` Marius Amado Alves
2005-06-13 17:59 ` Simon Wright
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox