Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/math/special_functions/atanh_test.hpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.8 KB
Line 
1// unit test file atanh.hpp for the special functions test suite
2
3//  (C) Copyright Hubert Holin 2003.
4//  Distributed under the Boost Software License, Version 1.0. (See
5//  accompanying file LICENSE_1_0.txt or copy at
6//  http://www.boost.org/LICENSE_1_0.txt)
7
8
9#include <functional>
10#include <iomanip>
11//#include <iostream>
12
13
14#include <boost/math/special_functions/atanh.hpp>
15
16
17#include <boost/test/unit_test.hpp>
18#include <boost/test/test_case_template.hpp>
19
20template<typename T>
21T    atanh_error_evaluator(T x)
22{
23    using    ::std::abs;
24    using    ::std::tanh;
25    using    ::std::cosh;
26       
27    using    ::std::numeric_limits;
28   
29    using    ::boost::math::atanh;
30   
31   
32    static T const   epsilon = numeric_limits<float>::epsilon();
33   
34    T                y = tanh(x);
35    T                z = atanh(y);
36   
37    T                absolute_error = abs(z-x);
38    T                relative_error = absolute_error/(cosh(x)*cosh(x));
39    T                scaled_error = relative_error/epsilon;
40   
41    return(scaled_error);
42}
43
44
45BOOST_TEST_CASE_TEMPLATE_FUNCTION(atanh_test, T)
46{
47    using    ::std::abs;
48    using    ::std::tanh;
49    using    ::std::log;
50       
51    using    ::std::numeric_limits;
52   
53    using    ::boost::math::atanh;
54   
55   
56    BOOST_MESSAGE("Testing atanh in the real domain for "
57        << string_type_name<T>::_() << ".");
58   
59    BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
60        (abs(atanh<T>(static_cast<T>(0))))
61        (numeric_limits<T>::epsilon()));
62   
63    BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
64        (abs(atanh<T>(static_cast<T>(3)/5) - log(static_cast<T>(2))))
65        (numeric_limits<T>::epsilon()));
66   
67    BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
68        (abs(atanh<T>(static_cast<T>(-3)/5) + log(static_cast<T>(2))))
69        (numeric_limits<T>::epsilon()));
70   
71    for    (int i = 0; i <= 100; i++)
72    {
73        T    x = static_cast<T>(i-50)/static_cast<T>(5);
74        T    y = tanh(x);
75       
76        if    (
77                (abs(y-static_cast<T>(1)) >= numeric_limits<T>::epsilon())&&
78                (abs(y+static_cast<T>(1)) >= numeric_limits<T>::epsilon())
79            )
80        {
81            BOOST_CHECK_PREDICATE(::std::less_equal<T>(),
82                (atanh_error_evaluator(x))
83                (static_cast<T>(4)));
84        }
85    }
86}
87
88
89void    atanh_manual_check()
90{
91    using    ::std::abs;
92    using    ::std::tanh;
93       
94    using    ::std::numeric_limits;
95   
96   
97    BOOST_MESSAGE(" ");
98    BOOST_MESSAGE("atanh");
99   
100    for    (int i = 0; i <= 100; i++)
101    {
102        float        xf = static_cast<float>(i-50)/static_cast<float>(5);
103        double       xd = static_cast<double>(i-50)/static_cast<double>(5);
104        long double  xl =
105                static_cast<long double>(i-50)/static_cast<long double>(5);
106       
107        float        yf = tanh(xf);
108        double       yd = tanh(xd);
109        (void) &yd;        // avoid "unused variable" warning
110        long double  yl = tanh(xl);
111        (void) &yl;        // avoid "unused variable" warning
112       
113        if    (
114                std::numeric_limits<float>::has_infinity &&
115                std::numeric_limits<double>::has_infinity &&
116                std::numeric_limits<long double>::has_infinity
117            )
118        {
119            BOOST_MESSAGE( ::std::setw(15)
120                        << atanh_error_evaluator(xf)
121                        << ::std::setw(15)
122                        << atanh_error_evaluator(xd)
123                        << ::std::setw(15)
124                        << atanh_error_evaluator(xl));
125        }
126        else
127        {
128            if    (
129                    (abs(yf-static_cast<float>(1)) <
130                        numeric_limits<float>::epsilon())||
131                    (abs(yf+static_cast<float>(1)) <
132                        numeric_limits<float>::epsilon())||
133                    (abs(yf-static_cast<double>(1)) <
134                        numeric_limits<double>::epsilon())||
135                    (abs(yf+static_cast<double>(1)) <
136                        numeric_limits<double>::epsilon())||
137                    (abs(yf-static_cast<long double>(1)) <
138                        numeric_limits<long double>::epsilon())||
139                    (abs(yf+static_cast<long double>(1)) <
140                        numeric_limits<long double>::epsilon())
141                )
142            {
143                BOOST_MESSAGE("Platform's numerics may lack precision.");
144            }
145            else
146            {
147                BOOST_MESSAGE( ::std::setw(15)
148                            << atanh_error_evaluator(xf)
149                            << ::std::setw(15)
150                            << atanh_error_evaluator(xd)
151                            << ::std::setw(15)
152                            << atanh_error_evaluator(xl));
153            }
154        }
155    }
156   
157    BOOST_MESSAGE(" ");
158}
159
Note: See TracBrowser for help on using the repository browser.