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 log10f returns correct values. |
10 | ** |
11 | ** Dependencies: PAL_Initialize |
12 | ** PAL_Terminate |
13 | ** Fail |
14 | ** fabs |
15 | ** _isnanf |
16 | ** |
17 | **===========================================================================*/ |
18 | |
19 | #include <palsuite.h> |
20 | |
21 | // binary32 (float) has a machine epsilon of 2^-23 (approx. 1.19e-07). However, this |
22 | // is slightly too accurate when writing tests meant to run against libm implementations |
23 | // for various platforms. 2^-21 (approx. 4.76e-07) 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 (6-9 digits). |
28 | |
29 | // For example, a test with an expect result in the format of 0.xxxxxxxxx will use PAL_EPSILON |
30 | // for the variance, while an expected result in the format of 0.0xxxxxxxxx will use |
31 | // PAL_EPSILON / 10 and and expected result in the format of x.xxxxxx will use PAL_EPSILON * 10. |
32 | #define PAL_EPSILON 4.76837158e-07 |
33 | |
34 | #define PAL_NAN sqrtf(-1.0f) |
35 | #define PAL_POSINF -logf(0.0f) |
36 | #define PAL_NEGINF logf(0.0f) |
37 | |
38 | /** |
39 | * Helper test structure |
40 | */ |
41 | struct test |
42 | { |
43 | float value; /* value to test the function with */ |
44 | float expected; /* expected result */ |
45 | float variance; /* maximum delta between the expected and actual result */ |
46 | }; |
47 | |
48 | /** |
49 | * validate |
50 | * |
51 | * test validation function |
52 | */ |
53 | void __cdecl validate(float value, float expected, float variance) |
54 | { |
55 | float result = log10f(value); |
56 | |
57 | /* |
58 | * The test is valid when the difference between result |
59 | * and expected is less than or equal to variance |
60 | */ |
61 | float delta = fabsf(result - expected); |
62 | |
63 | if (delta > variance) |
64 | { |
65 | Fail("log10f(%g) returned %10.9g when it should have returned %10.9g" , |
66 | value, result, expected); |
67 | } |
68 | } |
69 | |
70 | /** |
71 | * validate |
72 | * |
73 | * test validation function for values returning NaN |
74 | */ |
75 | void __cdecl validate_isnan(float value) |
76 | { |
77 | float result = log10f(value); |
78 | |
79 | if (!_isnanf(result)) |
80 | { |
81 | Fail("log10f(%g) returned %10.9g when it should have returned %10.9g" , |
82 | value, result, PAL_NAN); |
83 | } |
84 | } |
85 | |
86 | /** |
87 | * main |
88 | * |
89 | * executable entry point |
90 | */ |
91 | int __cdecl main(int argc, char **argv) |
92 | { |
93 | struct test tests[] = |
94 | { |
95 | /* value expected variance */ |
96 | { 0, PAL_NEGINF, 0 }, |
97 | { 0.000721784159f, -3.14159265f, PAL_EPSILON * 10 }, // expected: -(pi) |
98 | { 0.00191301410f, -2.71828183f, PAL_EPSILON * 10 }, // expected: -(e) |
99 | { 0.00498212830f, -2.30258509f, PAL_EPSILON * 10 }, // expected: -(ln(10)) |
100 | { 0.0268660410f, -1.57079633f, PAL_EPSILON * 10 }, // expected: -(pi / 2) |
101 | { 0.0360831928f, -1.44269504f, PAL_EPSILON * 10 }, // expected: -(logf2(e)) |
102 | { 0.0385288847f, -1.41421356f, PAL_EPSILON * 10 }, // expected: -(sqrtf(2)) |
103 | { 0.0744082059f, -1.12837917f, PAL_EPSILON * 10 }, // expected: -(2 / sqrtf(pi)) |
104 | { 0.1f, -1, PAL_EPSILON * 10 }, // expected: -(1) |
105 | { 0.163908636f, -0.785398163f, PAL_EPSILON }, // expected: -(pi / 4) |
106 | { 0.196287760f, -0.707106781f, PAL_EPSILON }, // expected: -(1 / sqrtf(2)) |
107 | { 0.202699566f, -0.693147181f, PAL_EPSILON }, // expected: -(ln(2)) |
108 | { 0.230876765f, -0.636619772f, PAL_EPSILON }, // expected: -(2 / pi) |
109 | { 0.367879441f, -0.434294482f, PAL_EPSILON }, // expected: -(log10f(e)) |
110 | { 0.480496373f, -0.318309886f, PAL_EPSILON }, // expected: -(1 / pi) |
111 | { 1, 0, PAL_EPSILON }, |
112 | { 2.08118116f, 0.318309886f, PAL_EPSILON }, // expected: 1 / pi |
113 | { 2.71828183f, 0.434294482f, PAL_EPSILON }, // expected: log10f(e) value: e |
114 | { 4.33131503f, 0.636619772f, PAL_EPSILON }, // expected: 2 / pi |
115 | { 4.93340967f, 0.693147181f, PAL_EPSILON }, // expected: ln(2) |
116 | { 5.09456117f, 0.707106781f, PAL_EPSILON }, // expected: 1 / sqrtf(2) |
117 | { 6.10095980f, 0.785398163f, PAL_EPSILON }, // expected: pi / 4 |
118 | { 10, 1, PAL_EPSILON * 10 }, |
119 | { 13.4393779f, 1.12837917f, PAL_EPSILON * 10 }, // expected: 2 / sqrtf(pi) |
120 | { 25.9545535f, 1.41421356f, PAL_EPSILON * 10 }, // expected: sqrtf(2) |
121 | { 27.7137338f, 1.44269504f, PAL_EPSILON * 10 }, // expected: logf2(e) |
122 | { 37.2217105f, 1.57079633f, PAL_EPSILON * 10 }, // expected: pi / 2 |
123 | { 200.717432f, 2.30258509f, PAL_EPSILON * 10 }, // expected: ln(10) |
124 | { 522.735300f, 2.71828183f, PAL_EPSILON * 10 }, // expected: e |
125 | { 1385.45573f, 3.14159265f, PAL_EPSILON * 10 }, // expected: pi |
126 | { PAL_POSINF, PAL_POSINF, 0 }, |
127 | }; |
128 | |
129 | if (PAL_Initialize(argc, argv) != 0) |
130 | { |
131 | return FAIL; |
132 | } |
133 | |
134 | for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++) |
135 | { |
136 | validate(tests[i].value, tests[i].expected, tests[i].variance); |
137 | } |
138 | |
139 | validate_isnan(PAL_NEGINF); |
140 | validate_isnan(PAL_NAN); |
141 | |
142 | PAL_Terminate(); |
143 | return PASS; |
144 | } |
145 | |