From: Stephen Leake <stephen_leake@stephe-leake.org>
Subject: Re: Float conversion
Date: Sat, 31 Jul 2010 11:12:34 -0400
Date: 2010-07-31T11:12:34-04:00 [thread overview]
Message-ID: <82y6cru1lp.fsf@stephe-leake.org> (raw)
In-Reply-To: d981aeab-67e9-4fde-a96c-ec6a2a403703@c10g2000yqi.googlegroups.com
Phil Clayton <phil.clayton@lineone.net> writes:
> if A < B and A < C
> then
> Y := A;
> elsif B < C and B < A
> then
> Y := B;
> else
> Y := C;
> end if;
>
> The justification given is
>
> if A is smallest, set Y to A
> else if B is smallest, set Y to B
> else C is smallest so set Y to C
>
> Unfortunately, the program doesn't work. If you haven't spotted why,
> it is well worth trying to work it out, perhaps with a few test cases.
>
> In fact, this particular error came to various people's attention
> because it made its way though all stages of a safety-critical
> software development process. (Fortunately the consequences were not
> too serious, though intriguing.) The program fails exactly when A = B
> < C because it returns C, which is not the minimum.
I am _always_ suspicious of 'and' conditions in nested if/then/else; it
is easy to leave out a case. If this had been written:
if A < B then
if A < C then
Y := A;
else
-- A >= C
...
The problem would have been clear from the start.
> I often bring this example up to motivate the use of formal methods as
> it is particularly difficult to find the error through testing,
> especially when A, B and C are real types. What are the chances of
> having A equal to B?
100%, for a rationally designed test! Clearly to cover all cases, you
need A < B, A = B, A > B, A < C, etc.
> Where does the original justification go wrong? Well, when talking
> about 'the smallest' there is an implicit assumption being made that
> it is unique. The justification never considers the case when A or B
> is the non-unique smallest.
And the same error could be made in a formal specification. "formal"
does _not_ mean "complete"!
> Of course, the correct program just uses "<=" instead of "<",
That would be one way, but it would still require the same detailed
analysis and test. I prefer the exhaustive if/then/else style above.
--
-- Stephe
next prev parent reply other threads:[~2010-07-31 15:12 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-29 12:23 Float conversion Henrique
2010-07-29 12:44 ` Jacob Sparre Andersen
2010-07-29 12:46 ` Dmitry A. Kazakov
2010-07-29 15:08 ` Georg Bauhaus
2010-07-29 15:10 ` Georg Bauhaus
2010-07-29 15:35 ` Dmitry A. Kazakov
2010-07-29 18:21 ` Henrique
2010-07-29 19:08 ` Jeffrey R. Carter
2010-07-29 19:15 ` Dmitry A. Kazakov
2010-07-30 1:30 ` Phil Clayton
2010-07-30 8:43 ` Dmitry A. Kazakov
2010-07-30 13:14 ` Phil Clayton
2010-07-30 14:34 ` Dmitry A. Kazakov
2010-07-31 15:12 ` Stephen Leake [this message]
2010-08-03 1:07 ` Phil Clayton
2010-08-03 3:31 ` Shark8
2010-08-03 10:38 ` Georg Bauhaus
2010-08-04 7:27 ` Stephen Leake
2010-08-04 16:15 ` Georg Bauhaus
2010-08-04 16:32 ` Phil Clayton
2010-08-04 7:26 ` Stephen Leake
2010-08-04 12:52 ` Robert A Duff
2010-08-04 14:32 ` Dmitry A. Kazakov
2010-08-04 19:36 ` Simon Wright
2010-08-04 19:46 ` Dmitry A. Kazakov
2010-08-04 20:29 ` Georg Bauhaus
2010-08-05 12:05 ` Stephen Leake
2010-08-07 5:54 ` Shark8
2010-08-07 8:56 ` Georg Bauhaus
2010-08-07 13:49 ` Shark8
2010-08-05 10:20 ` Phil Clayton
2010-07-30 13:16 ` Henrique
2010-07-29 15:37 ` Warren
2010-07-29 14:56 ` Georg Bauhaus
2010-07-29 17:56 ` Jeffrey R. Carter
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox