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