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 EXP function returns
28 -- a result that is within the error bound allowed.
31 -- This test consists of a generic package that is
32 -- instantiated to check complex numbers based upon
33 -- both Float and a long float type.
34 -- The test for each floating point type is divided into
36 -- Special value checks where the result is a known constant.
37 -- Checks that use an identity for determining the result.
39 -- SPECIAL REQUIREMENTS
40 -- The Strict Mode for the numerical accuracy must be
41 -- selected. The method by which this mode is selected
42 -- is implementation dependent.
44 -- APPLICABILITY CRITERIA:
45 -- This test applies only to implementations supporting the
47 -- This test only applies to the Strict Mode for numerical
52 -- 21 Mar 96 SAIC Initial release for 2.1
53 -- 17 Aug 96 SAIC Incorporated reviewer comments.
54 -- 27 Aug 99 RLB Repair on the error result of checks.
55 -- 02 Apr 03 RLB Added code to discard excess precision in the
56 -- construction of the test value for the
65 -- CELEFUNT: A Portable Test Package for Complex Elementary Functions
66 -- Algorithm 714, Collected Algorithms from ACM.
67 -- Published in Transactions On Mathematical Software,
68 -- Vol. 19, No. 1, March, 1993, pp. 1-21.
70 -- CRC Standard Mathematical Tables
76 with Ada
.Numerics
.Generic_Complex_Types
;
77 with Ada
.Numerics
.Generic_Complex_Elementary_Functions
;
79 Verbose
: constant Boolean := False;
80 -- Note that Max_Samples is the number of samples taken in
81 -- both the real and imaginary directions. Thus, for Max_Samples
82 -- of 100 the number of values checked is 10000.
83 Max_Samples
: constant := 100;
85 E
: constant := Ada
.Numerics
.E
;
86 Pi
: constant := Ada
.Numerics
.Pi
;
89 type Real
is digits <>;
90 package Generic_Check
is
94 package body Generic_Check
is
95 package Complex_Type
is new
96 Ada
.Numerics
.Generic_Complex_Types
(Real
);
100 Ada
.Numerics
.Generic_Complex_Elementary_Functions
(Complex_Type
);
102 function Exp
(X
: Complex
) return Complex
renames CEF
.Exp
;
103 function Exp
(X
: Imaginary
) return Complex
renames CEF
.Exp
;
105 -- flag used to terminate some tests early
106 Accuracy_Error_Reported
: Boolean := False;
109 -- The following value is a lower bound on the accuracy
110 -- required. It is normally 0.0 so that the lower bound
111 -- is computed from Model_Epsilon. However, for tests
112 -- where the expected result is only known to a certain
113 -- amount of precision this bound takes on a non-zero
114 -- value to account for that level of precision.
115 Error_Low_Bound
: Real
:= 0.0;
117 procedure Check
(Actual
, Expected
: Real
;
124 -- In the case where the expected result is very small or 0
125 -- we compute the maximum error as a multiple of Model_Small instead
126 -- of Model_Epsilon and Expected.
127 Rel_Error
:= MRE
* abs Expected
* Real
'Model_Epsilon;
128 Abs_Error
:= MRE
* Real
'Model_Small;
129 if Rel_Error
> Abs_Error
then
130 Max_Error
:= Rel_Error
;
132 Max_Error
:= Abs_Error
;
135 -- take into account the low bound on the error
136 if Max_Error
< Error_Low_Bound
then
137 Max_Error
:= Error_Low_Bound
;
140 if abs (Actual
- Expected
) > Max_Error
then
141 Accuracy_Error_Reported
:= True;
142 Report
.Failed
(Test_Name
&
143 " actual: " & Real
'Image (Actual
) &
144 " expected: " & Real
'Image (Expected
) &
145 " difference: " & Real
'Image (Actual
- Expected
) &
146 " max err:" & Real
'Image (Max_Error
) );
148 if Actual
= Expected
then
149 Report
.Comment
(Test_Name
& " exact result");
151 Report
.Comment
(Test_Name
& " passed");
157 procedure Check
(Actual
, Expected
: Complex
;
161 Check
(Actual
.Re
, Expected
.Re
, Test_Name
& " real part", MRE
);
162 Check
(Actual
.Im
, Expected
.Im
, Test_Name
& " imaginary part", MRE
);
166 procedure Special_Value_Test
is
167 -- In the following tests the expected result is accurate
168 -- to the machine precision so the minimum guaranteed error
169 -- bound can be used.
171 -- The error bounds given assumed z is exact. When using
172 -- pi there is an extra error of 1.0ME.
173 -- The pi inside the exp call requires that the complex
174 -- component have an extra error allowance of 1.0*angle*ME.
175 -- Thus for pi/2,the Minimum_Error_I is
176 -- (2.0 + 1.0(pi/2))ME <= 3.6ME.
177 -- For pi, it is (2.0 + 1.0*pi)ME <= 5.2ME,
178 -- and for 2pi, it is (2.0 + 1.0(2pi))ME <= 8.3ME.
180 -- The addition of 1 or i to a result is so that neither of
181 -- the components of an expected result is 0. This is so
182 -- that a reasonable relative error is allowed.
183 Minimum_Error_C
: constant := 7.0; -- for exp(Complex)
184 Minimum_Error_I
: constant := 2.0; -- for exp(Imaginary)
186 Check
(Exp
(1.0 + 0.0*i
) + i
,
190 Check
(Exp
((Pi
/ 2.0) * i
) + 1.0,
194 Check
(Exp
(Pi
* i
) + i
,
198 Check
(Exp
(Pi
* 2.0 * i
) + i
,
203 when Constraint_Error
=>
204 Report
.Failed
("Constraint_Error raised in special value test");
206 Report
.Failed
("exception in special value test");
207 end Special_Value_Test
;
211 procedure Exact_Result_Test
is
212 No_Error
: constant := 0.0;
215 Check
(Exp
(0.0 + 0.0*i
), 1.0 + 0.0 * i
, "exp(0+0i)", No_Error
);
216 Check
(Exp
( 0.0*i
), 1.0 + 0.0 * i
, "exp(0i)", No_Error
);
218 when Constraint_Error
=>
219 Report
.Failed
("Constraint_Error raised in Exact_Result Test");
221 Report
.Failed
("exception in Exact_Result Test");
222 end Exact_Result_Test
;
225 procedure Identity_Test
(A
, B
: Real
) is
226 -- For this test we use the identity
227 -- Exp(Z) = Exp(Z-W) * Exp (W)
228 -- where W = (1+i)/16
230 -- The second part of this test checks the identity
231 -- Exp(Z) * Exp(-Z) = 1
235 Actual1
, Actual2
: Complex
;
236 W
: constant Complex
:= (0.0625, 0.0625);
237 -- the following constant was taken from the CELEFUNC EXP test.
238 -- This is the value EXP(W) - 1
239 C
: constant Complex
:= (6.2416044877018563681e-2,
240 6.6487597751003112768e-2);
242 if Real
'Digits > 20 then
243 -- constant ExpW is accurate to 20 digits.
244 -- The low bound is 19 * 10**-20
245 Error_Low_Bound
:= 0.00000_00000_00019
;
246 Report
.Comment
("complex exp accuracy checked to 20 digits");
249 Accuracy_Error_Reported
:= False; -- reset
250 for II
in 1..Max_Samples
loop
251 X
.Re
:= Real
'Machine ((B
- A
) * Real
(II
) / Real
(Max_Samples
)
253 for J
in 1..Max_Samples
loop
254 X
.Im
:= Real
'Machine ((B
- A
) * Real
(J
) / Real
(Max_Samples
)
259 -- Exp(X) = Exp(X-W) * Exp (W)
260 -- = Exp(X-W) * (1 - (1-Exp(W))
261 -- = Exp(X-W) * (1 + (Exp(W) - 1))
262 -- = Exp(X-W) * (1 + C)
265 Actual2
:= Actual2
+ Actual2
* C
;
267 Check
(Actual1
, Actual2
,
268 "Identity_1_Test " & Integer'Image (II
) &
269 Integer'Image (J
) & ": Exp((" &
270 Real
'Image (X
.Re
) & ", " &
271 Real
'Image (X
.Im
) & ")) ",
272 20.0); -- 2 exp and 1 multiply and 1 add = 2*7+1*5+1
273 -- Note: The above is not strictly correct, as multiply
274 -- has a box error, rather than a relative error.
275 -- Supposedly, the interval is chosen to avoid the need
276 -- to worry about this.
278 -- Exp(X) * Exp(-X) + i = 1 + i
279 -- The addition of i is to allow a reasonable relative
280 -- error in the imaginary part
281 Actual2
:= (Actual1
* Exp
(-X
)) + i
;
282 Check
(Actual2
, (1.0, 1.0),
283 "Identity_2_Test " & Integer'Image (II
) &
284 Integer'Image (J
) & ": Exp((" &
285 Real
'Image (X
.Re
) & ", " &
286 Real
'Image (X
.Im
) & ")) ",
287 20.0); -- 2 exp and 1 multiply and one add = 2*7+1*5+1
289 if Accuracy_Error_Reported
then
290 -- only report the first error in this test in order to keep
291 -- lots of failures from producing a huge error log
296 Error_Low_Bound
:= 0.0;
298 when Constraint_Error
=>
300 ("Constraint_Error raised in Identity_Test" &
301 " for X=(" & Real
'Image (X
.Re
) &
302 ", " & Real
'Image (X
.Im
) & ")");
304 Report
.Failed
("exception in Identity_Test" &
305 " for X=(" & Real
'Image (X
.Re
) &
306 ", " & Real
'Image (X
.Im
) & ")");
315 -- test regions where we can avoid cancellation error problems
317 Identity_Test
(0.0625, 1.0);
318 Identity_Test
(15.0, 17.0);
319 Identity_Test
(1.625, 3.0);
323 -----------------------------------------------------------------------
324 -----------------------------------------------------------------------
325 package Float_Check
is new Generic_Check
(Float);
327 -- check the floating point type with the most digits
328 type A_Long_Float
is digits System
.Max_Digits
;
329 package A_Long_Float_Check
is new Generic_Check
(A_Long_Float
);
331 -----------------------------------------------------------------------
332 -----------------------------------------------------------------------
336 Report
.Test
("CXG2018",
337 "Check the accuracy of the complex EXP function");
340 Report
.Comment
("checking Standard.Float");
346 Report
.Comment
("checking a digits" &
347 Integer'Image (System
.Max_Digits
) &
348 " floating point type");
351 A_Long_Float_Check
.Do_Test
;