3 -- Grant of Unlimited Rights
5 -- Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
6 -- F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained
7 -- unlimited rights in the software and documentation contained herein.
8 -- Unlimited rights are defined in DFAR 252.227-7013(a)(19). By making
9 -- this public release, the Government intends to confer upon all
10 -- recipients unlimited rights equal to those held by the Government.
11 -- These rights include rights to use, duplicate, release or disclose the
12 -- released technical data and computer software in whole or in part, in
13 -- any manner and for any purpose whatsoever, and to have or permit others
18 -- ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR
19 -- DISCLOSED ARE AS IS. THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED
20 -- WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE
21 -- SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE
22 -- OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A
23 -- PARTICULAR PURPOSE OF SAID MATERIAL.
27 -- Check that the complex "abs" or modulus function returns
28 -- results that are within the error bound allowed.
31 -- This test uses a generic package to compute and check the
32 -- values of the modulus function. In addition, a non-generic
33 -- copy of this package is used to check the non-generic package
34 -- Ada.Numerics.Complex_Types.
35 -- Of special interest is the case where either the real or
36 -- the imaginary part of the argument is very large while the
37 -- other part is very small or 0.
38 -- We want to check that the value is computed such that
39 -- an overflow does not occur. If computed directly from the
41 -- abs (x+yi) = sqrt(x**2 + y**2)
42 -- then overflow or underflow is much more likely than if the
43 -- argument is normalized first.
45 -- SPECIAL REQUIREMENTS
46 -- The Strict Mode for the numerical accuracy must be
47 -- selected. The method by which this mode is selected
48 -- is implementation dependent.
50 -- APPLICABILITY CRITERIA:
51 -- This test applies only to implementations supporting the
53 -- This test only applies to the Strict Mode for numerical
58 -- 31 JAN 96 SAIC Initial release for 2.1
59 -- 02 JUN 98 EDS Add parens to intermediate calculations.
64 -- Problems and Methodologies in Mathematical Software Production;
65 -- editors: P. C. Messina and A Murli;
66 -- Lecture Notes in Computer Science
68 -- Springer Verlag 1982
73 with Ada
.Numerics
.Generic_Complex_Types
;
74 with Ada
.Numerics
.Complex_Types
;
76 Verbose
: constant Boolean := False;
77 Maximum_Relative_Error
: constant := 3.0;
80 type Real
is digits <>;
81 package Generic_Check
is
85 package body Generic_Check
is
86 package Complex_Types
is new
87 Ada
.Numerics
.Generic_Complex_Types
(Real
);
90 procedure Check
(Actual
, Expected
: Real
;
92 MRE
: Real
:= Maximum_Relative_Error
) is
97 -- In the case where the expected result is very small or 0
98 -- we compute the maximum error as a multiple of Model_Epsilon instead
99 -- of Model_Epsilon and Expected.
100 Rel_Error
:= MRE
* (abs Expected
* Real
'Model_Epsilon);
101 Abs_Error
:= MRE
* Real
'Model_Epsilon;
102 if Rel_Error
> Abs_Error
then
103 Max_Error
:= Rel_Error
;
105 Max_Error
:= Abs_Error
;
108 if abs (Actual
- Expected
) > Max_Error
then
109 Report
.Failed
(Test_Name
&
110 " actual: " & Real
'Image (Actual
) &
111 " expected: " & Real
'Image (Expected
) &
113 Real
'Image (Expected
- Actual
) &
114 " max_err:" & Real
'Image (Max_Error
) );
116 if Actual
= Expected
then
117 Report
.Comment
(Test_Name
& " exact result");
119 Report
.Comment
(Test_Name
& " passed");
136 Check
(X
, T
, "test 1 -- abs(bigreal + 0i)");
138 when Constraint_Error
=>
139 Report
.Failed
("Constraint_Error raised in test 1");
141 Report
.Failed
("exception in test 1");
149 Check
(X
, T
, "test 2 -- abs(0 + bigreal*i)");
151 when Constraint_Error
=>
152 Report
.Failed
("Constraint_Error raised in test 2");
154 Report
.Failed
("exception in test 2");
161 Check
(X
, 5.0 , "test 3 -- abs(3 + 4*i)");
163 when Constraint_Error
=>
164 Report
.Failed
("Constraint_Error raised in test 3");
166 Report
.Failed
("exception in test 3");
173 S
:= Real
(Real
'Machine_Radix) ** (Real
'Machine_EMax - 3);
174 Z
:= 3.0 * S
+ 4.0*S
*i
;
176 Check
(X
, 5.0*S
, "test 4 -- abs(3S + 4S*i) for large S",
177 5.0*Real
'Model_Epsilon);
179 when Constraint_Error
=>
180 Report
.Failed
("Constraint_Error raised in test 4");
182 Report
.Failed
("exception in test 4");
187 T
:= Real
'Model_Small;
190 Check
(X
, T
, "test 5 -- abs(small + 0*i)");
192 when Constraint_Error
=>
193 Report
.Failed
("Constraint_Error raised in test 5");
195 Report
.Failed
("exception in test 5");
200 T
:= Real
'Model_Small;
203 Check
(X
, T
, "test 6 -- abs(0 + small*i)");
205 when Constraint_Error
=>
206 Report
.Failed
("Constraint_Error raised in test 6");
208 Report
.Failed
("exception in test 6");
215 S
:= Real
(Real
'Machine_Radix) ** (Real
'Model_EMin + 3);
216 Z
:= 3.0 * S
+ 4.0*S
*i
;
218 Check
(X
, 5.0*S
, "test 7 -- abs(3S + 4S*i) for small S",
219 5.0*Real
'Model_Epsilon);
221 when Constraint_Error
=>
222 Report
.Failed
("Constraint_Error raised in test 7");
224 Report
.Failed
("exception in test 7");
229 -- CRC Standard Mathematical Tables; 23rd Edition; pg 738
231 1.41421_35623_73095_04880_16887_24209_69807_85696_71875_37695
;
235 Check
(X
, Sqrt2
, "test 8 -- abs(1 + 1*i)");
237 when Constraint_Error
=>
238 Report
.Failed
("Constraint_Error raised in test 8");
240 Report
.Failed
("exception in test 8");
248 Check
(X
, T
, "test 5 -- abs(0 + 0*i)");
250 when Constraint_Error
=>
251 Report
.Failed
("Constraint_Error raised in test 9");
253 Report
.Failed
("exception in test 9");
258 -----------------------------------------------------------------------
259 --- non generic copy of the above generic package
260 -----------------------------------------------------------------------
262 package Non_Generic_Check
is
263 subtype Real
is Float;
265 end Non_Generic_Check
;
267 package body Non_Generic_Check
is
268 use Ada
.Numerics
.Complex_Types
;
270 procedure Check
(Actual
, Expected
: Real
;
272 MRE
: Real
:= Maximum_Relative_Error
) is
277 -- In the case where the expected result is very small or 0
278 -- we compute the maximum error as a multiple of Model_Epsilon instead
279 -- of Model_Epsilon and Expected.
280 Rel_Error
:= MRE
* (abs Expected
* Real
'Model_Epsilon);
281 Abs_Error
:= MRE
* Real
'Model_Epsilon;
282 if Rel_Error
> Abs_Error
then
283 Max_Error
:= Rel_Error
;
285 Max_Error
:= Abs_Error
;
288 if abs (Actual
- Expected
) > Max_Error
then
289 Report
.Failed
(Test_Name
&
290 " actual: " & Real
'Image (Actual
) &
291 " expected: " & Real
'Image (Expected
) &
293 Real
'Image (Expected
- Actual
) &
294 " max_err:" & Real
'Image (Max_Error
) );
296 if Actual
= Expected
then
297 Report
.Comment
(Test_Name
& " exact result");
299 Report
.Comment
(Test_Name
& " passed");
316 Check
(X
, T
, "test 1 -- abs(bigreal + 0i)");
318 when Constraint_Error
=>
319 Report
.Failed
("Constraint_Error raised in test 1");
321 Report
.Failed
("exception in test 1");
329 Check
(X
, T
, "test 2 -- abs(0 + bigreal*i)");
331 when Constraint_Error
=>
332 Report
.Failed
("Constraint_Error raised in test 2");
334 Report
.Failed
("exception in test 2");
341 Check
(X
, 5.0 , "test 3 -- abs(3 + 4*i)");
343 when Constraint_Error
=>
344 Report
.Failed
("Constraint_Error raised in test 3");
346 Report
.Failed
("exception in test 3");
353 S
:= Real
(Real
'Machine_Radix) ** (Real
'Machine_EMax - 3);
354 Z
:= 3.0 * S
+ 4.0*S
*i
;
356 Check
(X
, 5.0*S
, "test 4 -- abs(3S + 4S*i) for large S",
357 5.0*Real
'Model_Epsilon);
359 when Constraint_Error
=>
360 Report
.Failed
("Constraint_Error raised in test 4");
362 Report
.Failed
("exception in test 4");
367 T
:= Real
'Model_Small;
370 Check
(X
, T
, "test 5 -- abs(small + 0*i)");
372 when Constraint_Error
=>
373 Report
.Failed
("Constraint_Error raised in test 5");
375 Report
.Failed
("exception in test 5");
380 T
:= Real
'Model_Small;
383 Check
(X
, T
, "test 6 -- abs(0 + small*i)");
385 when Constraint_Error
=>
386 Report
.Failed
("Constraint_Error raised in test 6");
388 Report
.Failed
("exception in test 6");
395 S
:= Real
(Real
'Machine_Radix) ** (Real
'Model_EMin + 3);
396 Z
:= 3.0 * S
+ 4.0*S
*i
;
398 Check
(X
, 5.0*S
, "test 7 -- abs(3S + 4S*i) for small S",
399 5.0*Real
'Model_Epsilon);
401 when Constraint_Error
=>
402 Report
.Failed
("Constraint_Error raised in test 7");
404 Report
.Failed
("exception in test 7");
409 -- CRC Standard Mathematical Tables; 23rd Edition; pg 738
411 1.41421_35623_73095_04880_16887_24209_69807_85696_71875_37695
;
415 Check
(X
, Sqrt2
, "test 8 -- abs(1 + 1*i)");
417 when Constraint_Error
=>
418 Report
.Failed
("Constraint_Error raised in test 8");
420 Report
.Failed
("exception in test 8");
428 Check
(X
, T
, "test 5 -- abs(0 + 0*i)");
430 when Constraint_Error
=>
431 Report
.Failed
("Constraint_Error raised in test 9");
433 Report
.Failed
("exception in test 9");
436 end Non_Generic_Check
;
438 -----------------------------------------------------------------------
439 --- end of "manual instantiation"
440 -----------------------------------------------------------------------
441 package Chk_Float
is new Generic_Check
(Float);
443 -- check the floating point type with the most digits
444 type A_Long_Float
is digits System
.Max_Digits
;
445 package Chk_A_Long_Float
is new Generic_Check
(A_Long_Float
);
447 Report
.Test
("CXG2002",
448 "Check the accuracy of the complex modulus" &
452 Report
.Comment
("checking Standard.Float");
457 Report
.Comment
("checking a digits" &
458 Integer'Image (System
.Max_Digits
) &
459 " floating point type");
461 Chk_A_Long_Float
.Do_Test
;
464 Report
.Comment
("checking non-generic package");
466 Non_Generic_Check
.Do_Test
;