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