summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/atan/test1/test1.cpp
blob: 6840d4617242ec0313981c8b8cbf7c0c9e0978e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=============================================================================
**
** Source: test1.c
**
** Purpose: Test to ensure that atan return the correct values
** 
** Dependencies: PAL_Initialize
**               PAL_Terminate
**               Fail
**               fabs
**
**===========================================================================*/

#include <palsuite.h>

// binary64 (double) has a machine epsilon of 2^-52 (approx. 2.22e-16). However, this 
// is slightly too accurate when writing tests meant to run against libm implementations
// for various platforms. 2^-50 (approx. 8.88e-16) seems to be as accurate as we can get.
//
// The tests themselves will take PAL_EPSILON and adjust it according to the expected result
// so that the delta used for comparison will compare the most significant digits and ignore
// any digits that are outside the double precision range (15-17 digits).

// For example, a test with an expect result in the format of 0.xxxxxxxxxxxxxxxxx will use
// PAL_EPSILON for the variance, while an expected result in the format of 0.0xxxxxxxxxxxxxxxxx
// will use PAL_EPSILON / 10 and and expected result in the format of x.xxxxxxxxxxxxxxxx will
// use PAL_EPSILON * 10.
#define PAL_EPSILON 8.8817841970012523e-16

#define PAL_NAN     sqrt(-1.0)
#define PAL_POSINF -log(0.0)
#define PAL_NEGINF  log(0.0)

/**
 * Helper test structure
 */
struct test
{
    double value;     /* value to test the function with */
    double expected;  /* expected result */
    double variance;  /* maximum delta between the expected and actual result */
};

/**
 * validate
 *
 * test validation function
 */
void __cdecl validate(double value, double expected, double variance)
{
    double result = atan(value);

    /*
     * The test is valid when the difference between result
     * and expected is less than or equal to variance
     */
    double delta = fabs(result - expected);

    if (delta > variance)
    {
        Fail("atan(%g) returned %20.17g when it should have returned %20.17g",
             value, result, expected);
    }
}

/**
 * validate
 *
 * test validation function for values returning NaN
 */
void __cdecl validate_isnan(double value)
{
    double result = atan(value);

    if (!_isnan(result))
    {
        Fail("atan(%g) returned %20.17g when it should have returned %20.17g",
             value, result, PAL_NAN);
    }
}

/**
 * main
 * 
 * executable entry point
 */
int __cdecl main(int argc, char **argv)
{
    struct test tests[] = 
    {
        /* value                   expected                variance */
        {  0,                      0,                      PAL_EPSILON },
        {  0.32951473309607836,    0.31830988618379067,    PAL_EPSILON },           // expected:  1 / pi
        {  0.45054953406980750,    0.42331082513074800,    PAL_EPSILON },           // expected:  pi - e
        {  0.46382906716062964,    0.43429448190325183,    PAL_EPSILON },           // expected:  log10(e)
        {  0.73930295048660405,    0.63661977236758134,    PAL_EPSILON },           // expected:  2 / pi
        {  0.83064087786078395,    0.69314718055994531,    PAL_EPSILON },           // expected:  ln(2)
        {  0.85451043200960189,    0.70710678118654752,    PAL_EPSILON },           // expected:  1 / sqrt(2)
        {  1,                      0.78539816339744831,    PAL_EPSILON },           // expected:  pi / 4
        {  1.1134071468135374,     0.83900756059574755,    PAL_EPSILON },           // expected:  pi - ln(10)
        {  1.5574077246549022,     1,                      PAL_EPSILON * 10 },
        {  2.1108768356626451,     1.1283791670955126,     PAL_EPSILON * 10 },      // expected:  2 / sqrt(pi)
        {  6.3341191670421916,     1.4142135623730950,     PAL_EPSILON * 10 },      // expected:  sqrt(2)
        {  7.7635756709721848,     1.4426950408889634,     PAL_EPSILON * 10 },      // expected:  log2(e)
        {  PAL_POSINF,             1.5707963267948966,     PAL_EPSILON * 10 },      // expected:  pi / 2
    };

    /* PAL initialization */
    if (PAL_Initialize(argc, argv) != 0)
    {
        return FAIL;
    }

    for (int i = 0; i < (sizeof(tests) / sizeof(struct test)); i++)
    {
        validate( tests[i].value,  tests[i].expected, tests[i].variance);
        validate(-tests[i].value, -tests[i].expected, tests[i].variance);
    }

    validate_isnan(PAL_NAN);

    PAL_Terminate();
    return PASS;
}