1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | // See the LICENSE file in the project root for more information. |
4 | |
5 | /*===================================================================== |
6 | ** |
7 | ** Source: test1.c |
8 | ** |
9 | ** Purpose: Tests that atan2f returns correct values for a subset of values. |
10 | ** Tests with positive and negative values of x and y to ensure |
11 | ** atan2f is returning results from the correct quadrant. |
12 | ** |
13 | **===================================================================*/ |
14 | |
15 | #include <palsuite.h> |
16 | |
17 | // binary32 (float) has a machine epsilon of 2^-23 (approx. 1.19e-07). However, this |
18 | // is slightly too accurate when writing tests meant to run against libm implementations |
19 | // for various platforms. 2^-21 (approx. 4.76e-07) seems to be as accurate as we can get. |
20 | // |
21 | // The tests themselves will take PAL_EPSILON and adjust it according to the expected result |
22 | // so that the delta used for comparison will compare the most significant digits and ignore |
23 | // any digits that are outside the double precision range (6-9 digits). |
24 | |
25 | // For example, a test with an expect result in the format of 0.xxxxxxxxx will use PAL_EPSILON |
26 | // for the variance, while an expected result in the format of 0.0xxxxxxxxx will use |
27 | // PAL_EPSILON / 10 and and expected result in the format of x.xxxxxx will use PAL_EPSILON * 10. |
28 | #define PAL_EPSILON 4.76837158e-07 |
29 | |
30 | #define PAL_NAN sqrtf(-1.0f) |
31 | #define PAL_POSINF -logf(0.0f) |
32 | #define PAL_NEGINF logf(0.0f) |
33 | |
34 | /** |
35 | * Helper test structure |
36 | */ |
37 | struct test |
38 | { |
39 | float x; /* first component of the value to test the function with */ |
40 | float y; /* second component of the value to test the function with */ |
41 | float expected; /* expected result */ |
42 | float variance; /* maximum delta between the expected and actual result */ |
43 | }; |
44 | |
45 | /** |
46 | * validate |
47 | * |
48 | * test validation function |
49 | */ |
50 | void __cdecl validate(float x, float y, float expected, float variance) |
51 | { |
52 | float result = powf(x, y); |
53 | |
54 | /* |
55 | * The test is valid when the difference between result |
56 | * and expected is less than or equal to variance |
57 | */ |
58 | float delta = fabsf(result - expected); |
59 | |
60 | if (delta > variance) |
61 | { |
62 | Fail("powf(%g, %g) returned %10.9g when it should have returned %10.9g" , |
63 | x, y, result, expected); |
64 | } |
65 | } |
66 | |
67 | /** |
68 | * validate |
69 | * |
70 | * test validation function for values returning NaN |
71 | */ |
72 | void __cdecl validate_isnan(float x, float y) |
73 | { |
74 | float result = powf(x, y); |
75 | |
76 | if (!_isnanf(result)) |
77 | { |
78 | Fail("powf(%g, %g) returned %10.9g when it should have returned %10.9g" , |
79 | x, y, result, PAL_NAN); |
80 | } |
81 | } |
82 | |
83 | /** |
84 | * main |
85 | * |
86 | * executable entry point |
87 | */ |
88 | int __cdecl main(int argc, char **argv) |
89 | { |
90 | struct test tests[] = |
91 | { |
92 | /* x y expected variance */ |
93 | { PAL_NEGINF, PAL_NEGINF, 0, PAL_EPSILON }, |
94 | { PAL_NEGINF, PAL_POSINF, PAL_POSINF, 0 }, |
95 | |
96 | { -10, PAL_NEGINF, 0, PAL_EPSILON }, |
97 | { -10, -1, -0.1f, PAL_EPSILON }, |
98 | { -10, 0, 1, PAL_EPSILON * 10 }, |
99 | { -10, 1, -10, PAL_EPSILON * 100 }, |
100 | { -10, PAL_POSINF, PAL_POSINF, 0 }, |
101 | |
102 | { -2.71828183f, PAL_NEGINF, 0, PAL_EPSILON }, // x: -(e) |
103 | { -2.71828183f, -1, -0.367879441f, PAL_EPSILON }, // x: -(e) |
104 | { -2.71828183f, 0, 1, PAL_EPSILON * 10 }, // x: -(e) |
105 | { -2.71828183f, 1, -2.71828183f, PAL_EPSILON * 10 }, // x: -(e) expected: e |
106 | { -2.71828183f, PAL_POSINF, PAL_POSINF, 0 }, // x: -(e) |
107 | |
108 | { -1.0, PAL_NEGINF, 1.0, PAL_EPSILON * 10 }, |
109 | { -1.0, PAL_POSINF, 1.0, PAL_EPSILON * 10 }, |
110 | |
111 | { -0.0, PAL_NEGINF, PAL_POSINF, 0 }, |
112 | { -0.0, -1, PAL_NEGINF, 0 }, |
113 | { -0.0f, -0.0f, 1, PAL_EPSILON * 10 }, |
114 | { -0.0f, 0, 1, PAL_EPSILON * 10 }, |
115 | { -0.0, 1, -0.0, PAL_EPSILON }, |
116 | { -0.0, PAL_POSINF, 0, PAL_EPSILON }, |
117 | |
118 | { PAL_NAN, -0.0, 1.0, PAL_EPSILON * 10 }, |
119 | { PAL_NAN, 0, 1.0, PAL_EPSILON * 10 }, |
120 | |
121 | { 0.0, PAL_NEGINF, PAL_POSINF, 0 }, |
122 | { 0.0, -1, PAL_POSINF, 0 }, |
123 | { 0, -0.0f, 1, PAL_EPSILON * 10 }, |
124 | { 0, 0, 1, PAL_EPSILON * 10 }, |
125 | { 0.0, 1, 0, PAL_EPSILON }, |
126 | { 0.0, PAL_POSINF, 0, PAL_EPSILON }, |
127 | |
128 | { 1, PAL_NEGINF, 1, PAL_EPSILON * 10 }, |
129 | { 1, PAL_POSINF, 1, PAL_EPSILON * 10 }, |
130 | |
131 | { 2.71828183f, PAL_NEGINF, 0, PAL_EPSILON }, |
132 | { 2.71828183f, -3.14159265f, 0.0432139183f, PAL_EPSILON / 10 }, // x: e y: -(pi) |
133 | { 2.71828183f, -2.71828183f, 0.0659880358f, PAL_EPSILON / 10 }, // x: e y: -(e) |
134 | { 2.71828183f, -2.30258509f, 0.1f, PAL_EPSILON }, // x: e y: -(ln(10)) |
135 | { 2.71828183f, -1.57079633f, 0.207879576f, PAL_EPSILON }, // x: e y: -(pi / 2) |
136 | { 2.71828183f, -1.44269504f, 0.236290088f, PAL_EPSILON }, // x: e y: -(logf2(e)) |
137 | { 2.71828183f, -1.41421356f, 0.243116734f, PAL_EPSILON }, // x: e y: -(sqrtf(2)) |
138 | { 2.71828183f, -1.12837917f, 0.323557264f, PAL_EPSILON }, // x: e y: -(2 / sqrtf(pi)) |
139 | { 2.71828183f, -1, 0.367879441f, PAL_EPSILON }, // x: e y: -(1) |
140 | { 2.71828183f, -0.785398163f, 0.455938128f, PAL_EPSILON }, // x: e y: -(pi / 4) |
141 | { 2.71828183f, -0.707106781f, 0.493068691f, PAL_EPSILON }, // x: e y: -(1 / sqrtf(2)) |
142 | { 2.71828183f, -0.693147181f, 0.5f, PAL_EPSILON }, // x: e y: -(ln(2)) |
143 | { 2.71828183f, -0.636619772f, 0.529077808f, PAL_EPSILON }, // x: e y: -(2 / pi) |
144 | { 2.71828183f, -0.434294482f, 0.647721485f, PAL_EPSILON }, // x: e y: -(log10f(e)) |
145 | { 2.71828183f, -0.318309886f, 0.727377349f, PAL_EPSILON }, // x: e y: -(1 / pi) |
146 | { 2.71828183f, 0, 1, PAL_EPSILON * 10 }, // x: e |
147 | { 2.71828183f, 0.318309886f, 1.37480223f, PAL_EPSILON * 10 }, // x: e y: 1 / pi |
148 | { 2.71828183f, 0.434294482f, 1.54387344f, PAL_EPSILON * 10 }, // x: e y: log10f(e) |
149 | { 2.71828183f, 0.636619772f, 1.89008116f, PAL_EPSILON * 10 }, // x: e y: 2 / pi |
150 | { 2.71828183f, 0.693147181f, 2, PAL_EPSILON * 10 }, // x: e y: ln(2) |
151 | { 2.71828183f, 0.707106781f, 2.02811498f, PAL_EPSILON * 10 }, // x: e y: 1 / sqrtf(2) |
152 | { 2.71828183f, 0.785398163f, 2.19328005f, PAL_EPSILON * 10 }, // x: e y: pi / 4 |
153 | { 2.71828183f, 1, 2.71828183f, PAL_EPSILON * 10 }, // x: e expected: e |
154 | { 2.71828183f, 1.12837917f, 3.09064302f, PAL_EPSILON * 10 }, // x: e y: 2 / sqrtf(pi) |
155 | { 2.71828183f, 1.41421356f, 4.11325038f, PAL_EPSILON * 10 }, // x: e y: sqrtf(2) |
156 | { 2.71828183f, 1.44269504f, 4.23208611f, PAL_EPSILON * 10 }, // x: e y: logf2(e) |
157 | { 2.71828183f, 1.57079633f, 4.81047738f, PAL_EPSILON * 10 }, // x: e y: pi / 2 |
158 | { 2.71828183f, 2.30258509f, 10, PAL_EPSILON * 100 }, // x: e y: ln(10) |
159 | { 2.71828183f, 2.71828183f, 15.1542622f, PAL_EPSILON * 100 }, // x: e y: e |
160 | { 2.71828183f, 3.14159265f, 23.1406926f, PAL_EPSILON * 100 }, // x: e y: pi |
161 | { 2.71828183f, PAL_POSINF, PAL_POSINF, 0 }, // x: e |
162 | |
163 | { 10, PAL_NEGINF, 0, 0 }, |
164 | { 10, -3.14159265f, 0.000721784159f, PAL_EPSILON / 1000 }, // y: -(pi) |
165 | { 10, -2.71828183f, 0.00191301410f, PAL_EPSILON / 100 }, // y: -(e) |
166 | { 10, -2.30258509f, 0.00498212830f, PAL_EPSILON / 100 }, // y: -(ln(10)) |
167 | { 10, -1.57079633f, 0.0268660410f, PAL_EPSILON / 10 }, // y: -(pi / 2) |
168 | { 10, -1.44269504f, 0.0360831928f, PAL_EPSILON / 10 }, // y: -(logf2(e)) |
169 | { 10, -1.41421356f, 0.0385288847f, PAL_EPSILON / 10 }, // y: -(sqrtf(2)) |
170 | { 10, -1.12837917f, 0.0744082059f, PAL_EPSILON / 10 }, // y: -(2 / sqrtf(pi)) |
171 | { 10, -1, 0.1f, PAL_EPSILON }, // y: -(1) |
172 | { 10, -0.785398163f, 0.163908636f, PAL_EPSILON }, // y: -(pi / 4) |
173 | { 10, -0.707106781f, 0.196287760f, PAL_EPSILON }, // y: -(1 / sqrtf(2)) |
174 | { 10, -0.693147181f, 0.202699566f, PAL_EPSILON }, // y: -(ln(2)) |
175 | { 10, -0.636619772f, 0.230876765f, PAL_EPSILON }, // y: -(2 / pi) |
176 | { 10, -0.434294482f, 0.367879441f, PAL_EPSILON }, // y: -(log10f(e)) |
177 | { 10, -0.318309886f, 0.480496373f, PAL_EPSILON }, // y: -(1 / pi) |
178 | { 10, 0, 1, PAL_EPSILON * 10 }, |
179 | { 10, 0.318309886f, 2.08118116f, PAL_EPSILON * 10 }, // y: 1 / pi |
180 | { 10, 0.434294482f, 2.71828183f, PAL_EPSILON * 10 }, // y: log10f(e) expected: e |
181 | { 10, 0.636619772f, 4.33131503f, PAL_EPSILON * 10 }, // y: 2 / pi |
182 | { 10, 0.693147181f, 4.93340967f, PAL_EPSILON * 10 }, // y: ln(2) |
183 | { 10, 0.707106781f, 5.09456117f, PAL_EPSILON * 10 }, // y: 1 / sqrtf(2) |
184 | { 10, 0.785398163f, 6.10095980f, PAL_EPSILON * 10 }, // y: pi / 4 |
185 | { 10, 1, 10, PAL_EPSILON * 100 }, |
186 | { 10, 1.12837917f, 13.4393779f, PAL_EPSILON * 100 }, // y: 2 / sqrtf(pi) |
187 | { 10, 1.41421356f, 25.9545535f, PAL_EPSILON * 100 }, // y: sqrtf(2) |
188 | { 10, 1.44269504f, 27.7137338f, PAL_EPSILON * 100 }, // y: logf2(e) |
189 | { 10, 1.57079633f, 37.2217105f, PAL_EPSILON * 100 }, // y: pi / 2 |
190 | { 10, 2.30258509f, 200.717432f, PAL_EPSILON * 1000 }, // y: ln(10) |
191 | { 10, 2.71828183f, 522.735300f, PAL_EPSILON * 1000 }, // y: e |
192 | { 10, 3.14159265f, 1385.45573f, PAL_EPSILON * 10000 }, // y: pi |
193 | { 10, PAL_POSINF, PAL_POSINF, 0 }, |
194 | |
195 | { PAL_POSINF, PAL_NEGINF, 0, PAL_EPSILON }, |
196 | { PAL_POSINF, PAL_POSINF, PAL_POSINF, 0 }, |
197 | }; |
198 | |
199 | if (PAL_Initialize(argc, argv) != 0) |
200 | { |
201 | return FAIL; |
202 | } |
203 | |
204 | for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++) |
205 | { |
206 | validate(tests[i].x, tests[i].y, tests[i].expected, tests[i].variance); |
207 | } |
208 | |
209 | validate_isnan(-10, -1.57079633f); // y: -(pi / 2) |
210 | validate_isnan(-10, -0.785398163f); // y: -(pi / 4) |
211 | validate_isnan(-10, 0.785398163f); // y: pi / 4 |
212 | validate_isnan(-10, 1.57079633f); // y: pi / 2 |
213 | |
214 | validate_isnan(-2.71828183f, -1.57079633f); // x: -(e) y: -(pi / 2) |
215 | validate_isnan(-2.71828183f, -0.785398163f); // x: -(e) y: -(pi / 4) |
216 | validate_isnan(-2.71828183f, 0.785398163f); // x: -(e) y: pi / 4 |
217 | validate_isnan(-2.71828183f, 1.57079633f); // x: -(e) y: pi / 2 |
218 | |
219 | validate_isnan(PAL_NEGINF, PAL_NAN); |
220 | validate_isnan(PAL_NAN, PAL_NEGINF); |
221 | |
222 | validate_isnan(PAL_POSINF, PAL_NAN); |
223 | validate_isnan(PAL_NAN, PAL_POSINF); |
224 | |
225 | validate_isnan(PAL_NAN, PAL_NAN); |
226 | |
227 | PAL_Terminate(); |
228 | return PASS; |
229 | } |
230 | |