Previous | Next | Table of Contents | Index | Program List | Copyright

4.6 System Structures: Using Ada's Math Library

We use computers for many kinds of applications, some of which require that we actually write programs to do mathematical computations. It is therefore useful to have available a set of the usual elementary functions such as square root, sine, cosine, and so on. Ada provides such a set of functions in a standard library called Ada.Numerics.Elementary_Functions. The full description of this standard library is found in Appendix F; Program 4.5 shows how one of the its functions, Sqrt, is used to compute a square root.

Program 4.5
Computing Several Square Roots

WITH Ada.Text_IO;
WITH Ada.Float_Text_IO;
WITH Ada.Numerics.Elementary_Functions;
PROCEDURE Square_Roots IS
------------------------------------------------------------------------
--| Illustrates the square root function provided by
--| the standard Ada math function package.
--| Author: Michael B. Feldman, The George Washington University
--| Last Modified: July 1995
------------------------------------------------------------------------

  SUBTYPE NonNegFloat IS Float RANGE 0.0 .. Float'Last;

  First : NonNegFloat;
  Second: NonNegFloat;
  Answer: NonNegFloat;

BEGIN  -- Square_Roots

  Ada.Text_IO.Put (Item => "Please enter first number > ");
  Ada.Float_Text_IO.Get(Item => First);
  Answer := Ada.Numerics.Elementary_Functions.Sqrt(First);
  Ada.Text_IO.Put (Item => "The first number's square root is ");
  Ada.Float_Text_IO.Put (Item => Answer, Fore => 1, Aft => 5, Exp => 0);
  Ada.Text_IO.New_Line;

  Ada.Text_IO.Put (Item => "Please enter second number > ");
  Ada.Float_Text_IO.Get(Item => Second);
  Ada.Text_IO.Put (Item => "The second number's square root is ");
  Ada.Float_Text_IO.Put
    (Item => Ada.Numerics.Elementary_Functions.Sqrt (Second),
     Fore => 1, Aft => 5, Exp => 0);
  Ada.Text_IO.New_Line;

  Answer := Ada.Numerics.Elementary_Functions.Sqrt(First + Second);
  Ada.Text_IO.Put 
    (Item => "The square root of the sum of the numbers is ");
  Ada.Float_Text_IO.Put (Item => Answer, Fore => 1, Aft => 5, Exp => 0);
  Ada.Text_IO.New_Line;

END Square_Roots;
Sample Run
Please enter first number > 9
The first number's square root is 3.00000
Please enter second number > 16
The second number's square root is 4.00000
The square root of the sum of the numbers is 5.00000
As you can see from the sample run, this program prompts the user for two floating-point values and computes the square roots of the two numbers and of their sum. Note in the program that the second call of the square root function is nested in the Put statement and that in the third call, the parameter is the expression First + Second. This is just to illustrate that function calls can be nested in other expressions and that expressions can be nested in function calls.

In Chapter 7 we'll come back to the math library.


Previous | Next | Table of Contents | Index | Program List | Copyright

Copyright © 1996 by Addison-Wesley Publishing Company, Inc.