| 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 atan2 returns correct values for a subset of values. |
| 10 | ** Tests with positive and negative values of x and y to ensure |
| 11 | ** atan2 is returning results from the correct quadrant. |
| 12 | ** |
| 13 | **===================================================================*/ |
| 14 | |
| 15 | #include <palsuite.h> |
| 16 | |
| 17 | // binary64 (double) has a machine epsilon of 2^-52 (approx. 2.22e-16). However, this |
| 18 | // is slightly too accurate when writing tests meant to run against libm implementations |
| 19 | // for various platforms. 2^-50 (approx. 8.88e-16) 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 (15-17 digits). |
| 24 | |
| 25 | // For example, a test with an expect result in the format of 0.xxxxxxxxxxxxxxxxx will use |
| 26 | // PAL_EPSILON for the variance, while an expected result in the format of 0.0xxxxxxxxxxxxxxxxx |
| 27 | // will use PAL_EPSILON / 10 and and expected result in the format of x.xxxxxxxxxxxxxxxx will |
| 28 | // use PAL_EPSILON * 10. |
| 29 | #define PAL_EPSILON 8.8817841970012523e-16 |
| 30 | |
| 31 | #define PAL_NAN sqrt(-1.0) |
| 32 | #define PAL_POSINF -log(0.0) |
| 33 | #define PAL_NEGINF log(0.0) |
| 34 | |
| 35 | /** |
| 36 | * Helper test structure |
| 37 | */ |
| 38 | struct test |
| 39 | { |
| 40 | double x; /* first component of the value to test the function with */ |
| 41 | double y; /* second component of the value to test the function with */ |
| 42 | double expected; /* expected result */ |
| 43 | double variance; /* maximum delta between the expected and actual result */ |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * validate |
| 48 | * |
| 49 | * test validation function |
| 50 | */ |
| 51 | void __cdecl validate(double x, double y, double expected, double variance) |
| 52 | { |
| 53 | double result = pow(x, y); |
| 54 | |
| 55 | /* |
| 56 | * The test is valid when the difference between result |
| 57 | * and expected is less than or equal to variance |
| 58 | */ |
| 59 | double delta = fabs(result - expected); |
| 60 | |
| 61 | if (delta > variance) |
| 62 | { |
| 63 | Fail("pow(%g, %g) returned %20.17g when it should have returned %20.17g" , |
| 64 | x, y, result, expected); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * validate |
| 70 | * |
| 71 | * test validation function for values returning NaN |
| 72 | */ |
| 73 | void __cdecl validate_isnan(double x, double y) |
| 74 | { |
| 75 | double result = pow(x, y); |
| 76 | |
| 77 | if (!_isnan(result)) |
| 78 | { |
| 79 | Fail("pow(%g, %g) returned %20.17g when it should have returned %20.17g" , |
| 80 | x, y, result, PAL_NAN); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * main |
| 86 | * |
| 87 | * executable entry point |
| 88 | */ |
| 89 | int __cdecl main(int argc, char **argv) |
| 90 | { |
| 91 | struct test tests[] = |
| 92 | { |
| 93 | /* x y expected variance */ |
| 94 | { PAL_NEGINF, PAL_NEGINF, 0, PAL_EPSILON }, |
| 95 | { PAL_NEGINF, PAL_POSINF, PAL_POSINF, 0 }, |
| 96 | |
| 97 | { -10, PAL_NEGINF, 0, PAL_EPSILON }, |
| 98 | { -10, -1, -0.1, PAL_EPSILON }, |
| 99 | { -10, 0, 1, PAL_EPSILON * 10 }, |
| 100 | { -10, 1, -10, PAL_EPSILON * 100 }, |
| 101 | { -10, PAL_POSINF, PAL_POSINF, 0 }, |
| 102 | |
| 103 | { -2.7182818284590452, PAL_NEGINF, 0, PAL_EPSILON }, // x: -(e) |
| 104 | { -2.7182818284590452, -1, -0.36787944117144232, PAL_EPSILON }, // x: -(e) |
| 105 | { -2.7182818284590452, 0, 1, PAL_EPSILON * 10 }, // x: -(e) |
| 106 | { -2.7182818284590452, 1, -2.7182818284590452, PAL_EPSILON * 10 }, // x: -(e) expected: e |
| 107 | { -2.7182818284590452, PAL_POSINF, PAL_POSINF, 0 }, // x: -(e) |
| 108 | |
| 109 | { -1.0, PAL_NEGINF, 1.0, PAL_EPSILON * 10 }, |
| 110 | { -1.0, PAL_POSINF, 1.0, PAL_EPSILON * 10 }, |
| 111 | |
| 112 | { -0.0, PAL_NEGINF, PAL_POSINF, 0 }, |
| 113 | { -0.0, -1, PAL_NEGINF, 0 }, |
| 114 | { -0.0, -0.0, 1, PAL_EPSILON * 10 }, |
| 115 | { -0.0, 0, 1, PAL_EPSILON * 10 }, |
| 116 | { -0.0, 1, -0.0, PAL_EPSILON }, |
| 117 | { -0.0, PAL_POSINF, 0, PAL_EPSILON }, |
| 118 | |
| 119 | { PAL_NAN, -0.0, 1.0, PAL_EPSILON * 10 }, |
| 120 | { PAL_NAN, 0, 1.0, PAL_EPSILON * 10 }, |
| 121 | |
| 122 | { 0.0, PAL_NEGINF, PAL_POSINF, 0 }, |
| 123 | { 0.0, -1, PAL_POSINF, 0 }, |
| 124 | { 0, -0.0, 1, PAL_EPSILON * 10 }, |
| 125 | { 0, 0, 1, PAL_EPSILON * 10 }, |
| 126 | { 0.0, 1, 0, PAL_EPSILON }, |
| 127 | { 0.0, PAL_POSINF, 0, PAL_EPSILON }, |
| 128 | |
| 129 | { 1, PAL_NEGINF, 1, PAL_EPSILON * 10 }, |
| 130 | { 1, PAL_POSINF, 1, PAL_EPSILON * 10 }, |
| 131 | |
| 132 | { 2.7182818284590452, PAL_NEGINF, 0, PAL_EPSILON }, |
| 133 | { 2.7182818284590452, -3.1415926535897932, 0.043213918263772250, PAL_EPSILON / 10 }, // x: e y: -(pi) |
| 134 | { 2.7182818284590452, -2.7182818284590452, 0.065988035845312537, PAL_EPSILON / 10 }, // x: e y: -(e) |
| 135 | { 2.7182818284590452, -2.3025850929940457, 0.1, PAL_EPSILON }, // x: e y: -(ln(10)) |
| 136 | { 2.7182818284590452, -1.5707963267948966, 0.20787957635076191, PAL_EPSILON }, // x: e y: -(pi / 2) |
| 137 | { 2.7182818284590452, -1.4426950408889634, 0.23629008834452270, PAL_EPSILON }, // x: e y: -(log2(e)) |
| 138 | { 2.7182818284590452, -1.4142135623730950, 0.24311673443421421, PAL_EPSILON }, // x: e y: -(sqrt(2)) |
| 139 | { 2.7182818284590452, -1.1283791670955126, 0.32355726390307110, PAL_EPSILON }, // x: e y: -(2 / sqrt(pi)) |
| 140 | { 2.7182818284590452, -1, 0.36787944117144232, PAL_EPSILON }, // x: e y: -(1) |
| 141 | { 2.7182818284590452, -0.78539816339744831, 0.45593812776599624, PAL_EPSILON }, // x: e y: -(pi / 4) |
| 142 | { 2.7182818284590452, -0.70710678118654752, 0.49306869139523979, PAL_EPSILON }, // x: e y: -(1 / sqrt(2)) |
| 143 | { 2.7182818284590452, -0.69314718055994531, 0.5, PAL_EPSILON }, // x: e y: -(ln(2)) |
| 144 | { 2.7182818284590452, -0.63661977236758134, 0.52907780826773535, PAL_EPSILON }, // x: e y: -(2 / pi) |
| 145 | { 2.7182818284590452, -0.43429448190325183, 0.64772148514180065, PAL_EPSILON }, // x: e y: -(log10(e)) |
| 146 | { 2.7182818284590452, -0.31830988618379067, 0.72737734929521647, PAL_EPSILON }, // x: e y: -(1 / pi) |
| 147 | { 2.7182818284590452, 0, 1, PAL_EPSILON * 10 }, // x: e |
| 148 | { 2.7182818284590452, 0.31830988618379067, 1.3748022274393586, PAL_EPSILON * 10 }, // x: e y: 1 / pi |
| 149 | { 2.7182818284590452, 0.43429448190325183, 1.5438734439711811, PAL_EPSILON * 10 }, // x: e y: log10(e) |
| 150 | { 2.7182818284590452, 0.63661977236758134, 1.8900811645722220, PAL_EPSILON * 10 }, // x: e y: 2 / pi |
| 151 | { 2.7182818284590452, 0.69314718055994531, 2, PAL_EPSILON * 10 }, // x: e y: ln(2) |
| 152 | { 2.7182818284590452, 0.70710678118654752, 2.0281149816474725, PAL_EPSILON * 10 }, // x: e y: 1 / sqrt(2) |
| 153 | { 2.7182818284590452, 0.78539816339744831, 2.1932800507380155, PAL_EPSILON * 10 }, // x: e y: pi / 4 |
| 154 | { 2.7182818284590452, 1, 2.7182818284590452, PAL_EPSILON * 10 }, // x: e expected: e |
| 155 | { 2.7182818284590452, 1.1283791670955126, 3.0906430223107976, PAL_EPSILON * 10 }, // x: e y: 2 / sqrt(pi) |
| 156 | { 2.7182818284590452, 1.4142135623730950, 4.1132503787829275, PAL_EPSILON * 10 }, // x: e y: sqrt(2) |
| 157 | { 2.7182818284590452, 1.4426950408889634, 4.2320861065570819, PAL_EPSILON * 10 }, // x: e y: log2(e) |
| 158 | { 2.7182818284590452, 1.5707963267948966, 4.8104773809653517, PAL_EPSILON * 10 }, // x: e y: pi / 2 |
| 159 | { 2.7182818284590452, 2.3025850929940457, 10, PAL_EPSILON * 100 }, // x: e y: ln(10) |
| 160 | { 2.7182818284590452, 2.7182818284590452, 15.154262241479264, PAL_EPSILON * 100 }, // x: e y: e |
| 161 | { 2.7182818284590452, 3.1415926535897932, 23.140692632779269, PAL_EPSILON * 100 }, // x: e y: pi |
| 162 | { 2.7182818284590452, PAL_POSINF, PAL_POSINF, 0 }, // x: e |
| 163 | |
| 164 | { 10, PAL_NEGINF, 0, 0 }, |
| 165 | { 10, -3.1415926535897932, 0.00072178415907472774, PAL_EPSILON / 1000 }, // y: -(pi) |
| 166 | { 10, -2.7182818284590452, 0.0019130141022243176, PAL_EPSILON / 100 }, // y: -(e) |
| 167 | { 10, -2.3025850929940457, 0.0049821282964407206, PAL_EPSILON / 100 }, // y: -(ln(10)) |
| 168 | { 10, -1.5707963267948966, 0.026866041001136132, PAL_EPSILON / 10 }, // y: -(pi / 2) |
| 169 | { 10, -1.4426950408889634, 0.036083192820787210, PAL_EPSILON / 10 }, // y: -(log2(e)) |
| 170 | { 10, -1.4142135623730950, 0.038528884700322026, PAL_EPSILON / 10 }, // y: -(sqrt(2)) |
| 171 | { 10, -1.1283791670955126, 0.074408205860642723, PAL_EPSILON / 10 }, // y: -(2 / sqrt(pi)) |
| 172 | { 10, -1, 0.1, PAL_EPSILON }, // y: -(1) |
| 173 | { 10, -0.78539816339744831, 0.16390863613957665, PAL_EPSILON }, // y: -(pi / 4) |
| 174 | { 10, -0.70710678118654752, 0.19628775993505562, PAL_EPSILON }, // y: -(1 / sqrt(2)) |
| 175 | { 10, -0.69314718055994531, 0.20269956628651730, PAL_EPSILON }, // y: -(ln(2)) |
| 176 | { 10, -0.63661977236758134, 0.23087676451600055, PAL_EPSILON }, // y: -(2 / pi) |
| 177 | { 10, -0.43429448190325183, 0.36787944117144232, PAL_EPSILON }, // y: -(log10(e)) |
| 178 | { 10, -0.31830988618379067, 0.48049637305186868, PAL_EPSILON }, // y: -(1 / pi) |
| 179 | { 10, 0, 1, PAL_EPSILON * 10 }, |
| 180 | { 10, 0.31830988618379067, 2.0811811619898573, PAL_EPSILON * 10 }, // y: 1 / pi |
| 181 | { 10, 0.43429448190325183, 2.7182818284590452, PAL_EPSILON * 10 }, // y: log10(e) expected: e |
| 182 | { 10, 0.63661977236758134, 4.3313150290214525, PAL_EPSILON * 10 }, // y: 2 / pi |
| 183 | { 10, 0.69314718055994531, 4.9334096679145963, PAL_EPSILON * 10 }, // y: ln(2) |
| 184 | { 10, 0.70710678118654752, 5.0945611704512962, PAL_EPSILON * 10 }, // y: 1 / sqrt(2) |
| 185 | { 10, 0.78539816339744831, 6.1009598002416937, PAL_EPSILON * 10 }, // y: pi / 4 |
| 186 | { 10, 1, 10, PAL_EPSILON * 100 }, |
| 187 | { 10, 1.1283791670955126, 13.439377934644400, PAL_EPSILON * 100 }, // y: 2 / sqrt(pi) |
| 188 | { 10, 1.4142135623730950, 25.954553519470081, PAL_EPSILON * 100 }, // y: sqrt(2) |
| 189 | { 10, 1.4426950408889634, 27.713733786437790, PAL_EPSILON * 100 }, // y: log2(e) |
| 190 | { 10, 1.5707963267948966, 37.221710484165167, PAL_EPSILON * 100 }, // y: pi / 2 |
| 191 | { 10, 2.3025850929940457, 200.71743249053009, PAL_EPSILON * 1000 }, // y: ln(10) |
| 192 | { 10, 2.7182818284590452, 522.73529967043665, PAL_EPSILON * 1000 }, // y: e |
| 193 | { 10, 3.1415926535897932, 1385.4557313670111, PAL_EPSILON * 10000 }, // y: pi |
| 194 | { 10, PAL_POSINF, PAL_POSINF, 0 }, |
| 195 | |
| 196 | { PAL_POSINF, PAL_NEGINF, 0, PAL_EPSILON }, |
| 197 | { PAL_POSINF, PAL_POSINF, PAL_POSINF, 0 }, |
| 198 | }; |
| 199 | |
| 200 | if (PAL_Initialize(argc, argv) != 0) |
| 201 | { |
| 202 | return FAIL; |
| 203 | } |
| 204 | |
| 205 | for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++) |
| 206 | { |
| 207 | validate(tests[i].x, tests[i].y, tests[i].expected, tests[i].variance); |
| 208 | } |
| 209 | |
| 210 | validate_isnan(-10, -1.5707963267948966); // y: -(pi / 2) |
| 211 | validate_isnan(-10, -0.78539816339744828); // y: -(pi / 4) |
| 212 | validate_isnan(-10, 0.78539816339744828); // y: pi / 4 |
| 213 | validate_isnan(-10, 1.5707963267948966); // y: pi / 2 |
| 214 | |
| 215 | validate_isnan(-2.7182818284590452, -1.5707963267948966); // x: -(e) y: -(pi / 2) |
| 216 | validate_isnan(-2.7182818284590452, -0.78539816339744828); // x: -(e) y: -(pi / 4) |
| 217 | validate_isnan(-2.7182818284590452, 0.78539816339744828); // x: -(e) y: pi / 4 |
| 218 | validate_isnan(-2.7182818284590452, 1.5707963267948966); // x: -(e) y: pi / 2 |
| 219 | |
| 220 | validate_isnan(PAL_NEGINF, PAL_NAN); |
| 221 | validate_isnan(PAL_NAN, PAL_NEGINF); |
| 222 | |
| 223 | validate_isnan(PAL_POSINF, PAL_NAN); |
| 224 | validate_isnan(PAL_NAN, PAL_POSINF); |
| 225 | |
| 226 | validate_isnan(PAL_NAN, PAL_NAN); |
| 227 | |
| 228 | PAL_Terminate(); |
| 229 | return PASS; |
| 230 | } |
| 231 | |