1 |
149 |
jeremybenn |
-- CXG2013.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 TAN and COT functions return
|
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 that use an identity for determining the result.
|
37 |
|
|
-- Exception checks.
|
38 |
|
|
--
|
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.
|
43 |
|
|
--
|
44 |
|
|
-- APPLICABILITY CRITERIA:
|
45 |
|
|
-- This test applies only to implementations supporting the
|
46 |
|
|
-- Numerics Annex.
|
47 |
|
|
-- This test only applies to the Strict Mode for numerical
|
48 |
|
|
-- accuracy.
|
49 |
|
|
--
|
50 |
|
|
--
|
51 |
|
|
-- CHANGE HISTORY:
|
52 |
|
|
-- 11 Mar 96 SAIC Initial release for 2.1
|
53 |
|
|
-- 17 Aug 96 SAIC Commentary fixes.
|
54 |
|
|
-- 03 Feb 97 PWB.CTA Removed checks with explicit Cycle => 2.0*Pi
|
55 |
|
|
-- 02 DEC 97 EDS Change Max_Samples constant to 1001.
|
56 |
|
|
-- 29 JUN 98 EDS Deleted Special_Angle_Test as fatally flawed.
|
57 |
|
|
|
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 CXG2013 is
|
82 |
|
|
Verbose : constant Boolean := False;
|
83 |
|
|
Max_Samples : constant := 1001;
|
84 |
|
|
|
85 |
|
|
-- CRC Standard Mathematical Tables; 23rd Edition; pg 738
|
86 |
|
|
Sqrt2 : constant :=
|
87 |
|
|
1.41421_35623_73095_04880_16887_24209_69807_85696_71875_37695;
|
88 |
|
|
Sqrt3 : constant :=
|
89 |
|
|
1.73205_08075_68877_29352_74463_41505_87236_69428_05253_81039;
|
90 |
|
|
|
91 |
|
|
Pi : constant := Ada.Numerics.Pi;
|
92 |
|
|
|
93 |
|
|
generic
|
94 |
|
|
type Real is digits <>;
|
95 |
|
|
package Generic_Check is
|
96 |
|
|
procedure Do_Test;
|
97 |
|
|
end Generic_Check;
|
98 |
|
|
|
99 |
|
|
package body Generic_Check is
|
100 |
|
|
package Elementary_Functions is new
|
101 |
|
|
Ada.Numerics.Generic_Elementary_Functions (Real);
|
102 |
|
|
function Sqrt (X : Real) return Real renames
|
103 |
|
|
Elementary_Functions.Sqrt;
|
104 |
|
|
function Tan (X : Real) return Real renames
|
105 |
|
|
Elementary_Functions.Tan;
|
106 |
|
|
function Cot (X : Real) return Real renames
|
107 |
|
|
Elementary_Functions.Cot;
|
108 |
|
|
function Tan (X, Cycle : Real) return Real renames
|
109 |
|
|
Elementary_Functions.Tan;
|
110 |
|
|
function Cot (X, Cycle : Real) return Real renames
|
111 |
|
|
Elementary_Functions.Cot;
|
112 |
|
|
|
113 |
|
|
-- flag used to terminate some tests early
|
114 |
|
|
Accuracy_Error_Reported : Boolean := False;
|
115 |
|
|
|
116 |
|
|
-- factor to be applied in computing MRE
|
117 |
|
|
Maximum_Relative_Error : constant Real := 4.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 instead
|
128 |
|
|
-- 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 |
|
|
if abs (Actual - Expected) > Max_Error then
|
138 |
|
|
Accuracy_Error_Reported := True;
|
139 |
|
|
Report.Failed (Test_Name &
|
140 |
|
|
" actual: " & Real'Image (Actual) &
|
141 |
|
|
" expected: " & Real'Image (Expected) &
|
142 |
|
|
" difference: " & Real'Image (Actual - Expected) &
|
143 |
|
|
" max err:" & Real'Image (Max_Error) );
|
144 |
|
|
elsif Verbose then
|
145 |
|
|
if Actual = Expected then
|
146 |
|
|
Report.Comment (Test_Name & " exact result");
|
147 |
|
|
else
|
148 |
|
|
Report.Comment (Test_Name & " passed");
|
149 |
|
|
end if;
|
150 |
|
|
end if;
|
151 |
|
|
end Check;
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
procedure Exact_Result_Test is
|
156 |
|
|
No_Error : constant := 0.0;
|
157 |
|
|
begin
|
158 |
|
|
-- A.5.1(38);6.0
|
159 |
|
|
Check (Tan (0.0), 0.0, "tan(0)", No_Error);
|
160 |
|
|
|
161 |
|
|
-- A.5.1(41);6.0
|
162 |
|
|
Check (Tan (180.0, 360.0), 0.0, "tan(180,360)", No_Error);
|
163 |
|
|
Check (Tan (360.0, 360.0), 0.0, "tan(360,360)", No_Error);
|
164 |
|
|
Check (Tan (720.0, 360.0), 0.0, "tan(720,360)", No_Error);
|
165 |
|
|
|
166 |
|
|
-- A.5.1(41);6.0
|
167 |
|
|
Check (Cot ( 90.0, 360.0), 0.0, "cot( 90,360)", No_Error);
|
168 |
|
|
Check (Cot (270.0, 360.0), 0.0, "cot(270,360)", No_Error);
|
169 |
|
|
Check (Cot (810.0, 360.0), 0.0, "cot(810,360)", No_Error);
|
170 |
|
|
|
171 |
|
|
exception
|
172 |
|
|
when Constraint_Error =>
|
173 |
|
|
Report.Failed ("Constraint_Error raised in Exact_Result Test");
|
174 |
|
|
when others =>
|
175 |
|
|
Report.Failed ("exception in Exact_Result Test");
|
176 |
|
|
end Exact_Result_Test;
|
177 |
|
|
|
178 |
|
|
|
179 |
|
|
procedure Tan_Test (A, B : Real) is
|
180 |
|
|
-- Use identity Tan(X) = [2*Tan(x/2)]/[1-Tan(x/2) ** 2]
|
181 |
|
|
-- checks over the range -pi/4 .. pi/4 require no argument reduction
|
182 |
|
|
-- checks over the range 7pi/8 .. 9pi/8 require argument reduction
|
183 |
|
|
X, Y : Real;
|
184 |
|
|
Actual1, Actual2 : Real;
|
185 |
|
|
begin
|
186 |
|
|
Accuracy_Error_Reported := False; -- reset
|
187 |
|
|
for I in 1..Max_Samples loop
|
188 |
|
|
X := (B - A) * Real (I) / Real (Max_Samples) + A;
|
189 |
|
|
-- argument purification to insure x and x/2 are exact
|
190 |
|
|
-- See Cody page 170.
|
191 |
|
|
Y := Real'Machine (X*0.5);
|
192 |
|
|
X := Real'Machine (Y + Y);
|
193 |
|
|
|
194 |
|
|
Actual1 := Tan(X);
|
195 |
|
|
Actual2 := (2.0 * Tan (Y)) / (1.0 - Tan (Y) ** 2);
|
196 |
|
|
|
197 |
|
|
if abs (X - Pi) > ( (B-A)/Real(2*Max_Samples) ) then
|
198 |
|
|
Check (Actual1, Actual2,
|
199 |
|
|
"Tan_Test " & Integer'Image (I) & ": tan(" &
|
200 |
|
|
Real'Image (X) & ") ",
|
201 |
|
|
(1.0 + Sqrt2) * Maximum_Relative_Error);
|
202 |
|
|
-- see Cody pg 165 for error bound info
|
203 |
|
|
end if;
|
204 |
|
|
|
205 |
|
|
if Accuracy_Error_Reported then
|
206 |
|
|
-- only report the first error in this test in order to keep
|
207 |
|
|
-- lots of failures from producing a huge error log
|
208 |
|
|
return;
|
209 |
|
|
end if;
|
210 |
|
|
|
211 |
|
|
end loop;
|
212 |
|
|
|
213 |
|
|
exception
|
214 |
|
|
when Constraint_Error =>
|
215 |
|
|
Report.Failed
|
216 |
|
|
("Constraint_Error raised in Tan_Test");
|
217 |
|
|
when others =>
|
218 |
|
|
Report.Failed ("exception in Tan_Test");
|
219 |
|
|
end Tan_Test;
|
220 |
|
|
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
procedure Cot_Test is
|
224 |
|
|
-- Use identity Cot(X) = [Cot(X/2)**2 - 1]/[2*Cot(X/2)]
|
225 |
|
|
A : constant := 6.0 * Pi;
|
226 |
|
|
B : constant := 25.0 / 4.0 * Pi;
|
227 |
|
|
X, Y : Real;
|
228 |
|
|
Actual1, Actual2 : Real;
|
229 |
|
|
begin
|
230 |
|
|
Accuracy_Error_Reported := False; -- reset
|
231 |
|
|
for I in 1..Max_Samples loop
|
232 |
|
|
X := (B - A) * Real (I) / Real (Max_Samples) + A;
|
233 |
|
|
-- argument purification to insure x and x/2 are exact.
|
234 |
|
|
-- See Cody page 170.
|
235 |
|
|
Y := Real'Machine (X*0.5);
|
236 |
|
|
X := Real'Machine (Y + Y);
|
237 |
|
|
|
238 |
|
|
Actual1 := Cot(X);
|
239 |
|
|
Actual2 := (Cot (Y) ** 2 - 1.0) / (2.0 * Cot (Y));
|
240 |
|
|
|
241 |
|
|
Check (Actual1, Actual2,
|
242 |
|
|
"Cot_Test " & Integer'Image (I) & ": cot(" &
|
243 |
|
|
Real'Image (X) & ") ",
|
244 |
|
|
(1.0 + Sqrt2) * Maximum_Relative_Error);
|
245 |
|
|
-- see Cody pg 165 for error bound info
|
246 |
|
|
|
247 |
|
|
if Accuracy_Error_Reported then
|
248 |
|
|
-- only report the first error in this test in order to keep
|
249 |
|
|
-- lots of failures from producing a huge error log
|
250 |
|
|
return;
|
251 |
|
|
end if;
|
252 |
|
|
|
253 |
|
|
end loop;
|
254 |
|
|
|
255 |
|
|
exception
|
256 |
|
|
when Constraint_Error =>
|
257 |
|
|
Report.Failed
|
258 |
|
|
("Constraint_Error raised in Cot_Test");
|
259 |
|
|
when others =>
|
260 |
|
|
Report.Failed ("exception in Cot_Test");
|
261 |
|
|
end Cot_Test;
|
262 |
|
|
|
263 |
|
|
|
264 |
|
|
procedure Exception_Test is
|
265 |
|
|
X1, X2, X3, X4, X5 : Real := 0.0;
|
266 |
|
|
begin
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
begin -- A.5.1(20);6.0
|
270 |
|
|
X1 := Tan (0.0, Cycle => 0.0);
|
271 |
|
|
Report.Failed ("no exception for cycle = 0.0");
|
272 |
|
|
exception
|
273 |
|
|
when Ada.Numerics.Argument_Error => null;
|
274 |
|
|
when others =>
|
275 |
|
|
Report.Failed ("wrong exception for cycle = 0.0");
|
276 |
|
|
end;
|
277 |
|
|
|
278 |
|
|
begin -- A.5.1(20);6.0
|
279 |
|
|
X2 := Cot (1.0, Cycle => -3.0);
|
280 |
|
|
Report.Failed ("no exception for cycle < 0.0");
|
281 |
|
|
exception
|
282 |
|
|
when Ada.Numerics.Argument_Error => null;
|
283 |
|
|
when others =>
|
284 |
|
|
Report.Failed ("wrong exception for cycle < 0.0");
|
285 |
|
|
end;
|
286 |
|
|
|
287 |
|
|
-- the remaining tests only apply to machines that overflow
|
288 |
|
|
if Real'Machine_Overflows then -- A.5.1(28);6.0
|
289 |
|
|
|
290 |
|
|
begin -- A.5.1(29);6.0
|
291 |
|
|
X3 := Cot (0.0);
|
292 |
|
|
Report.Failed ("exception not raised for cot(0)");
|
293 |
|
|
exception
|
294 |
|
|
when Constraint_Error => null; -- ok
|
295 |
|
|
when others =>
|
296 |
|
|
Report.Failed ("wrong exception raised for cot(0)");
|
297 |
|
|
end;
|
298 |
|
|
|
299 |
|
|
begin -- A.5.1(31);6.0
|
300 |
|
|
X4 := Tan (90.0, 360.0);
|
301 |
|
|
Report.Failed ("exception not raised for tan(90,360)");
|
302 |
|
|
exception
|
303 |
|
|
when Constraint_Error => null; -- ok
|
304 |
|
|
when others =>
|
305 |
|
|
Report.Failed ("wrong exception raised for tan(90,360)");
|
306 |
|
|
end;
|
307 |
|
|
|
308 |
|
|
begin -- A.5.1(32);6.0
|
309 |
|
|
X5 := Cot (180.0, 360.0);
|
310 |
|
|
Report.Failed ("exception not raised for cot(180,360)");
|
311 |
|
|
exception
|
312 |
|
|
when Constraint_Error => null; -- ok
|
313 |
|
|
when others =>
|
314 |
|
|
Report.Failed ("wrong exception raised for cot(180,360)");
|
315 |
|
|
end;
|
316 |
|
|
end if;
|
317 |
|
|
|
318 |
|
|
-- optimizer thwarting
|
319 |
|
|
if Report.Ident_Bool (False) then
|
320 |
|
|
Report.Comment (Real'Image (X1+X2+X3+X4+X5));
|
321 |
|
|
end if;
|
322 |
|
|
end Exception_Test;
|
323 |
|
|
|
324 |
|
|
|
325 |
|
|
procedure Do_Test is
|
326 |
|
|
begin
|
327 |
|
|
Exact_Result_Test;
|
328 |
|
|
Tan_Test (-Pi/4.0, Pi/4.0);
|
329 |
|
|
Tan_Test (7.0*Pi/8.0, 9.0*Pi/8.0);
|
330 |
|
|
Cot_Test;
|
331 |
|
|
Exception_Test;
|
332 |
|
|
end Do_Test;
|
333 |
|
|
end Generic_Check;
|
334 |
|
|
|
335 |
|
|
-----------------------------------------------------------------------
|
336 |
|
|
-----------------------------------------------------------------------
|
337 |
|
|
package Float_Check is new Generic_Check (Float);
|
338 |
|
|
|
339 |
|
|
-- check the floating point type with the most digits
|
340 |
|
|
type A_Long_Float is digits System.Max_Digits;
|
341 |
|
|
package A_Long_Float_Check is new Generic_Check (A_Long_Float);
|
342 |
|
|
|
343 |
|
|
-----------------------------------------------------------------------
|
344 |
|
|
-----------------------------------------------------------------------
|
345 |
|
|
|
346 |
|
|
|
347 |
|
|
begin
|
348 |
|
|
Report.Test ("CXG2013",
|
349 |
|
|
"Check the accuracy of the TAN and COT functions");
|
350 |
|
|
|
351 |
|
|
if Verbose then
|
352 |
|
|
Report.Comment ("checking Standard.Float");
|
353 |
|
|
end if;
|
354 |
|
|
|
355 |
|
|
Float_Check.Do_Test;
|
356 |
|
|
|
357 |
|
|
if Verbose then
|
358 |
|
|
Report.Comment ("checking a digits" &
|
359 |
|
|
Integer'Image (System.Max_Digits) &
|
360 |
|
|
" floating point type");
|
361 |
|
|
end if;
|
362 |
|
|
|
363 |
|
|
A_Long_Float_Check.Do_Test;
|
364 |
|
|
|
365 |
|
|
|
366 |
|
|
Report.Result;
|
367 |
|
|
end CXG2013;
|