| 1 |
-- CXG2019.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 complex LOG function returns |
| 28 |
-- a result that is within the error bound allowed. |
| 29 |
-- |
| 30 |
-- TEST DESCRIPTION: |
| 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 |
| 35 |
-- several parts: |
| 36 |
-- Special value checks where the result is a known constant. |
| 37 |
-- Checks that use an identity for determining the result. |
| 38 |
-- Exception conditions. |
| 39 |
-- |
| 40 |
-- SPECIAL REQUIREMENTS |
| 41 |
-- The Strict Mode for the numerical accuracy must be |
| 42 |
-- selected. The method by which this mode is selected |
| 43 |
-- is implementation dependent. |
| 44 |
-- |
| 45 |
-- APPLICABILITY CRITERIA: |
| 46 |
-- This test applies only to implementations supporting the |
| 47 |
-- Numerics Annex. |
| 48 |
-- This test only applies to the Strict Mode for numerical |
| 49 |
-- accuracy. |
| 50 |
-- |
| 51 |
-- |
| 52 |
-- CHANGE HISTORY: |
| 53 |
-- 22 Mar 96 SAIC Initial release for 2.1 |
| 54 |
-- |
| 55 |
--! |
| 56 |
|
| 57 |
-- |
| 58 |
-- References: |
| 59 |
-- |
| 60 |
-- W. J. Cody |
| 61 |
-- CELEFUNT: A Portable Test Package for Complex Elementary Functions |
| 62 |
-- Algorithm 714, Collected Algorithms from ACM. |
| 63 |
-- Published in Transactions On Mathematical Software, |
| 64 |
-- Vol. 19, No. 1, March, 1993, pp. 1-21. |
| 65 |
-- |
| 66 |
-- CRC Standard Mathematical Tables |
| 67 |
-- 23rd Edition |
| 68 |
-- |
| 69 |
|
| 70 |
with System; |
| 71 |
with Report; |
| 72 |
with Ada.Numerics.Generic_Complex_Types; |
| 73 |
with Ada.Numerics.Generic_Complex_Elementary_Functions; |
| 74 |
procedure CXG2019 is |
| 75 |
Verbose : constant Boolean := False; |
| 76 |
-- Note that Max_Samples is the number of samples taken in |
| 77 |
-- both the real and imaginary directions. Thus, for Max_Samples |
| 78 |
-- of 100 the number of values checked is 10000. |
| 79 |
Max_Samples : constant := 100; |
| 80 |
|
| 81 |
E : constant := Ada.Numerics.E; |
| 82 |
Pi : constant := Ada.Numerics.Pi; |
| 83 |
|
| 84 |
generic |
| 85 |
type Real is digits <>; |
| 86 |
package Generic_Check is |
| 87 |
procedure Do_Test; |
| 88 |
end Generic_Check; |
| 89 |
|
| 90 |
package body Generic_Check is |
| 91 |
package Complex_Type is new |
| 92 |
Ada.Numerics.Generic_Complex_Types (Real); |
| 93 |
use Complex_Type; |
| 94 |
|
| 95 |
package CEF is new |
| 96 |
Ada.Numerics.Generic_Complex_Elementary_Functions (Complex_Type); |
| 97 |
|
| 98 |
function Log (X : Complex) return Complex renames CEF.Log; |
| 99 |
|
| 100 |
-- flag used to terminate some tests early |
| 101 |
Accuracy_Error_Reported : Boolean := False; |
| 102 |
|
| 103 |
|
| 104 |
procedure Check (Actual, Expected : Real; |
| 105 |
Test_Name : String; |
| 106 |
MRE : Real) is |
| 107 |
Max_Error : Real; |
| 108 |
Rel_Error : Real; |
| 109 |
Abs_Error : Real; |
| 110 |
begin |
| 111 |
-- In the case where the expected result is very small or 0 |
| 112 |
-- we compute the maximum error as a multiple of Model_Small instead |
| 113 |
-- of Model_Epsilon and Expected. |
| 114 |
Rel_Error := MRE * abs Expected * Real'Model_Epsilon; |
| 115 |
Abs_Error := MRE * Real'Model_Epsilon; |
| 116 |
if Rel_Error > Abs_Error then |
| 117 |
Max_Error := Rel_Error; |
| 118 |
else |
| 119 |
Max_Error := Abs_Error; |
| 120 |
end if; |
| 121 |
|
| 122 |
if abs (Actual - Expected) > Max_Error then |
| 123 |
Accuracy_Error_Reported := True; |
| 124 |
Report.Failed (Test_Name & |
| 125 |
" actual: " & Real'Image (Actual) & |
| 126 |
" expected: " & Real'Image (Expected) & |
| 127 |
" difference: " & Real'Image (Actual - Expected) & |
| 128 |
" max err:" & Real'Image (Max_Error) ); |
| 129 |
elsif Verbose then |
| 130 |
if Actual = Expected then |
| 131 |
Report.Comment (Test_Name & " exact result"); |
| 132 |
else |
| 133 |
Report.Comment (Test_Name & " passed"); |
| 134 |
end if; |
| 135 |
end if; |
| 136 |
end Check; |
| 137 |
|
| 138 |
|
| 139 |
procedure Check (Actual, Expected : Complex; |
| 140 |
Test_Name : String; |
| 141 |
MRE : Real) is |
| 142 |
begin |
| 143 |
Check (Actual.Re, Expected.Re, Test_Name & " real part", MRE); |
| 144 |
Check (Actual.Im, Expected.Im, Test_Name & " imaginary part", MRE); |
| 145 |
end Check; |
| 146 |
|
| 147 |
|
| 148 |
procedure Special_Value_Test is |
| 149 |
-- In the following tests the expected result is accurate |
| 150 |
-- to the machine precision so the minimum guaranteed error |
| 151 |
-- bound can be used if the argument is exact. |
| 152 |
-- |
| 153 |
-- When using pi there is an extra error of 1.0ME. |
| 154 |
-- Although the real component has an error bound of 13.0, |
| 155 |
-- the complex component must take into account this error |
| 156 |
-- in the value for Pi. |
| 157 |
-- |
| 158 |
-- One or i is added to the actual and expected results in |
| 159 |
-- order to prevent the expected result from having a |
| 160 |
-- real or imaginary part of 0. This is to allow a reasonable |
| 161 |
-- relative error for that component. |
| 162 |
Minimum_Error : constant := 13.0; |
| 163 |
begin |
| 164 |
Check (1.0 + Log (0.0 + i), |
| 165 |
1.0 + Pi / 2.0 * i, |
| 166 |
"1+log(0+i)", |
| 167 |
Minimum_Error + 1.0); |
| 168 |
Check (1.0 + Log ((-1.0, 0.0)), |
| 169 |
1.0 + (Pi * i), |
| 170 |
"log(-1+0i)+1 ", |
| 171 |
Minimum_Error + 1.0); |
| 172 |
exception |
| 173 |
when Constraint_Error => |
| 174 |
Report.Failed ("Constraint_Error raised in special value test"); |
| 175 |
when others => |
| 176 |
Report.Failed ("exception in special value test"); |
| 177 |
end Special_Value_Test; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
procedure Exact_Result_Test is |
| 182 |
No_Error : constant := 0.0; |
| 183 |
begin |
| 184 |
-- G.1.2(37);6.0 |
| 185 |
Check (Log(1.0 + 0.0*i), 0.0 + 0.0 * i, "log(1+0i)", No_Error); |
| 186 |
exception |
| 187 |
when Constraint_Error => |
| 188 |
Report.Failed ("Constraint_Error raised in Exact_Result Test"); |
| 189 |
when others => |
| 190 |
Report.Failed ("exception in Exact_Result Test"); |
| 191 |
end Exact_Result_Test; |
| 192 |
|
| 193 |
|
| 194 |
procedure Identity_Test (RA, RB, IA, IB : Real) is |
| 195 |
-- Tests an identity over a range of values specified |
| 196 |
-- by the 4 parameters. RA and RB denote the range for the |
| 197 |
-- real part while IA and IB denote the range for the |
| 198 |
-- imaginary part. |
| 199 |
-- |
| 200 |
-- For this test we use the identity |
| 201 |
-- Log(Z*Z) = 2 * Log(Z) |
| 202 |
-- |
| 203 |
|
| 204 |
Scale : Real := Real (Real'Machine_Radix) ** (Real'Mantissa / 2 + 4); |
| 205 |
W, X, Y, Z : Real; |
| 206 |
CX, CY : Complex; |
| 207 |
Actual1, Actual2 : Complex; |
| 208 |
begin |
| 209 |
Accuracy_Error_Reported := False; -- reset |
| 210 |
for II in 1..Max_Samples loop |
| 211 |
X := (RB - RA) * Real (II) / Real (Max_Samples) + RA; |
| 212 |
for J in 1..Max_Samples loop |
| 213 |
Y := (IB - IA) * Real (J) / Real (Max_Samples) + IA; |
| 214 |
|
| 215 |
-- purify the arguments to minimize roundoff error. |
| 216 |
-- We construct the values so that the products X*X, |
| 217 |
-- Y*Y, and X*Y are all exact machine numbers. |
| 218 |
-- See Cody page 7 and CELEFUNT code. |
| 219 |
Z := X * Scale; |
| 220 |
W := Z + X; |
| 221 |
X := W - Z; |
| 222 |
Z := Y * Scale; |
| 223 |
W := Z + Y; |
| 224 |
Y := W - Z; |
| 225 |
CX := Compose_From_Cartesian(X,Y); |
| 226 |
Z := X*X - Y*Y; |
| 227 |
W := X*Y; |
| 228 |
CY := Compose_From_Cartesian(Z,W+W); |
| 229 |
|
| 230 |
-- The arguments are now ready so on with the |
| 231 |
-- identity computation. |
| 232 |
Actual1 := Log(CX); |
| 233 |
|
| 234 |
Actual2 := Log(CY) * 0.5; |
| 235 |
|
| 236 |
Check (Actual1, Actual2, |
| 237 |
"Identity_1_Test " & Integer'Image (II) & |
| 238 |
Integer'Image (J) & ": Log((" & |
| 239 |
Real'Image (CX.Re) & ", " & |
| 240 |
Real'Image (CX.Im) & ")) ", |
| 241 |
26.0); -- 2 logs = 2*13. no error from this multiply |
| 242 |
|
| 243 |
if Accuracy_Error_Reported then |
| 244 |
-- only report the first error in this test in order to keep |
| 245 |
-- lots of failures from producing a huge error log |
| 246 |
return; |
| 247 |
end if; |
| 248 |
end loop; |
| 249 |
end loop; |
| 250 |
|
| 251 |
exception |
| 252 |
when Constraint_Error => |
| 253 |
Report.Failed |
| 254 |
("Constraint_Error raised in Identity_Test" & |
| 255 |
" for X=(" & Real'Image (X) & |
| 256 |
", " & Real'Image (X) & ")"); |
| 257 |
when others => |
| 258 |
Report.Failed ("exception in Identity_Test" & |
| 259 |
" for X=(" & Real'Image (X) & |
| 260 |
", " & Real'Image (X) & ")"); |
| 261 |
end Identity_Test; |
| 262 |
|
| 263 |
|
| 264 |
procedure Exception_Test is |
| 265 |
-- Check that log((0,0)) causes constraint_error. |
| 266 |
-- G.1.2(29); |
| 267 |
|
| 268 |
X : Complex := (0.0, 0.0); |
| 269 |
begin |
| 270 |
if not Real'Machine_Overflows then |
| 271 |
-- not applicable: G.1.2(28);6.0 |
| 272 |
return; |
| 273 |
end if; |
| 274 |
|
| 275 |
begin |
| 276 |
X := Log ((0.0, 0.0)); |
| 277 |
Report.Failed ("exception not raised for log(0,0)"); |
| 278 |
exception |
| 279 |
when Constraint_Error => null; -- ok |
| 280 |
when others => |
| 281 |
Report.Failed ("wrong exception raised for log(0,0)"); |
| 282 |
end; |
| 283 |
|
| 284 |
-- optimizer thwarting |
| 285 |
if Report.Ident_Bool(False) then |
| 286 |
Report.Comment (Real'Image (X.Re + X.Im)); |
| 287 |
end if; |
| 288 |
end Exception_Test; |
| 289 |
|
| 290 |
|
| 291 |
procedure Do_Test is |
| 292 |
begin |
| 293 |
Special_Value_Test; |
| 294 |
Exact_Result_Test; |
| 295 |
-- test regions that do not include the unit circle so that |
| 296 |
-- the real part of LOG(Z) does not vanish |
| 297 |
-- See Cody page 9. |
| 298 |
Identity_Test ( 2.0, 10.0, 0.0, 10.0); |
| 299 |
Identity_Test (1000.0, 2000.0, -4000.0, -1000.0); |
| 300 |
Identity_Test (Real'Model_Epsilon, 0.25, |
| 301 |
-0.25, -Real'Model_Epsilon); |
| 302 |
Exception_Test; |
| 303 |
end Do_Test; |
| 304 |
end Generic_Check; |
| 305 |
|
| 306 |
----------------------------------------------------------------------- |
| 307 |
----------------------------------------------------------------------- |
| 308 |
package Float_Check is new Generic_Check (Float); |
| 309 |
|
| 310 |
-- check the floating point type with the most digits |
| 311 |
type A_Long_Float is digits System.Max_Digits; |
| 312 |
package A_Long_Float_Check is new Generic_Check (A_Long_Float); |
| 313 |
|
| 314 |
----------------------------------------------------------------------- |
| 315 |
----------------------------------------------------------------------- |
| 316 |
|
| 317 |
|
| 318 |
begin |
| 319 |
Report.Test ("CXG2019", |
| 320 |
"Check the accuracy of the complex LOG function"); |
| 321 |
|
| 322 |
if Verbose then |
| 323 |
Report.Comment ("checking Standard.Float"); |
| 324 |
end if; |
| 325 |
|
| 326 |
Float_Check.Do_Test; |
| 327 |
|
| 328 |
if Verbose then |
| 329 |
Report.Comment ("checking a digits" & |
| 330 |
Integer'Image (System.Max_Digits) & |
| 331 |
" floating point type"); |
| 332 |
end if; |
| 333 |
|
| 334 |
A_Long_Float_Check.Do_Test; |
| 335 |
|
| 336 |
|
| 337 |
Report.Result; |
| 338 |
end CXG2019; |