source: trunk/test_more/epsilon.cpp @ 2506

Last change on this file since 2506 was 2506, checked in by bradbell, 8 years ago

Change Licenses: CPL-1.0 -> EPL-1.0, GPL-2.0->GPL-3.0

  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1/* $Id: epsilon.cpp 2506 2012-10-24 19:36:49Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7                    Eclipse Public License Version 1.0.
8
9A copy of this license is included in the COPYING file of this distribution.
10Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
11-------------------------------------------------------------------------- */
12
13/*
14$begin epsilon.cpp$$
15$spell
16$$
17
18$section Machine Epsilon: Example and Test$$
19$index epsilon$$
20$index example, epsilon$$
21$index test, epsilon$$
22
23$end
24*/
25// BEGIN C++
26
27# ifdef _MSC_VER
28// Supress Microsoft compiler warning about possible loss of precision,
29// in the constructors (when converting to std::complex<float>)
30//      Type one = 1
31//      Type two = 2
32// 1 and 2 are small enough so no loss of precision when converting to float.
33# pragma warning(disable:4244)
34# endif
35
36# include <cppad/cppad.hpp>
37# include <complex>
38
39namespace {
40        template <class Type>
41        Type add_one(const Type& value)
42        {       return( Type(1) + value ); }
43        //
44        template <class Type>
45        bool check_epsilon(void)
46        {       bool ok  = true;
47                using CppAD::epsilon;
48                using CppAD::abs;
49                Type eps   = CppAD::epsilon<Type>();
50                Type one   = 1;
51                Type two   = 2;
52                Type eps2  = eps / two; 
53                Type check = add_one(eps);
54                ok        &= one !=  check;
55                check      = add_one(eps2);
56                ok        &= one == check;
57                return ok;
58        }
59}
60
61bool epsilon(void)
62{       bool ok = true;
63        using CppAD::AD;
64
65        // Machine epsilon for Base types defined by CppAD
66        // (see base_require for defining ones own Base type).
67        ok &= check_epsilon<float>();
68        ok &= check_epsilon<double>();
69        ok &= check_epsilon< std::complex<float> >();
70        // ok &= check_epsilon< std::complex<double> >();
71
72        // Machine epsilon for some AD types.
73        ok &= check_epsilon< AD<float> >();
74        ok &= check_epsilon< AD<double> >();
75        ok &= check_epsilon<  AD<std::complex<float> > >();
76        ok &= check_epsilon<  AD<std::complex<double> > >();
77
78        return ok;
79}
80// END C++
Note: See TracBrowser for help on using the repository browser.