IMP logo
IMP Reference Guide  2.22.0
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-2022 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 <cmath>
24 #include <boost/version.hpp>
25 
26 #if BOOST_VERSION < 106900
27 #include <boost/detail/endian.hpp>
28 #else
29 #include <boost/predef/other/endian.h>
30 #endif
31 
32 IMPEM_BEGIN_NAMESPACE
33 
34 /* Unions are similar to structs, but they differ in one aspect:
35  the fields of a union share the same position in memory.
36  The size of the union is the size of its largest field
37  (or larger if alignment so requires, for example on a SPARC machine
38  a union contains a double and a char [17] so its size is likely to be
39  24 because it needs 64-bit alignment).
40  What is the point of this? Unions provide multiple ways of viewing
41  the same memory location, allowing for \more efficient use of memory.
42  Most of the uses of unions are covered by object-oriented features of C++,
43  so it is more common in C. However, sometimes it is convenient to avoid
44  the formalities of object-oriented programming when performance is
45  important or when one knows that the item in question will not be extended.
46  http://en.wikibooks.org/wiki/C++_Programming/Union
47 */
48 typedef union {
49  double d;
50  struct {
51 #if defined(BOOST_ENDIAN_LITTLE_BYTE) || defined(BOOST_LITTLE_ENDIAN)
52  int j, i;
53 #else
54  int i, j;
55 #endif
56  } n;
57 } _eco;
58 
59 /* Originally was (1048576/M_LN2); M_LN2 expanded out so that this header
60  can be correctly #include'd in other code on Windows machines without having
61  to #define the non-standard _USE_MATH_DEFINES macro */
62 #define IMP_EXP_A (1048576 / 0.693147180559945309417232121458176568)
63 #define IMP_EXP_C 60801
64 
65 inline double EXP(float y) {
66  static _eco eco;
67  eco.n.i = (int)(IMP_EXP_A * (y) + (1072693248 - IMP_EXP_C));
68  return eco.d;
69 }
70 
71 IMPEM_END_NAMESPACE
72 
73 #endif /* IMPEM_EXP_H */