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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,6e70c13232dc4a26 X-Google-Attributes: gid103376,public From: "Norman H. Cohen" Subject: Re: logarithms on ada Date: 1997/03/12 Message-ID: <3326E77A.1CDE@watson.ibm.com>#1/1 X-Deja-AN: 225123230 References: <5fcqrs$ius@panther.Gsu.EDU> <331F0AF6.31AF@watson.ibm.com> <5g2nfm$euv$1@goanna.cs.rmit.edu.au> Organization: IBM Thomas J. Watson Research Center Reply-To: ncohen@watson.ibm.com Newsgroups: comp.lang.ada Date: 1997-03-12T00:00:00+00:00 List-Id: Richard A. O'Keefe wrote: > "Norman H. Cohen" 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