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 scalbn returns correct values.
10**
11**===================================================================*/
12
13#include <palsuite.h>
14
15// binary64 (double) has a machine epsilon of 2^-52 (approx. 2.22e-16). However, this
16// is slightly too accurate when writing tests meant to run against libm implementations
17// for various platforms. 2^-50 (approx. 8.88e-16) seems to be as accurate as we can get.
18//
19// The tests themselves will take PAL_EPSILON and adjust it according to the expected result
20// so that the delta used for comparison will compare the most significant digits and ignore
21// any digits that are outside the double precision range (15-17 digits).
22
23// For example, a test with an expect result in the format of 0.xxxxxxxxxxxxxxxxx will use
24// PAL_EPSILON for the variance, while an expected result in the format of 0.0xxxxxxxxxxxxxxxxx
25// will use PAL_EPSILON / 10 and and expected result in the format of x.xxxxxxxxxxxxxxxx will
26// use PAL_EPSILON * 10.
27#define PAL_EPSILON 8.8817841970012523e-16
28
29#define PAL_NAN sqrt(-1.0)
30#define PAL_POSINF -log(0.0)
31#define PAL_NEGINF log(0.0)
32
33/**
34 * Helper test structure
35 */
36struct test
37{
38 double value; /* value to test the function with */
39 int exponent; /* exponent to test the function with */
40 double expected; /* expected result */
41 double variance; /* maximum delta between the expected and actual result */
42};
43
44/**
45 * validate
46 *
47 * test validation function
48 */
49void __cdecl validate(double value, int exponent, double expected, double variance)
50{
51 double result = scalbn(value, exponent);
52
53 /*
54 * The test is valid when the difference between result
55 * and expected is less than or equal to variance
56 */
57 double delta = fabs(result - expected);
58
59 if (delta > variance)
60 {
61 Fail("scalbn(%g, %d) returned %20.17g when it should have returned %20.17g\n",
62 value, exponent, result, expected);
63 }
64}
65
66/**
67 * validate
68 *
69 * test validation function for values returning NaN
70 */
71void __cdecl validate_isnan(double value, int exponent)
72{
73 double result = scalbn(value, exponent);
74
75 if (!_isnan(result))
76 {
77 Fail("scalbn(%g, %d) returned %20.17g when it should have returned %20.17g\n",
78 value, exponent, result, PAL_NAN);
79 }
80}
81
82/**
83 * main
84 *
85 * executable entry point
86 */
87int __cdecl main(int argc, char **argv)
88{
89 struct test tests[] =
90 {
91 /* value exponent expected variance */
92 { PAL_NEGINF, 0x80000000, PAL_NEGINF, 0 },
93 { 0, 0x80000000, 0, 0 },
94 { 0.11331473229676087, -3, 0.014164341537095108, PAL_EPSILON / 10 },
95 { 0.15195522325791297, -2, 0.037988805814478242, PAL_EPSILON / 10 },
96 { 0.20269956628651730, -2, 0.050674891571629327, PAL_EPSILON / 10 },
97 { 0.33662253682241906, -1, 0.16831126841120952, PAL_EPSILON },
98 { 0.36787944117144232, -1, 0.18393972058572117, PAL_EPSILON },
99 { 0.37521422724648177, -1, 0.1876071136232409, PAL_EPSILON },
100 { 0.45742934732229695, -1, 0.22871467366114848, PAL_EPSILON },
101 { 0.5, -1, 0.25, PAL_EPSILON },
102 { 0.58019181037172444, 0, 0.5801918103717244, PAL_EPSILON },
103 { 0.61254732653606592, 0, 0.61254732653606592, PAL_EPSILON },
104 { 0.61850313780157598, 0, 0.61850313780157595, PAL_EPSILON },
105 { 0.64321824193300488, 0, 0.64321824193300492, PAL_EPSILON },
106 { 0.74005557395545179, 0, 0.74005557395545174, PAL_EPSILON },
107 { 0.80200887896145195, 0, 0.8020088789614519, PAL_EPSILON },
108 { 1, 0, 1, PAL_EPSILON * 10 },
109 { 1.2468689889006383, 0, 1.2468689889006384, PAL_EPSILON * 10 },
110 { 1.3512498725672678, 0, 1.3512498725672677, PAL_EPSILON * 10 },
111 { 1.5546822754821001, 0, 1.5546822754821001, PAL_EPSILON * 10 },
112 { 1.6168066722416747, 0, 1.6168066722416747, PAL_EPSILON * 10 },
113 { 1.6325269194381528, 0, 1.6325269194381529, PAL_EPSILON * 10 },
114 { 1.7235679341273495, 0, 1.7235679341273495, PAL_EPSILON * 10 },
115 { 2, 1, 4, PAL_EPSILON * 10 },
116 { 2.1861299583286618, 1, 4.3722599166573239, PAL_EPSILON * 10 },
117 { 2.6651441426902252, 1, 5.3302882853804503, PAL_EPSILON * 10 },
118 { 2.7182818284590452, 1, 5.4365636569180902, PAL_EPSILON * 10 },
119 { 2.9706864235520193, 1, 5.9413728471040388, PAL_EPSILON * 10 },
120 { 4.9334096679145963, 2, 19.733638671658387, PAL_EPSILON * 100 },
121 { 6.5808859910179210, 2, 26.323543964071686, PAL_EPSILON * 100 },
122 { 8.8249778270762876, 3, 70.599822616610297, PAL_EPSILON * 100 },
123 { PAL_POSINF, 0x80000000, PAL_POSINF, 0 },
124 };
125
126 if (PAL_Initialize(argc, argv) != 0)
127 {
128 return FAIL;
129 }
130
131 for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++)
132 {
133 validate(tests[i].value, tests[i].exponent, tests[i].expected, tests[i].variance);
134 }
135
136 validate_isnan(PAL_NAN, 2147483647);
137
138 PAL_Terminate();
139 return PASS;
140}
141