00001 # ifndef CPPAD_INTEGER_INCLUDED 00002 # define CPPAD_INTEGER_INCLUDED 00003 00004 /* -------------------------------------------------------------------------- 00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell 00006 00007 CppAD is distributed under multiple licenses. This distribution is under 00008 the terms of the 00009 Common 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 Integer$$ 00018 $spell 00019 std 00020 VecAD 00021 CppAD 00022 namespace 00023 inline 00024 const 00025 bool 00026 $$ 00027 00028 $index Integer$$ 00029 00030 $index AD, convert to integer$$ 00031 $index convert, AD to integer$$ 00032 00033 $section Convert From AD to Integer$$ 00034 00035 $head Syntax$$ 00036 $syntax%%i% = Integer(%x%)%$$ 00037 00038 00039 $head Purpose$$ 00040 Converts from an AD type to the corresponding integer value. 00041 00042 $head i$$ 00043 The result $italic i$$ has prototype 00044 $syntax% 00045 int %i% 00046 %$$ 00047 00048 $head x$$ 00049 00050 $subhead Real Types$$ 00051 If the argument $italic x$$ has either of the following prototypes: 00052 $syntax% 00053 const float %% &%x% 00054 const double %% &%x% 00055 %$$ 00056 the fractional part is dropped to form the integer value. 00057 For example, if $italic x$$ is 1.5, $italic i$$ is 1. 00058 In general, if $latex x \geq 0$$, $italic i$$ is the 00059 greatest integer less than or equal $italic x$$. 00060 If $latex x \leq 0$$, $italic i$$ is the 00061 smallest integer greater than or equal $italic x$$. 00062 00063 $subhead Complex Types$$ 00064 If the argument $italic x$$ has either of the following prototypes: 00065 $syntax% 00066 const std::complex<float> %% &%x% 00067 const std::complex<double> %% &%x% 00068 %$$ 00069 The result $italic i$$ is given by 00070 $syntax% 00071 %i% = Integer(%x%.real()) 00072 %$$ 00073 00074 $subhead AD Types$$ 00075 If the argument $italic x$$ has either of the following prototypes: 00076 $syntax% 00077 const AD<%Base%> &%x% 00078 const VecAD<%Base%>::reference &%x% 00079 %$$ 00080 $italic Base$$ must support the $code Integer$$ function and 00081 the conversion has the same meaning as for $italic Base$$. 00082 00083 $head Operation Sequence$$ 00084 The result of this operation is not an 00085 $xref/glossary/AD of Base/AD of Base/$$ object. 00086 Thus it will not be recorded as part of an 00087 AD of $italic Base$$ 00088 $xref/glossary/Operation/Sequence/operation sequence/1/$$. 00089 00090 $head Example$$ 00091 $children% 00092 example/integer.cpp 00093 %$$ 00094 The file 00095 $xref/Integer.cpp/$$ 00096 contains an example and test of this operation. 00097 00098 $end 00099 ------------------------------------------------------------------------------ 00100 */ 00101 00102 00103 namespace CppAD { 00104 00105 inline int Integer(const float &x) 00106 { return static_cast<int>(x); } 00107 00108 inline int Integer(const double &x) 00109 { return static_cast<int>(x); } 00110 00111 template <class Base> 00112 inline int Integer(const AD<Base> &x) 00113 { return Integer(x.value_); } 00114 00115 template <class Base> 00116 inline int Integer(const VecAD_reference<Base> &x) 00117 { return Integer( x.ADBase() ); } 00118 } 00119 # endif 00120