CppAD: A C++ Algorithmic Differentiation Package  20130102
limits.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 # ifndef CPPAD_LIMITS_INCLUDED
00003 # define CPPAD_LIMITS_INCLUDED
00004 /* --------------------------------------------------------------------------
00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-13 Bradley M. Bell
00006 
00007 CppAD is distributed under multiple licenses. This distribution is under
00008 the terms of the 
00009                     Eclipse Public License Version 1.0.
00010 
00011 A copy of this license is included in the COPYING file of this distribution.
00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
00013 -------------------------------------------------------------------------- */
00014 
00015 /*
00016 ------------------------------------------------------------------------------
00017 $begin limits$$
00018 $spell
00019      std
00020      eps
00021      CppAD
00022      namespace
00023      const
00024 $$
00025 $index limits, AD$$
00026 $index AD, limits$$
00027 $index epsilon, AD$$
00028 $index limit, max$$
00029 $index limit, min$$
00030 $index limit, epsilon$$
00031 
00032 $section Numeric Limits For an AD and Base Types$$
00033 
00034 $head Syntax$$
00035 $icode%eps% = numeric_limits<%Float%>::epsilon()
00036 %$$
00037 $icode%min% = numeric_limits<%Float%>::min()
00038 %$$
00039 $icode%max% = numeric_limits<%Float%>::max()
00040 %$$
00041 
00042 $head Purpose$$
00043 Obtain the value of some of the C++ standard numeric limits
00044 using the CppAD namespace version of $code numeric_limits$$.
00045 These are all functions and have the prototype
00046 $codei%
00047      %Float% numeric_limits<%Float%>::%fun%(%void%)
00048 %$$
00049 where $icode fun$$ is $code epsilon$$, $code min$$, or $code max$$.
00050 $pre
00051 
00052 $$
00053 Note that C++ standard specifies that Non-fundamental standard
00054 types, such as $codei%std::complex<%T%>%$$ shall not have specializations
00055 of $code std::numeric_limits$$; see Section 18.2 of
00056 ISO/IEC 14882:1998(E).
00057 
00058 $head Float$$
00059 These functions are defined for all $codei%AD<%Base%>%$$,
00060 and for all corresponding $icode Base$$ types;
00061 see $icode Base$$ type $cref/limits/base_std_math/limits/$$.
00062 
00063 $head eps$$
00064 The result $icode eps$$ is equal to machine epsilon and has prototype
00065 $codei%
00066      %Float% %eps%
00067 %$$
00068 CppAD tests the value $icode eps$$ by checking that the following are true
00069 $codei%
00070      1 != 1 + %eps%
00071      1 == 1 + %eps% / 2
00072 %$$
00073 where all the values, and calculations, are done with the precision
00074 corresponding to $icode Float$$.
00075      
00076 
00077 $head min$$
00078 The result $icode min$$ is equal to 
00079 the minimum positive normalized value and has prototype
00080 $codei%
00081      %Float% %min%
00082 %$$
00083 CppAD tests the value $icode min$$ by checking that the following are true
00084 $codei%
00085      abs( ((%min% / 100) * 100) / %min% - 1 ) > 3 * %eps%
00086      abs( ((%min% * 100) / (100 * (1 - %eps%)) / %min% - 1 ) < 3 * %eps%
00087 %$$
00088 where all the values, and calculations, are done with the precision
00089 corresponding to $icode Float$$.
00090 
00091 $head max$$
00092 The result $icode max$$ is equal to 
00093 the maximum finite value and has prototype
00094 $codei%
00095      %Float% %max%
00096 %$$
00097 CppAD tests the value $icode max$$ by checking that the following are true
00098 $codei%
00099      abs( ((%max% * 100) / 100) / %max% - 1 ) > 3 * %eps%
00100      abs( ((%max% / 100) * (100 * (1 - %eps%)) / %max% - 1 ) < 3 * %eps%
00101 %$$
00102 where all the values, and calculations, are done with the precision
00103 corresponding to $icode Float$$.
00104 
00105 $head Example$$
00106 $children%
00107      example/limits.cpp
00108 %$$
00109 The file
00110 $cref limits.cpp$$
00111 contains an example and test of these functions.
00112 
00113 $end 
00114 ------------------------------------------------------------------------------
00115 */
00116 # include <iostream>
00117 
00118 # include <cppad/configure.hpp>
00119 # include <cppad/local/define.hpp>
00120 # include <cppad/local/cppad_assert.hpp>
00121 # include <cppad/local/declare_ad.hpp>
00122 
00123 CPPAD_BEGIN_NAMESPACE
00124 /*!
00125 \defgroup limits_hpp limits.hpp
00126 \{
00127 \file limits.hpp
00128 File that defines CppAD numeric_limits for AD types
00129 */
00130 
00131 /// Default value for all undefined numeric_limits types
00132 template <class Float>
00133 class numeric_limits {
00134 public:
00135      /// machine epsilon
00136      static Float epsilon(void)
00137      {    CPPAD_ASSERT_KNOWN(
00138           false,
00139           "numeric_limits<Float>::epsilon() is not specialized for this Float"
00140           );
00141           return Float(0);
00142      }
00143      /// minimum positive normalized value
00144      static Float min(void)
00145      {    CPPAD_ASSERT_KNOWN(
00146           false,
00147           "numeric_limits<Float>::min() is not specialized for this Float"
00148           );
00149           return Float(0);
00150      }
00151      /// maximum finite value
00152      static Float max(void)
00153      {    CPPAD_ASSERT_KNOWN(
00154           false,
00155           "numeric_limits<Float>::max() is not specialized for this Float"
00156           );
00157           return Float(0);
00158      }
00159 };
00160 
00161 /// Partial specialization that defines limits for for all AD types
00162 template <class Base>
00163 class numeric_limits< AD<Base> > {
00164 public:
00165      /// machine epsilon
00166      static AD<Base> epsilon(void)
00167      {    return AD<Base>( numeric_limits<Base>::epsilon() ); }
00168      /// minimum positive normalized value
00169      static AD<Base> min(void)
00170      {    return AD<Base>( numeric_limits<Base>::min() ); }
00171      /// maximum finite value
00172      static AD<Base> max(void)
00173      {    return AD<Base>( numeric_limits<Base>::max() ); }
00174 };
00175 
00176 /*! \} */
00177 CPPAD_END_NAMESPACE
00178 # endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines