comp.lang.ada
 help / color / mirror / Atom feed
From: "Norman H. Cohen" <ncohen@watson.ibm.com>
Subject: Re: logarithms on ada
Date: 1997/03/12
Date: 1997-03-12T00:00:00+00:00	[thread overview]
Message-ID: <3326E77A.1CDE@watson.ibm.com> (raw)
In-Reply-To: 5g2nfm$euv$1@goanna.cs.rmit.edu.au


Richard A. O'Keefe wrote:
 
> "Norman H. Cohen" <ncohen@watson.ibm.com> writes:
> >In most applications, you don't have to understand all the subtleties of
> >floating-point arithmetic to use floating point.  An age-old
> >introductory programming assignment is to read three floating-point
> >numbers A, B, and C, and print (approximations of) the solutions to the
> >quadratic equation A * x**2 + B * x + C = 0.
> 
> How very subtle of Norman Cohen to cite the very example that goes
> disastrously wrong if you don't understand the problems of floating
> point.  (At the very least you have to know what "cancellation" means.
> And you also have to know about a less direct way to get the "other"
> answer.)
> 
> >Students doing this assignment have to know where to find
> >Ada.Numerics.Elementary_Functions.Sqrt.  They don't have to know how to
> >determine convergence of Newton's alogrithm for square roots.
> 
> But they do need to understand "cancellation".

Come back down to earth, Richard!  I said that we're talking about an
>>introductory<< programming assignment, for which a naive program like the one shown below is a perfectly acceptable response.  

   with Ada.Numerics.Elementary_Functions; 
   use Ada.Numerics.Elementary_Functions;
   with Ada.Text_IO, Ada.Float_Text_IO; 
   use Ada.Text_IO, Ada.Float_Text_IO;

   procedure Quadratic is
      A, B, C, D, Sqrt_D: Float;
   begin
      Put_Line ("Enter quadratic equation coefficients A, B, and C:");
      Flush;
      Get(A);
      Get(B);
      Get(C);
      if A = 0.0 then
         Put_Line ("Equation is not quadratic because A is zero.");
      else
         D := B**2 - 4.0 * A * C;
         if D < 0.0 then
            Put_Line ("There are no real solutions.");
         else
            Sqrt_D := Sqrt(D);
            Put ("Solution 1: ");
            Put ( (-B + Sqrt_D) / (2.0*A) );
            New_Line;
            Put ("Solution 2: ");
            Put ( (-B - Sqrt_D) / (2.0*A) );
            New_Line;
         end if;
      end if;
   end Quadratic;

The recipients of this assignment may have less than a month of
programming experience.  An instructor may wish to explain at this point
in the course that floating-point types are only approximations of real
numbers, and that this can lead to surprising results.  (If the user
enters 1E-50 for A, the program may report that A is zero; if the user
enters 1E-40 for A, the program may fail because of overflow on the
division.)  The instructor may then mention that there is a subfield of
computer science devoted to determining the accuracy and safety of
floating-point computations, and to writing programs in a way that
maximizes these attributes.  No more needs to be said, or understood by
the students, at this point.  (The instructor may wish to revisit this
example later in the course to make it safe, not by numerical analysis,
but simply by providing exception handlers.)

In fact, the program will work acceptably for all input values that the
typical novice programmer will supply when testing the program.  (My
experience as a graduate teaching assistant for a course that had an
introductory assignment like this was that many students only used test
data for which A=1.0, because they could factor those quadratic
equations, and check their answers, more easily.  Many of these students
also forgot to parenthesize the denominator 2.0*A and never caught their
mistake, which has no effect when A=1.0.  So much for path coverage. 
:-) )

Even outside the classroom, there are plenty of applications that stay
far enough away from extreme values and singularities that they can be
written and used in a numerically naive manner.

-- 
Norman H. Cohen
mailto:ncohen@watson.ibm.com
http://www.research.ibm.com/people/n/ncohen




  reply	other threads:[~1997-03-12  0:00 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-03-02  0:00 logarithms on ada Brian R Franklin
1997-03-04  0:00 ` John McCabe
1997-03-06  0:00   ` Robert Dewar
1997-03-07  0:00     ` John McCabe
1997-03-07  0:00       ` Robert Dewar
1997-03-09  0:00         ` John McCabe
1997-03-09  0:00           ` Robert Dewar
1997-03-10  0:00             ` John McCabe
1997-03-10  0:00               ` Robert Dewar
1997-03-11  0:00                 ` John McCabe
1997-03-11  0:00                   ` Robert Dewar
1997-03-12  0:00                     ` John McCabe
1997-03-07  0:00   ` Darren C Davenport
1997-03-04  0:00 ` Bob Klungle
1997-03-04  0:00   ` Robert Dewar
1997-03-05  0:00     ` Doug Smith
1997-03-05  0:00       ` Robert Dewar
1997-03-06  0:00         ` Robert A Duff
1997-03-05  0:00           ` John McCabe
1997-03-06  0:00           ` Robert Dewar
1997-03-06  0:00           ` Norman H. Cohen
1997-03-08  0:00             ` Robert Dewar
1997-03-08  0:00             ` Robert Dewar
1997-03-11  0:00             ` Richard A. O'Keefe
1997-03-12  0:00               ` Norman H. Cohen [this message]
1997-03-06  0:00           ` Doug Smith
1997-03-06  0:00       ` Robert A Duff
1997-03-06  0:00         ` Andrew Dunstan
1997-03-10  0:00         ` Doug Smith
1997-03-11  0:00           ` Robert Dewar
1997-03-05  0:00     ` Bob Klungle
1997-03-05  0:00       ` David Shochat
1997-03-05  0:00         ` Robert Dewar
1997-03-06  0:00         ` Robert A Duff
1997-03-06  0:00           ` Robert Dewar
1997-03-05  0:00   ` Jon S Anthony
1997-03-04  0:00 ` Tom Moran
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox