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 sqrt function returns
28 -- results that are within the error bound allowed.
31 -- This test contains three test packages that are almost
32 -- identical. The first two packages differ only in the
33 -- floating point type that is being tested. The first
34 -- and third package differ only in whether the generic
35 -- elementary functions package or the pre-instantiated
37 -- The test package is not generic so that the arguments
38 -- and expected results for some of the test values
39 -- can be expressed as universal real instead of being
40 -- computed at runtime.
42 -- SPECIAL REQUIREMENTS
43 -- The Strict Mode for the numerical accuracy must be
44 -- selected. The method by which this mode is selected
45 -- is implementation dependent.
47 -- APPLICABILITY CRITERIA:
48 -- This test applies only to implementations supporting the
50 -- This test only applies to the Strict Mode for numerical
55 -- 2 FEB 96 SAIC Initial release for 2.1
56 -- 18 AUG 96 SAIC Made Check consistent with other tests.
62 with Ada
.Numerics
.Generic_Elementary_Functions
;
63 with Ada
.Numerics
.Elementary_Functions
;
65 Verbose
: constant Boolean := False;
67 package Float_Check
is
68 subtype Real
is Float;
72 package body Float_Check
is
73 package Elementary_Functions
is new
74 Ada
.Numerics
.Generic_Elementary_Functions
(Real
);
75 function Sqrt
(X
: Real
) return Real
renames
76 Elementary_Functions
.Sqrt
;
77 function Log
(X
: Real
) return Real
renames
78 Elementary_Functions
.Log
;
79 function Exp
(X
: Real
) return Real
renames
80 Elementary_Functions
.Exp
;
82 -- The default Maximum Relative Error is the value specified
84 Default_MRE
: constant Real
:= 2.0;
86 procedure Check
(Actual
, Expected
: Real
;
88 MRE
: Real
:= Default_MRE
) is
93 -- In the case where the expected result is very small or 0
94 -- we compute the maximum error as a multiple of Model_Epsilon instead
95 -- of Model_Epsilon and Expected.
96 Rel_Error
:= MRE
* abs Expected
* Real
'Model_Epsilon;
97 Abs_Error
:= MRE
* Real
'Model_Epsilon;
98 if Rel_Error
> Abs_Error
then
99 Max_Error
:= Rel_Error
;
101 Max_Error
:= Abs_Error
;
104 if abs (Actual
- Expected
) > Max_Error
then
105 Report
.Failed
(Test_Name
&
106 " actual: " & Real
'Image (Actual
) &
107 " expected: " & Real
'Image (Expected
) &
109 Real
'Image (Actual
- Expected
) &
110 " mre:" & Real
'Image (Max_Error
) );
112 if Actual
= Expected
then
113 Report
.Comment
(Test_Name
& " exact result");
115 Report
.Comment
(Test_Name
& " passed");
121 procedure Argument_Range_Check
(A
, B
: Real
;
123 -- test a logarithmically distributed selection of
124 -- arguments selected from the range A to B.
128 C
: Real
:= Log
(B
/A
);
129 Max_Samples
: constant := 1000;
132 for I
in 1..Max_Samples
loop
133 Expected
:= A
* Exp
(C
* Real
(I
) / Real
(Max_Samples
));
134 X
:= Expected
* Expected
;
137 -- note that since the expected value is computed, we
138 -- must take the error in that computation into account.
140 "test " & Test
& " -" &
142 " of argument range",
146 when Constraint_Error
=>
148 ("Constraint_Error raised in argument range check");
150 Report
.Failed
("exception in argument range check");
151 end Argument_Range_Check
;
158 T
: constant := (Real
'Machine_EMax - 1) / 2;
159 X
: constant := (1.0 * Real
'Machine_Radix) ** (2 * T
);
160 Expected
: constant := (1.0 * Real
'Machine_Radix) ** T
;
164 Check
(Y
, Expected
, "test 1 -- sqrt(radix**((emax-1)/2))");
166 when Constraint_Error
=>
167 Report
.Failed
("Constraint_Error raised in test 1");
169 Report
.Failed
("exception in test 1");
174 T
: constant := (Real
'Model_EMin + 1) / 2;
175 X
: constant := (1.0 * Real
'Machine_Radix) ** (2 * T
);
176 Expected
: constant := (1.0 * Real
'Machine_Radix) ** T
;
180 Check
(Y
, Expected
, "test 2 -- sqrt(radix**((emin+1)/2))");
182 when Constraint_Error
=>
183 Report
.Failed
("Constraint_Error raised in test 2");
185 Report
.Failed
("exception in test 2");
191 Expected
: constant := 1.0;
195 Check
(Y
, Expected
, "test 3 -- sqrt(1.0)",
196 0.0); -- no error allowed
198 when Constraint_Error
=>
199 Report
.Failed
("Constraint_Error raised in test 3");
201 Report
.Failed
("exception in test 3");
207 Expected
: constant := 0.0;
211 Check
(Y
, Expected
, "test 4 -- sqrt(0.0)",
212 0.0); -- no error allowed
214 when Constraint_Error
=>
215 Report
.Failed
("Constraint_Error raised in test 4");
217 Report
.Failed
("exception in test 4");
222 X
: constant := -1.0;
226 -- the following code should not be executed.
227 -- The call to Check is to keep the call to Sqrt from
228 -- appearing to be dead code.
229 Check
(Y
, -1.0, "test 5 -- sqrt(-1)" );
230 Report
.Failed
("test 5 - argument_error expected");
232 when Constraint_Error
=>
233 Report
.Failed
("Constraint_Error raised in test 5");
234 when Ada
.Numerics
.Argument_Error
=>
236 Report
.Comment
("test 5 correctly got argument_error");
239 Report
.Failed
("exception in test 5");
244 X
: constant := Ada
.Numerics
.Pi
** 2;
245 Expected
: constant := Ada
.Numerics
.Pi
;
249 Check
(Y
, Expected
, "test 6 -- sqrt(pi**2)");
251 when Constraint_Error
=>
252 Report
.Failed
("Constraint_Error raised in test 6");
254 Report
.Failed
("exception in test 6");
258 Argument_Range_Check
(1.0/Sqrt
(Real
(Real
'Machine_Radix)),
261 Argument_Range_Check
(1.0,
262 Sqrt
(Real
(Real
'Machine_Radix)),
267 -----------------------------------------------------------------------
268 -----------------------------------------------------------------------
269 -- check the floating point type with the most digits
270 type A_Long_Float
is digits System
.Max_Digits
;
273 package A_Long_Float_Check
is
274 subtype Real
is A_Long_Float
;
276 end A_Long_Float_Check
;
278 package body A_Long_Float_Check
is
279 package Elementary_Functions
is new
280 Ada
.Numerics
.Generic_Elementary_Functions
(Real
);
281 function Sqrt
(X
: Real
) return Real
renames
282 Elementary_Functions
.Sqrt
;
283 function Log
(X
: Real
) return Real
renames
284 Elementary_Functions
.Log
;
285 function Exp
(X
: Real
) return Real
renames
286 Elementary_Functions
.Exp
;
288 -- The default Maximum Relative Error is the value specified
290 Default_MRE
: constant Real
:= 2.0;
292 procedure Check
(Actual
, Expected
: Real
;
294 MRE
: Real
:= Default_MRE
) is
299 -- In the case where the expected result is very small or 0
300 -- we compute the maximum error as a multiple of Model_Epsilon instead
301 -- of Model_Epsilon and Expected.
302 Rel_Error
:= MRE
* abs Expected
* Real
'Model_Epsilon;
303 Abs_Error
:= MRE
* Real
'Model_Epsilon;
304 if Rel_Error
> Abs_Error
then
305 Max_Error
:= Rel_Error
;
307 Max_Error
:= Abs_Error
;
310 if abs (Actual
- Expected
) > Max_Error
then
311 Report
.Failed
(Test_Name
&
312 " actual: " & Real
'Image (Actual
) &
313 " expected: " & Real
'Image (Expected
) &
315 Real
'Image (Actual
- Expected
) &
316 " mre:" & Real
'Image (Max_Error
) );
318 if Actual
= Expected
then
319 Report
.Comment
(Test_Name
& " exact result");
321 Report
.Comment
(Test_Name
& " passed");
327 procedure Argument_Range_Check
(A
, B
: Real
;
329 -- test a logarithmically distributed selection of
330 -- arguments selected from the range A to B.
334 C
: Real
:= Log
(B
/A
);
335 Max_Samples
: constant := 1000;
338 for I
in 1..Max_Samples
loop
339 Expected
:= A
* Exp
(C
* Real
(I
) / Real
(Max_Samples
));
340 X
:= Expected
* Expected
;
343 -- note that since the expected value is computed, we
344 -- must take the error in that computation into account.
346 "test " & Test
& " -" &
348 " of argument range",
352 when Constraint_Error
=>
354 ("Constraint_Error raised in argument range check");
356 Report
.Failed
("exception in argument range check");
357 end Argument_Range_Check
;
365 T
: constant := (Real
'Machine_EMax - 1) / 2;
366 X
: constant := (1.0 * Real
'Machine_Radix) ** (2 * T
);
367 Expected
: constant := (1.0 * Real
'Machine_Radix) ** T
;
371 Check
(Y
, Expected
, "test 1 -- sqrt(radix**((emax-1)/2))");
373 when Constraint_Error
=>
374 Report
.Failed
("Constraint_Error raised in test 1");
376 Report
.Failed
("exception in test 1");
381 T
: constant := (Real
'Model_EMin + 1) / 2;
382 X
: constant := (1.0 * Real
'Machine_Radix) ** (2 * T
);
383 Expected
: constant := (1.0 * Real
'Machine_Radix) ** T
;
387 Check
(Y
, Expected
, "test 2 -- sqrt(radix**((emin+1)/2))");
389 when Constraint_Error
=>
390 Report
.Failed
("Constraint_Error raised in test 2");
392 Report
.Failed
("exception in test 2");
398 Expected
: constant := 1.0;
402 Check
(Y
, Expected
, "test 3 -- sqrt(1.0)",
403 0.0); -- no error allowed
405 when Constraint_Error
=>
406 Report
.Failed
("Constraint_Error raised in test 3");
408 Report
.Failed
("exception in test 3");
414 Expected
: constant := 0.0;
418 Check
(Y
, Expected
, "test 4 -- sqrt(0.0)",
419 0.0); -- no error allowed
421 when Constraint_Error
=>
422 Report
.Failed
("Constraint_Error raised in test 4");
424 Report
.Failed
("exception in test 4");
429 X
: constant := -1.0;
433 -- the following code should not be executed.
434 -- The call to Check is to keep the call to Sqrt from
435 -- appearing to be dead code.
436 Check
(Y
, -1.0, "test 5 -- sqrt(-1)" );
437 Report
.Failed
("test 5 - argument_error expected");
439 when Constraint_Error
=>
440 Report
.Failed
("Constraint_Error raised in test 5");
441 when Ada
.Numerics
.Argument_Error
=>
443 Report
.Comment
("test 5 correctly got argument_error");
446 Report
.Failed
("exception in test 5");
451 X
: constant := Ada
.Numerics
.Pi
** 2;
452 Expected
: constant := Ada
.Numerics
.Pi
;
456 Check
(Y
, Expected
, "test 6 -- sqrt(pi**2)");
458 when Constraint_Error
=>
459 Report
.Failed
("Constraint_Error raised in test 6");
461 Report
.Failed
("exception in test 6");
465 Argument_Range_Check
(1.0/Sqrt
(Real
(Real
'Machine_Radix)),
468 Argument_Range_Check
(1.0,
469 Sqrt
(Real
(Real
'Machine_Radix)),
472 end A_Long_Float_Check
;
474 -----------------------------------------------------------------------
475 -----------------------------------------------------------------------
477 package Non_Generic_Check
is
479 end Non_Generic_Check
;
481 package body Non_Generic_Check
is
483 Ada
.Numerics
.Elementary_Functions
;
484 subtype Real
is Float;
486 -- The default Maximum Relative Error is the value specified
488 Default_MRE
: constant Real
:= 2.0;
490 procedure Check
(Actual
, Expected
: Real
;
492 MRE
: Real
:= Default_MRE
) is
497 -- In the case where the expected result is very small or 0
498 -- we compute the maximum error as a multiple of Model_Epsilon instead
499 -- of Model_Epsilon and Expected.
500 Rel_Error
:= MRE
* abs Expected
* Real
'Model_Epsilon;
501 Abs_Error
:= MRE
* Real
'Model_Epsilon;
502 if Rel_Error
> Abs_Error
then
503 Max_Error
:= Rel_Error
;
505 Max_Error
:= Abs_Error
;
508 if abs (Actual
- Expected
) > Max_Error
then
509 Report
.Failed
(Test_Name
&
510 " actual: " & Real
'Image (Actual
) &
511 " expected: " & Real
'Image (Expected
) &
513 Real
'Image (Actual
- Expected
) &
514 " mre:" & Real
'Image (Max_Error
) );
516 if Actual
= Expected
then
517 Report
.Comment
(Test_Name
& " exact result");
519 Report
.Comment
(Test_Name
& " passed");
526 procedure Argument_Range_Check
(A
, B
: Float;
528 -- test a logarithmically distributed selection of
529 -- arguments selected from the range A to B.
533 C
: Float := EF
.Log
(B
/A
);
534 Max_Samples
: constant := 1000;
537 for I
in 1..Max_Samples
loop
538 Expected
:= A
* EF
.Exp
(C
* Float (I
) / Float (Max_Samples
));
539 X
:= Expected
* Expected
;
542 -- note that since the expected value is computed, we
543 -- must take the error in that computation into account.
545 "test " & Test
& " -" &
547 " of argument range",
551 when Constraint_Error
=>
553 ("Constraint_Error raised in argument range check");
555 Report
.Failed
("exception in argument range check");
556 end Argument_Range_Check
;
564 T
: constant := (Float'Machine_EMax - 1) / 2;
565 X
: constant := (1.0 * Float'Machine_Radix) ** (2 * T
);
566 Expected
: constant := (1.0 * Float'Machine_Radix) ** T
;
570 Check
(Y
, Expected
, "test 1 -- sqrt(radix**((emax-1)/2))");
572 when Constraint_Error
=>
573 Report
.Failed
("Constraint_Error raised in test 1");
575 Report
.Failed
("exception in test 1");
580 T
: constant := (Float'Model_EMin + 1) / 2;
581 X
: constant := (1.0 * Float'Machine_Radix) ** (2 * T
);
582 Expected
: constant := (1.0 * Float'Machine_Radix) ** T
;
586 Check
(Y
, Expected
, "test 2 -- sqrt(radix**((emin+1)/2))");
588 when Constraint_Error
=>
589 Report
.Failed
("Constraint_Error raised in test 2");
591 Report
.Failed
("exception in test 2");
597 Expected
: constant := 1.0;
601 Check
(Y
, Expected
, "test 3 -- sqrt(1.0)",
602 0.0); -- no error allowed
604 when Constraint_Error
=>
605 Report
.Failed
("Constraint_Error raised in test 3");
607 Report
.Failed
("exception in test 3");
613 Expected
: constant := 0.0;
617 Check
(Y
, Expected
, "test 4 -- sqrt(0.0)",
618 0.0); -- no error allowed
620 when Constraint_Error
=>
621 Report
.Failed
("Constraint_Error raised in test 4");
623 Report
.Failed
("exception in test 4");
628 X
: constant := -1.0;
632 -- the following code should not be executed.
633 -- The call to Check is to keep the call to Sqrt from
634 -- appearing to be dead code.
635 Check
(Y
, -1.0, "test 5 -- sqrt(-1)" );
636 Report
.Failed
("test 5 - argument_error expected");
638 when Constraint_Error
=>
639 Report
.Failed
("Constraint_Error raised in test 5");
640 when Ada
.Numerics
.Argument_Error
=>
642 Report
.Comment
("test 5 correctly got argument_error");
645 Report
.Failed
("exception in test 5");
650 X
: constant := Ada
.Numerics
.Pi
** 2;
651 Expected
: constant := Ada
.Numerics
.Pi
;
655 Check
(Y
, Expected
, "test 6 -- sqrt(pi**2)");
657 when Constraint_Error
=>
658 Report
.Failed
("Constraint_Error raised in test 6");
660 Report
.Failed
("exception in test 6");
664 Argument_Range_Check
(1.0/EF
.Sqrt
(Float(Float'Machine_Radix)),
667 Argument_Range_Check
(1.0,
668 EF
.Sqrt
(Float(Float'Machine_Radix)),
671 end Non_Generic_Check
;
673 -----------------------------------------------------------------------
674 -----------------------------------------------------------------------
677 Report
.Test
("CXG2003",
678 "Check the accuracy of the sqrt function");
681 Report
.Comment
("checking Standard.Float");
687 Report
.Comment
("checking a digits" &
688 Integer'Image (System
.Max_Digits
) &
689 " floating point type");
692 A_Long_Float_Check
.Do_Test
;
695 Report
.Comment
("checking non-generic package");
698 Non_Generic_Check
.Do_Test
;