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: Test to ensure that log10 returns correct values. |
10 | ** |
11 | ** Dependencies: PAL_Initialize |
12 | ** PAL_Terminate |
13 | ** Fail |
14 | ** fabs |
15 | ** _isnan |
16 | ** |
17 | **===========================================================================*/ |
18 | |
19 | #include <palsuite.h> |
20 | |
21 | // binary64 (double) has a machine epsilon of 2^-52 (approx. 2.22e-16). However, this |
22 | // is slightly too accurate when writing tests meant to run against libm implementations |
23 | // for various platforms. 2^-50 (approx. 8.88e-16) seems to be as accurate as we can get. |
24 | // |
25 | // The tests themselves will take PAL_EPSILON and adjust it according to the expected result |
26 | // so that the delta used for comparison will compare the most significant digits and ignore |
27 | // any digits that are outside the double precision range (15-17 digits). |
28 | |
29 | // For example, a test with an expect result in the format of 0.xxxxxxxxxxxxxxxxx will use |
30 | // PAL_EPSILON for the variance, while an expected result in the format of 0.0xxxxxxxxxxxxxxxxx |
31 | // will use PAL_EPSILON / 10 and and expected result in the format of x.xxxxxxxxxxxxxxxx will |
32 | // use PAL_EPSILON * 10. |
33 | #define PAL_EPSILON 8.8817841970012523e-16 |
34 | |
35 | #define PAL_NAN sqrt(-1.0) |
36 | #define PAL_POSINF -log(0.0) |
37 | #define PAL_NEGINF log(0.0) |
38 | |
39 | /** |
40 | * Helper test structure |
41 | */ |
42 | struct test |
43 | { |
44 | double value; /* value to test the function with */ |
45 | double expected; /* expected result */ |
46 | double variance; /* maximum delta between the expected and actual result */ |
47 | }; |
48 | |
49 | /** |
50 | * validate |
51 | * |
52 | * test validation function |
53 | */ |
54 | void __cdecl validate(double value, double expected, double variance) |
55 | { |
56 | double result = log10(value); |
57 | |
58 | /* |
59 | * The test is valid when the difference between result |
60 | * and expected is less than or equal to variance |
61 | */ |
62 | double delta = fabs(result - expected); |
63 | |
64 | if (delta > variance) |
65 | { |
66 | Fail("log10(%g) returned %20.17g when it should have returned %20.17g" , |
67 | value, result, expected); |
68 | } |
69 | } |
70 | |
71 | /** |
72 | * validate |
73 | * |
74 | * test validation function for values returning NaN |
75 | */ |
76 | void __cdecl validate_isnan(double value) |
77 | { |
78 | double result = log10(value); |
79 | |
80 | if (!_isnan(result)) |
81 | { |
82 | Fail("log10(%g) returned %20.17g when it should have returned %20.17g" , |
83 | value, result, PAL_NAN); |
84 | } |
85 | } |
86 | |
87 | /** |
88 | * main |
89 | * |
90 | * executable entry point |
91 | */ |
92 | int __cdecl main(int argc, char **argv) |
93 | { |
94 | struct test tests[] = |
95 | { |
96 | /* value expected variance */ |
97 | { 0, PAL_NEGINF, 0 }, |
98 | { 0.00072178415907472774, -3.1415926535897932, PAL_EPSILON * 10 }, // expected: -(pi) |
99 | { 0.0019130141022243176, -2.7182818284590452, PAL_EPSILON * 10 }, // expected: -(e) |
100 | { 0.0049821282964407206, -2.3025850929940457, PAL_EPSILON * 10 }, // expected: -(ln(10)) |
101 | { 0.026866041001136132, -1.5707963267948966, PAL_EPSILON * 10 }, // expected: -(pi / 2) |
102 | { 0.036083192820787210, -1.4426950408889634, PAL_EPSILON * 10 }, // expected: -(log2(e)) |
103 | { 0.038528884700322026, -1.4142135623730950, PAL_EPSILON * 10 }, // expected: -(sqrt(2)) |
104 | { 0.074408205860642723, -1.1283791670955126, PAL_EPSILON * 10 }, // expected: -(2 / sqrt(pi)) |
105 | { 0.1, -1, PAL_EPSILON * 10 }, // expected: -(1) |
106 | { 0.16390863613957665, -0.78539816339744831, PAL_EPSILON }, // expected: -(pi / 4) |
107 | { 0.19628775993505562, -0.70710678118654752, PAL_EPSILON }, // expected: -(1 / sqrt(2)) |
108 | { 0.20269956628651730, -0.69314718055994531, PAL_EPSILON }, // expected: -(ln(2)) |
109 | { 0.23087676451600055, -0.63661977236758134, PAL_EPSILON }, // expected: -(2 / pi) |
110 | { 0.36787944117144232, -0.43429448190325183, PAL_EPSILON }, // expected: -(log10(e)) |
111 | { 0.48049637305186868, -0.31830988618379067, PAL_EPSILON }, // expected: -(1 / pi) |
112 | { 1, 0, PAL_EPSILON }, |
113 | { 2.0811811619898573, 0.31830988618379067, PAL_EPSILON }, // expected: 1 / pi |
114 | { 2.7182818284590452, 0.43429448190325183, PAL_EPSILON }, // expected: log10(e) value: e |
115 | { 4.3313150290214525, 0.63661977236758134, PAL_EPSILON }, // expected: 2 / pi |
116 | { 4.9334096679145963, 0.69314718055994531, PAL_EPSILON }, // expected: ln(2) |
117 | { 5.0945611704512962, 0.70710678118654752, PAL_EPSILON }, // expected: 1 / sqrt(2) |
118 | { 6.1009598002416937, 0.78539816339744831, PAL_EPSILON }, // expected: pi / 4 |
119 | { 10, 1, PAL_EPSILON * 10 }, |
120 | { 13.439377934644400, 1.1283791670955126, PAL_EPSILON * 10 }, // expected: 2 / sqrt(pi) |
121 | { 25.954553519470081, 1.4142135623730950, PAL_EPSILON * 10 }, // expected: sqrt(2) |
122 | { 27.713733786437790, 1.4426950408889634, PAL_EPSILON * 10 }, // expected: log2(e) |
123 | { 37.221710484165167, 1.5707963267948966, PAL_EPSILON * 10 }, // expected: pi / 2 |
124 | { 200.71743249053009, 2.3025850929940457, PAL_EPSILON * 10 }, // expected: ln(10) |
125 | { 522.73529967043665, 2.7182818284590452, PAL_EPSILON * 10 }, // expected: e |
126 | { 1385.4557313670111, 3.1415926535897932, PAL_EPSILON * 10 }, // expected: pi |
127 | { PAL_POSINF, PAL_POSINF, 0 }, |
128 | }; |
129 | |
130 | |
131 | if (PAL_Initialize(argc, argv) != 0) |
132 | { |
133 | return FAIL; |
134 | } |
135 | |
136 | for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++) |
137 | { |
138 | validate(tests[i].value, tests[i].expected, tests[i].variance); |
139 | } |
140 | |
141 | validate_isnan(PAL_NEGINF); |
142 | validate_isnan(PAL_NAN); |
143 | |
144 | PAL_Terminate(); |
145 | return PASS; |
146 | } |
147 | |