comp.lang.ada
 help / color / mirror / Atom feed
From: Phil Clayton <phil.clayton@lineone.net>
Subject: Re: Float conversion
Date: Fri, 30 Jul 2010 06:14:49 -0700 (PDT)
Date: 2010-07-30T06:14:49-07:00	[thread overview]
Message-ID: <d981aeab-67e9-4fde-a96c-ec6a2a403703@c10g2000yqi.googlegroups.com> (raw)
In-Reply-To: 1naf3ekl5k916$.f7ugc92galdz$.dlg@40tude.net

On Jul 30, 9:43 am, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:
> On Thu, 29 Jul 2010 18:30:06 -0700 (PDT), Phil Clayton wrote:
> > However, just because equality between floating point numbers is a
> > dubious concept and should be avoided, it does not mean e.g. "<" and
> > "<=" are simply interchangeable.  Far from it.
>
> They are almost same. According to the extension principle:
>
> I1<=I2
>
>   T (true) if forall x in I1, forall y in I2  x<=y
>   F (false) if forall x in I1, forall y in I2  not x<=y
>   _|_ (undefined) otherwise
>
> I1<I2
>
>   T if forall x in I1, forall y in I2  x<y
>   F if forall x in I1, forall y in I2  not x<y
>   _|_ otherwise
>
> The difference comes from _|_ being rendered T for "<=" and F for "<" when
> I1 and I2 are equal as sets. It would be ill-advised to exploit this
> difference without care.

Certainly ill-advised, but it can be difficult to know when this
difference matters.  This gives me the perfect excuse to wheel out one
of my favourite examples.  It's a great example that I keep coming
back to for many reasons.

We want a 3-way min function (for integers or reals) that gives

  Y = min {A, B, C}

and we are given

  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 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?  Furthermore, the program works when e.g. B = C <
A.  (The issue occurred predictably on the real system however,
because A and B were set to 0 under certain conditions.)

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.

Of course, the correct program just uses "<=" instead of "<", which is
why this example is relevant here: this goes to show that you can't
simply interchange "<" and "<=" for real types.  Furthermore, "<" and
"<=" should be chosen carefully to avoid inadvertent equality
conditions: in this example, using "<" has  introduced an equality
test in the condition for taking the final else branch:

  not (A < B and A < C) and not (B < C and B < A)

is equivalent to

  A = B or (C <= A and C <= B)
  ^^^^^

Finally, this example goes to show that issues resulting from implicit
equality conditions can be really hard to spot!

Of course, I could have pointed out that e.g.

  not (A < B or B < A)

and

  not (A <= B or B <= A)

are not the same but nobody is going to make that mistake - I thought
a real example would be more valuable.  Sorry for the mild digression,
hope that was of interest!

Phil



  reply	other threads:[~2010-07-30 13:14 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 [this message]
2010-07-30 14:34                 ` Dmitry A. Kazakov
2010-07-31 15:12                 ` Stephen Leake
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