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
35struct test
36{
37 double y; /* second component of the value to test the function with */
38 double x; /* first component of the value to test the function with */
39 double expected; /* expected result */
40 double variance; /* maximum delta between the expected and actual result */
41};
42
43/**
44 * validate
45 *
46 * test validation function
47 */
48void __cdecl validate(double y, double x, double expected, double variance)
49{
50 double result = atan2(y, x);
51
52 /*
53 * The test is valid when the difference between result
54 * and expected is less than or equal to variance
55 */
56 double delta = fabs(result - expected);
57
58 if (delta > variance)
59 {
60 Fail("atan2(%g, %g) returned %20.17g when it should have returned %20.17g",
61 y, x, result, expected);
62 }
63}
64
65/**
66 * validate
67 *
68 * test validation function for values returning NaN
69 */
70void __cdecl validate_isnan(double y, double x)
71{
72 double result = atan2(y, x);
73
74 if (!_isnan(result))
75 {
76 Fail("atan2(%g, %g) returned %20.17g when it should have returned %20.17g",
77 y, x, result, PAL_NAN);
78 }
79}
80
81/**
82 * main
83 *
84 * executable entry point
85 */
86int __cdecl main(int argc, char **argv)
87{
88 struct test tests[] =
89 {
90 /* y x expected variance */
91 { 0, PAL_POSINF, 0, PAL_EPSILON },
92 { 0, 0, 0, PAL_EPSILON },
93 { 0.31296179620778659, 0.94976571538163866, 0.31830988618379067, PAL_EPSILON }, // expected: 1 / pi
94 { 0.42077048331375735, 0.90716712923909839, 0.43429448190325183, PAL_EPSILON }, // expected: log10(e)
95 { 0.59448076852482208, 0.80410982822879171, 0.63661977236758134, PAL_EPSILON }, // expected: 2 / pi
96 { 0.63896127631363480, 0.76923890136397213, 0.69314718055994531, PAL_EPSILON }, // expected: ln(2)
97 { 0.64963693908006244, 0.76024459707563015, 0.70710678118654752, PAL_EPSILON }, // expected: 1 / sqrt(2)
98 { 0.70710678118654752, 0.70710678118654752, 0.78539816339744831, PAL_EPSILON }, // expected: pi / 4, value: 1 / sqrt(2)
99 { 1, 1, 0.78539816339744831, PAL_EPSILON }, // expected: pi / 4
100 { PAL_POSINF, PAL_POSINF, 0.78539816339744831, PAL_EPSILON }, // expected: pi / 4
101 { 0.84147098480789651, 0.54030230586813972, 1, PAL_EPSILON * 10 },
102 { 0.90371945743584630, 0.42812514788535792, 1.1283791670955126, PAL_EPSILON * 10 }, // expected: 2 / sqrt(pi)
103 { 0.98776594599273553, 0.15594369476537447, 1.4142135623730950, PAL_EPSILON * 10 }, // expected: sqrt(2)
104 { 0.99180624439366372, 0.12775121753523991, 1.4426950408889634, PAL_EPSILON * 10 }, // expected: log2(e)
105 { 1, 0, 1.5707963267948966, PAL_EPSILON * 10 }, // expected: pi / 2
106 { PAL_POSINF, 0, 1.5707963267948966, PAL_EPSILON * 10 }, // expected: pi / 2
107 { PAL_POSINF, 1, 1.5707963267948966, PAL_EPSILON * 10 }, // expected: pi / 2
108 { 0.74398033695749319, -0.66820151019031295, 2.3025850929940457, PAL_EPSILON * 10 }, // expected: ln(10)
109 { 0.41078129050290870, -0.91173391478696510, 2.7182818284590452, PAL_EPSILON * 10 }, // expected: e
110 { 0, -1, 3.1415926535897932, PAL_EPSILON * 10 }, // expected: pi
111 { 1, PAL_POSINF, 0, PAL_EPSILON },
112 };
113
114 if (PAL_Initialize(argc, argv) != 0)
115 {
116 return FAIL;
117 }
118
119 for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++)
120 {
121 const double pi = 3.1415926535897932;
122
123 validate( tests[i].y, tests[i].x, tests[i].expected, tests[i].variance);
124 validate(-tests[i].y, tests[i].x, -tests[i].expected, tests[i].variance);
125 validate( tests[i].y, -tests[i].x, pi - tests[i].expected, tests[i].variance);
126 validate(-tests[i].y, -tests[i].x, tests[i].expected - pi, tests[i].variance);
127 }
128
129 validate_isnan(PAL_NEGINF, PAL_NAN);
130 validate_isnan(PAL_NAN, PAL_NEGINF);
131 validate_isnan(PAL_NAN, PAL_POSINF);
132 validate_isnan(PAL_POSINF, PAL_NAN);
133
134 validate_isnan(PAL_NAN, -1);
135 validate_isnan(PAL_NAN, -0.0);
136 validate_isnan(PAL_NAN, 0);
137 validate_isnan(PAL_NAN, 1);
138
139 validate_isnan(-1, PAL_NAN);
140 validate_isnan(-0.0, PAL_NAN);
141 validate_isnan( 0, PAL_NAN);
142 validate_isnan( 1, PAL_NAN);
143
144 validate_isnan(PAL_NAN, PAL_NAN);
145
146 PAL_Terminate();
147 return PASS;
148}
149