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 | struct test |
35 | { |
36 | float y; /* second component of the value to test the function with */ |
37 | float x; /* first component of the value to test the function with */ |
38 | float expected; /* expected result */ |
39 | float variance; /* maximum delta between the expected and actual result */ |
40 | }; |
41 | |
42 | /** |
43 | * validate |
44 | * |
45 | * test validation function |
46 | */ |
47 | void __cdecl validate(float y, float x, float expected, float variance) |
48 | { |
49 | float result = atan2f(y, x); |
50 | |
51 | /* |
52 | * The test is valid when the difference between result |
53 | * and expected is less than or equal to variance |
54 | */ |
55 | float delta = fabsf(result - expected); |
56 | |
57 | if (delta > variance) |
58 | { |
59 | Fail("atan2f(%g, %g) returned %10.9g when it should have returned %10.9g" , |
60 | y, x, result, expected); |
61 | } |
62 | } |
63 | |
64 | /** |
65 | * validate |
66 | * |
67 | * test validation function for values returning NaN |
68 | */ |
69 | void __cdecl validate_isnan(float y, float x) |
70 | { |
71 | float result = atan2f(y, x); |
72 | |
73 | if (!_isnanf(result)) |
74 | { |
75 | Fail("atan2f(%g, %g) returned %10.9g when it should have returned %10.9g" , |
76 | y, x, result, PAL_NAN); |
77 | } |
78 | } |
79 | |
80 | /** |
81 | * main |
82 | * |
83 | * executable entry point |
84 | */ |
85 | int __cdecl main(int argc, char **argv) |
86 | { |
87 | struct test tests[] = |
88 | { |
89 | /* y x expected variance */ |
90 | { 0, PAL_POSINF, 0, PAL_EPSILON }, |
91 | { 0, 0, 0, PAL_EPSILON }, |
92 | { 0.312961796f, 0.949765715f, 0.318309886f, PAL_EPSILON }, // expected: 1 / pi |
93 | { 0.420770483f, 0.907167129f, 0.434294482f, PAL_EPSILON }, // expected: logf10f(e) |
94 | { 0.594480769f, 0.804109828f, 0.636619772f, PAL_EPSILON }, // expected: 2 / pi |
95 | { 0.638961276f, 0.769238901f, 0.693147181f, PAL_EPSILON }, // expected: ln(2) |
96 | { 0.649636939f, 0.760244597f, 0.707106781f, PAL_EPSILON }, // expected: 1 / sqrtf(2) |
97 | { 0.707106781f, 0.707106781f, 0.785398163f, PAL_EPSILON }, // expected: pi / 4, value: 1 / sqrtf(2) |
98 | { 1, 1, 0.785398163f, PAL_EPSILON }, // expected: pi / 4 |
99 | { PAL_POSINF, PAL_POSINF, 0.785398163f, PAL_EPSILON }, // expected: pi / 4 |
100 | { 0.841470985f, 0.540302306f, 1, PAL_EPSILON * 10 }, |
101 | { 0.903719457f, 0.428125148f, 1.12837917f, PAL_EPSILON * 10 }, // expected: 2 / sqrtf(pi) |
102 | { 0.987765946f, 0.155943695f, 1.41421356f, PAL_EPSILON * 10 }, // expected: sqrtf(2) |
103 | { 0.991806244f, 0.127751218f, 1.44269504f, PAL_EPSILON * 10 }, // expected: logf2(e) |
104 | { 1, 0, 1.57079633f, PAL_EPSILON * 10 }, // expected: pi / 2 |
105 | { PAL_POSINF, 0, 1.57079633f, PAL_EPSILON * 10 }, // expected: pi / 2 |
106 | { PAL_POSINF, 1, 1.57079633f, PAL_EPSILON * 10 }, // expected: pi / 2 |
107 | { 0.743980337f, -0.668201510f, 2.30258509f, PAL_EPSILON * 10 }, // expected: ln(10) |
108 | { 0.410781291f, -0.911733915f, 2.71828183f, PAL_EPSILON * 10 }, // expected: e |
109 | { 0, -1, 3.14159265f, PAL_EPSILON * 10 }, // expected: pi |
110 | { 1, PAL_POSINF, 0, PAL_EPSILON }, |
111 | }; |
112 | |
113 | if (PAL_Initialize(argc, argv) != 0) |
114 | { |
115 | return FAIL; |
116 | } |
117 | |
118 | for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++) |
119 | { |
120 | const float pi = 3.14159265f; |
121 | |
122 | validate( tests[i].y, tests[i].x, tests[i].expected, tests[i].variance); |
123 | validate(-tests[i].y, tests[i].x, -tests[i].expected, tests[i].variance); |
124 | validate( tests[i].y, -tests[i].x, pi - tests[i].expected, tests[i].variance); |
125 | validate(-tests[i].y, -tests[i].x, tests[i].expected - pi, tests[i].variance); |
126 | } |
127 | |
128 | validate_isnan(PAL_NEGINF, PAL_NAN); |
129 | validate_isnan(PAL_NAN, PAL_NEGINF); |
130 | validate_isnan(PAL_NAN, PAL_POSINF); |
131 | validate_isnan(PAL_POSINF, PAL_NAN); |
132 | |
133 | validate_isnan(PAL_NAN, -1); |
134 | validate_isnan(PAL_NAN, -0.0f); |
135 | validate_isnan(PAL_NAN, 0); |
136 | validate_isnan(PAL_NAN, 1); |
137 | |
138 | validate_isnan(-1, PAL_NAN); |
139 | validate_isnan(-0.0f, PAL_NAN); |
140 | validate_isnan( 0, PAL_NAN); |
141 | validate_isnan( 1, PAL_NAN); |
142 | |
143 | validate_isnan(PAL_NAN, PAL_NAN); |
144 | |
145 | PAL_Terminate(); |
146 | return PASS; |
147 | } |
148 | |