Debian Codegraph @

gcc/testsuite/ada/acats/tests/cxg/cxg2011.a

raw
1 -- CXG2011.A
2 --
3 -- Grant of Unlimited Rights
4 --
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
14 -- to do so.
15 --
16 -- DISCLAIMER
17 --
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.
24 --*
25 --
26 -- OBJECTIVE:
27 -- Check that the log function returns
28 -- results that are within the error bound allowed.
29 --
30 -- TEST DESCRIPTION:
31 -- This test consists of a generic package that is
32 -- instantiated to check both Float and a long float type.
33 -- The test for each floating point type is divided into
34 -- several parts:
35 -- Special value checks where the result is a known constant.
36 -- Checks in a range where a Taylor series can be used to compute
37 -- the expected result.
38 -- Checks that use an identity for determining the result.
39 -- Exception checks.
40 --
41 -- SPECIAL REQUIREMENTS
42 -- The Strict Mode for the numerical accuracy must be
43 -- selected. The method by which this mode is selected
44 -- is implementation dependent.
45 --
46 -- APPLICABILITY CRITERIA:
47 -- This test applies only to implementations supporting the
48 -- Numerics Annex.
49 -- This test only applies to the Strict Mode for numerical
50 -- accuracy.
51 --
52 --
53 -- CHANGE HISTORY:
54 -- 1 Mar 96 SAIC Initial release for 2.1
55 -- 22 Aug 96 SAIC Improved Check routine
56 -- 02 DEC 97 EDS Log (0.0) must raise Constraint_Error,
57 -- not Argument_Error
58 --!
59
60 --
61 -- References:
62 --
63 -- Software Manual for the Elementary Functions
64 -- William J. Cody, Jr. and William Waite
65 -- Prentice-Hall, 1980
66 --
67 -- CRC Standard Mathematical Tables
68 -- 23rd Edition
69 --
70 -- Implementation and Testing of Function Software
71 -- W. J. Cody
72 -- Problems and Methodologies in Mathematical Software Production
73 -- editors P. C. Messina and A. Murli
74 -- Lecture Notes in Computer Science Volume 142
75 -- Springer Verlag, 1982
76 --
77
78 with System;
79 with Report;
80 with Ada.Numerics.Generic_Elementary_Functions;
81 procedure CXG2011 is
82 Verbose : constant Boolean := False;
83 Max_Samples : constant := 1000;
84
85 -- CRC Handbook Page 738
86 Ln10 : constant := 2.30258_50929_94045_68401_79914_54684_36420_76011_01489;
87 Ln2 : constant := 0.69314_71805_59945_30941_72321_21458_17656_80755_00134;
88
89 generic
90 type Real is digits <>;
91 package Generic_Check is
92 procedure Do_Test;
93 end Generic_Check;
94
95 package body Generic_Check is
96 package Elementary_Functions is new
97 Ada.Numerics.Generic_Elementary_Functions (Real);
98 function Sqrt (X : Real'Base) return Real'Base renames
99 Elementary_Functions.Sqrt;
100 function Exp (X : Real'Base) return Real'Base renames
101 Elementary_Functions.Exp;
102 function Log (X : Real'Base) return Real'Base renames
103 Elementary_Functions.Log;
104 function Log (X, Base : Real'Base) return Real'Base renames
105 Elementary_Functions.Log;
106
107 -- flag used to terminate some tests early
108 Accuracy_Error_Reported : Boolean := False;
109
110
111 -- The following value is a lower bound on the accuracy
112 -- required. It is normally 0.0 so that the lower bound
113 -- is computed from Model_Epsilon. However, for tests
114 -- where the expected result is only known to a certain
115 -- amount of precision this bound takes on a non-zero
116 -- value to account for that level of precision.
117 Error_Low_Bound : Real := 0.0;
118
119 procedure Check (Actual, Expected : Real;
120 Test_Name : String;
121 MRE : Real) is
122 Max_Error : Real;
123 Rel_Error : Real;
124 Abs_Error : Real;
125 begin
126 -- In the case where the expected result is very small or 0
127 -- we compute the maximum error as a multiple of Model_Epsilon
128 -- instead of Model_Epsilon and Expected.
129 Rel_Error := MRE * abs Expected * Real'Model_Epsilon;
130 Abs_Error := MRE * Real'Model_Epsilon;
131 if Rel_Error > Abs_Error then
132 Max_Error := Rel_Error;
133 else
134 Max_Error := Abs_Error;
135 end if;
136
137 -- take into account the low bound on the error
138 if Max_Error < Error_Low_Bound then
139 Max_Error := Error_Low_Bound;
140 end if;
141
142 if abs (Actual - Expected) > Max_Error then
143 Accuracy_Error_Reported := True;
144 Report.Failed (Test_Name &
145 " actual: " & Real'Image (Actual) &
146 " expected: " & Real'Image (Expected) &
147 " difference: " & Real'Image (Actual - Expected) &
148 " max err:" & Real'Image (Max_Error) );
149 elsif Verbose then
150 if Actual = Expected then
151 Report.Comment (Test_Name & " exact result");
152 else
153 Report.Comment (Test_Name & " passed");
154 end if;
155 end if;
156 end Check;
157
158
159 procedure Special_Value_Test is
160 begin
161
162 --- test 1 ---
163 declare
164 Y : Real;
165 begin
166 Y := Log(1.0);
167 Check (Y, 0.0, "special value test 1 -- log(1)",
168 0.0); -- no error allowed
169 exception
170 when Constraint_Error =>
171 Report.Failed ("Constraint_Error raised in test 1");
172 when others =>
173 Report.Failed ("exception in test 1");
174 end;
175
176 --- test 2 ---
177 declare
178 Y : Real;
179 begin
180 Y := Log(10.0);
181 Check (Y, Ln10, "special value test 2 -- log(10)", 4.0);
182 exception
183 when Constraint_Error =>
184 Report.Failed ("Constraint_Error raised in test 2");
185 when others =>
186 Report.Failed ("exception in test 2");
187 end;
188
189 --- test 3 ---
190 declare
191 Y : Real;
192 begin
193 Y := Log (2.0);
194 Check (Y, Ln2, "special value test 3 -- log(2)", 4.0);
195 exception
196 when Constraint_Error =>
197 Report.Failed ("Constraint_Error raised in test 3");
198 when others =>
199 Report.Failed ("exception in test 3");
200 end;
201
202 --- test 4 ---
203 declare
204 Y : Real;
205 begin
206 Y := Log (2.0 ** 18, 2.0);
207 Check (Y, 18.0, "special value test 4 -- log(2**18,2)", 4.0);
208 exception
209 when Constraint_Error =>
210 Report.Failed ("Constraint_Error raised in test 4");
211 when others =>
212 Report.Failed ("exception in test 4");
213 end;
214 end Special_Value_Test;
215
216
217 procedure Taylor_Series_Test is
218 -- Use a 4 term taylor series expansion to check a selection of
219 -- arguments very near 1.0.
220 -- The range is chosen so that the 4 term taylor series will
221 -- provide accuracy to machine precision. Cody pg 49-50.
222 Half_Range : constant Real := Real'Model_Epsilon * 50.0;
223 A : constant Real := 1.0 - Half_Range;
224 B : constant Real := 1.0 + Half_Range;
225 X : Real;
226 Xm1 : Real;
227 Expected : Real;
228 Actual : Real;
229
230 begin
231 Accuracy_Error_Reported := False; -- reset
232 for I in 1..Max_Samples loop
233 X := (B - A) * Real (I) / Real (Max_Samples) + A;
234
235 Xm1 := X - 1.0;
236 -- The following is the first 4 terms of the taylor series
237 -- that has been rearranged to minimize error in the calculation
238 Expected := (Xm1 * (1.0/3.0 - Xm1/4.0) - 0.5) * Xm1 * Xm1 + Xm1;
239
240 Actual := Log (X);
241 Check (Actual, Expected,
242 "Taylor Series Test -" &
243 Integer'Image (I) &
244 " log (" & Real'Image (X) & ")",
245 4.0);
246 if Accuracy_Error_Reported then
247 -- only report the first error in this test in order to keep
248 -- lots of failures from producing a huge error log
249 return;
250 end if;
251 end loop;
252 exception
253 when Constraint_Error =>
254 Report.Failed
255 ("Constraint_Error raised in Taylor Series Test");
256 when others =>
257 Report.Failed ("exception in Taylor Series Test");
258 end Taylor_Series_Test;
259
260
261
262 procedure Log_Difference_Identity is
263 -- Check using the identity ln(x) = ln(17x/16) - ln(17/16)
264 -- over the range A to B.
265 -- The selected range assures that both X and 17x/16 will
266 -- have the same exponents and neither argument gets too close
267 -- to 1. Cody pg 50.
268 A : constant Real := 1.0 / Sqrt (2.0);
269 B : constant Real := 15.0 / 16.0;
270 X : Real;
271 Expected : Real;
272 Actual : Real;
273 begin
274 Accuracy_Error_Reported := False; -- reset
275 for I in 1..Max_Samples loop
276 X := (B - A) * Real (I) / Real (Max_Samples) + A;
277 -- magic argument purification
278 X := Real'Machine (Real'Machine (X+8.0) - 8.0);
279
280 Expected := Log (X + X / 16.0) - Log (17.0/16.0);
281
282 Actual := Log (X);
283 Check (Actual, Expected,
284 "Log Difference Identity -" &
285 Integer'Image (I) &
286 " log (" & Real'Image (X) & ")",
287 4.0);
288
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
292 return;
293 end if;
294 end loop;
295 exception
296 when Constraint_Error =>
297 Report.Failed
298 ("Constraint_Error raised in Log Difference Identity Test");
299 when others =>
300 Report.Failed ("exception in Log Difference Identity Test");
301 end Log_Difference_Identity;
302
303
304 procedure Log_Product_Identity is
305 -- Check using the identity ln(x**2) = 2ln(x)
306 -- over the range A to B.
307 -- This large range is chosen to minimize the possibility of
308 -- undetected systematic errors. Cody pg 53.
309 A : constant Real := 16.0;
310 B : constant Real := 240.0;
311 X : Real;
312 Expected : Real;
313 Actual : Real;
314 begin
315 Accuracy_Error_Reported := False; -- reset
316 for I in 1..Max_Samples loop
317 X := (B - A) * Real (I) / Real (Max_Samples) + A;
318 -- magic argument purification
319 X := Real'Machine (Real'Machine (X+8.0) - 8.0);
320
321 Expected := 2.0 * Log (X);
322
323 Actual := Log (X*X);
324 Check (Actual, Expected,
325 "Log Product Identity -" &
326 Integer'Image (I) &
327 " log (" & Real'Image (X) & ")",
328 4.0);
329 if Accuracy_Error_Reported then
330 -- only report the first error in this test in order to keep
331 -- lots of failures from producing a huge error log
332 return;
333 end if;
334 end loop;
335 exception
336 when Constraint_Error =>
337 Report.Failed
338 ("Constraint_Error raised in Log Product Identity Test");
339 when others =>
340 Report.Failed ("exception in Log Product Identity Test");
341 end Log_Product_Identity;
342
343
344 procedure Log10_Test is
345 -- Check using the identity log(x) = log(11x/10) - log(1.1)
346 -- over the range A to B. See Cody pg 52.
347 A : constant Real := 1.0 / Sqrt (10.0);
348 B : constant Real := 0.9;
349 X : Real;
350 Expected : Real;
351 Actual : Real;
352 begin
353 if Real'Digits > 17 then
354 -- constant used below is accuract to 17 digits
355 Error_Low_Bound := 0.00000_00000_00000_01;
356 Report.Comment ("log accuracy checked to 19 digits");
357 end if;
358 Accuracy_Error_Reported := False; -- reset
359 for I in 1..Max_Samples loop
360 X := (B - A) * Real (I) / Real (Max_Samples) + A;
361
362 Expected := Log (X + X/10.0, 10.0)
363 - 3.77060_15822_50407_5E-4 - 21.0 / 512.0;
364
365 Actual := Log (X, 10.0);
366 Check (Actual, Expected,
367 "Log 10 Test -" &
368 Integer'Image (I) &
369 " log (" & Real'Image (X) & ")",
370 4.0);
371
372 -- only report the first error in this test in order to keep
373 -- lots of failures from producing a huge error log
374 exit when Accuracy_Error_Reported;
375 end loop;
376 Error_Low_Bound := 0.0; -- reset
377
378 exception
379 when Constraint_Error =>
380 Report.Failed
381 ("Constraint_Error raised in Log 10 Test");
382 when others =>
383 Report.Failed ("exception in Log 10 Test");
384 end Log10_Test;
385
386
387 procedure Exception_Test is
388 X1, X2, X3, X4 : Real;
389 begin
390 begin
391 X1 := Log (0.0);
392 Report.Failed ("exception not raised for LOG(0)");
393 exception
394 -- Log (0.0) must raise Constraint_Error, not Argument_Error,
395 -- as per A.5.1(28,29). Was incorrect in ACVC 2.1 release.
396 when Ada.Numerics.Argument_Error =>
397 Report.Failed ("Argument_Error raised instead of" &
398 " Constraint_Error for LOG(0)--A.5.1(28,29)");
399 when Constraint_Error => null; -- ok
400 when others =>
401 Report.Failed ("wrong exception raised for LOG(0)");
402 end;
403
404 begin
405 X2 := Log ( 1.0, 0.0);
406 Report.Failed ("exception not raised for LOG(1,0)");
407 exception
408 when Ada.Numerics.Argument_Error => null; -- ok
409 when Constraint_Error =>
410 Report.Failed ("constraint_error raised instead of" &
411 " argument_error for LOG(1,0)");
412 when others =>
413 Report.Failed ("wrong exception raised for LOG(1,0)");
414 end;
415
416 begin
417 X3 := Log (1.0, 1.0);
418 Report.Failed ("exception not raised for LOG(1,1)");
419 exception
420 when Ada.Numerics.Argument_Error => null; -- ok
421 when Constraint_Error =>
422 Report.Failed ("constraint_error raised instead of" &
423 " argument_error for LOG(1,1)");
424 when others =>
425 Report.Failed ("wrong exception raised for LOG(1,1)");
426 end;
427
428 begin
429 X4 := Log (1.0, -10.0);
430 Report.Failed ("exception not raised for LOG(1,-10)");
431 exception
432 when Ada.Numerics.Argument_Error => null; -- ok
433 when Constraint_Error =>
434 Report.Failed ("constraint_error raised instead of" &
435 " argument_error for LOG(1,-10)");
436 when others =>
437 Report.Failed ("wrong exception raised for LOG(1,-10)");
438 end;
439
440 -- optimizer thwarting
441 if Report.Ident_Bool (False) then
442 Report.Comment (Real'Image (X1+X2+X3+X4));
443 end if;
444 end Exception_Test;
445
446
447 procedure Do_Test is
448 begin
449 Special_Value_Test;
450 Taylor_Series_Test;
451 Log_Difference_Identity;
452 Log_Product_Identity;
453 Log10_Test;
454 Exception_Test;
455 end Do_Test;
456 end Generic_Check;
457
458 -----------------------------------------------------------------------
459 -----------------------------------------------------------------------
460 package Float_Check is new Generic_Check (Float);
461
462 -- check the floating point type with the most digits
463 type A_Long_Float is digits System.Max_Digits;
464 package A_Long_Float_Check is new Generic_Check (A_Long_Float);
465
466 -----------------------------------------------------------------------
467 -----------------------------------------------------------------------
468
469
470 begin
471 Report.Test ("CXG2011",
472 "Check the accuracy of the log function");
473
474 if Verbose then
475 Report.Comment ("checking Standard.Float");
476 end if;
477
478 Float_Check.Do_Test;
479
480 if Verbose then
481 Report.Comment ("checking a digits" &
482 Integer'Image (System.Max_Digits) &
483 " floating point type");
484 end if;
485
486 A_Long_Float_Check.Do_Test;
487
488
489 Report.Result;
490 end CXG2011;