00001 /** 00002 * \file exp.h 00003 * \brief An approximation of the exponential function. 00004 * 00005 * Copyright 2007-2010 IMP Inventors. All rights reserved. 00006 * 00007 */ 00008 00009 #ifndef IMPEM_EXP_H 00010 #define IMPEM_EXP_H 00011 00012 #include "em_config.h" 00013 00014 /* An approximation of the exponential function. 00015 Schraudolph, Nicol N. "A Fast, Compact Approximation of the exponential 00016 function" Technical Report IDSIA-07-98 00017 (Neural Computation 11(4)) 00018 The function returns values within 1-2% deviation of the correct ones in 00019 the range (-700,700) 00020 */ 00021 00022 #include "em_config.h" 00023 #include <math.h> 00024 00025 IMPEM_BEGIN_NAMESPACE 00026 00027 /* Unions are similar to structs, but they differ in one aspect: 00028 the fields of a union share the same position in memory. 00029 The size of the union is the size of its largest field 00030 (or larger if alignment so requires, for example on a SPARC machine 00031 a union contains a double and a char [17] so its size is likely to be 00032 24 because it needs 64-bit alignment). 00033 What is the point of this? Unions provide multiple ways of viewing 00034 the same memory location, allowing for \more efficient use of memory. 00035 Most of the uses of unions are covered by object-oriented features of C++, 00036 so it is more common in C. However, sometimes it is convenient to avoid 00037 the formalities of object-oriented programming when performance is 00038 important or when one knows that the item in question will not be extended. 00039 http://en.wikibooks.org/wiki/C++_Programming/Union 00040 */ 00041 typedef union 00042 { 00043 double d; 00044 struct{ 00045 #ifdef IMP_LITTLE_ENDIAN 00046 int j,i; 00047 #else 00048 int i,j; 00049 #endif 00050 } n; 00051 } _eco; 00052 00053 #define IMP_EXP_A (1048576/M_LN2) 00054 #define IMP_EXP_C 60801 00055 00056 00057 00058 00059 inline double EXP(float y) { 00060 static _eco eco; 00061 eco.n.i = (int) (IMP_EXP_A*(y) + (1072693248-IMP_EXP_C)); 00062 return eco.d; 00063 } 00064 00065 IMPEM_END_NAMESPACE 00066 00067 #endif /* IMPEM_EXP_H */