IMP  2.2.1
The Integrative Modeling Platform
exp.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em/exp.h
3  * \brief An approximation of the exponential function.
4  *
5  * Copyright 2007-2014 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPEM_EXP_H
10 #define IMPEM_EXP_H
11 
12 #include <IMP/em/em_config.h>
13 
14 /* An approximation of the exponential function.
15  Schraudolph, Nicol N. "A Fast, Compact Approximation of the exponential
16  function" Technical Report IDSIA-07-98
17  (Neural Computation 11(4))
18  The function returns values within 1-2% deviation of the correct ones in
19  the range (-700,700)
20 */
21 
22 #include <IMP/em/em_config.h>
23 #include <math.h>
24 
25 IMPEM_BEGIN_NAMESPACE
26 
27 /* Unions are similar to structs, but they differ in one aspect:
28  the fields of a union share the same position in memory.
29  The size of the union is the size of its largest field
30  (or larger if alignment so requires, for example on a SPARC machine
31  a union contains a double and a char [17] so its size is likely to be
32  24 because it needs 64-bit alignment).
33  What is the point of this? Unions provide multiple ways of viewing
34  the same memory location, allowing for \more efficient use of memory.
35  Most of the uses of unions are covered by object-oriented features of C++,
36  so it is more common in C. However, sometimes it is convenient to avoid
37  the formalities of object-oriented programming when performance is
38  important or when one knows that the item in question will not be extended.
39  http://en.wikibooks.org/wiki/C++_Programming/Union
40 */
41 typedef union {
42  double d;
43  struct {
44 #ifdef BOOST_LITTLE_ENDIAN
45  int j, i;
46 #else
47  int i, j;
48 #endif
49  } n;
50 } _eco;
51 
52 /* Originally was (1048576/M_LN2); M_LN2 expanded out so that this header
53  can be correctly #include'd in other code on Windows machines without having
54  to #define the non-standard _USE_MATH_DEFINES macro */
55 #define IMP_EXP_A (1048576 / 0.693147180559945309417232121458176568)
56 #define IMP_EXP_C 60801
57 
58 inline double EXP(float y) {
59  static _eco eco;
60  eco.n.i = (int)(IMP_EXP_A * (y) + (1072693248 - IMP_EXP_C));
61  return eco.d;
62 }
63 
64 IMPEM_END_NAMESPACE
65 
66 #endif /* IMPEM_EXP_H */