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