A C++ class template defining a sequence of IEEE-754 floating point values.
The following code
typedef ieee754_seq<float, N, Emin, Emax> seq_t;defines a sequence seq_t of radix-2 IEEE-754 floats
where
and
The range of
For example, the sequence ieee754_seq<float,2,0,2> is
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1.25 | 1.5 | 1.75 | 2 | 2.5 | 3 | 3.5 | 4 |
The floating-pont numbers are not stored in the ieee754_seq object. They are generated on the fly by fast bit-manipulation operations on the integer index
Conversely, a floating-point number
These bit-wise conversions are possible because the IEEE-754 standard is used in most computer systems, thus, the internal representation of a floating-point number is essentially very similar to the one shown above.
The ieee754_seq number sequence is quasi log spaced. Thus it can be employed for optimized log-interpolation schemes with fast lookup and fewer calls to log().
This was originally proposed by Yuan et al NIMB83(1993) p.413 for the interpolation of scattering cross sections tabulated over a wide range of projectile energy.
The implentation here is based on the program Corteo, written by Francois Schiettekatte and released under the GNU GPL. The original corteo source code can be found here: http://www.lps.umontreal.ca/~schiette/index.php?n=Recherche.Corteo
Just include the single header file in your c++ code
#include <"ieee754_seq.h">
typedef ieee754_seq<float, 3, -10, 10> seq_t; // 160 points from ~1e-3 to ~1e3Note that seq_t need not be instantiated as it contains no data variable (all values and functions are static const).
Iteration over the number sequence can be done in various ways:
// for-loop with int
for (int i = 0; i < seq_t::size(); ++i)
cout << i << ' ' << s[i] << endl;
// for-loop with iterator
for (auto i = seq_t::begin(); i < seq_t::end(); ++i)
cout << i << ' ' << *i << ' ' << i.log2v() << endl;
// range for
seq_t s; // here we must instantiate the sequence type
for (const float &x : s)
cout << x << endl;When using an ieee754_seq::iterator the dereference operator * returns the floating point value. Further, the function i.log2v() returns the base-2 logarithm without calling std::log2().
The ieee754_seq class defines 2 interpolator objects for log-lin and log-log interpolation.
typedef ieee754_seq<float, 3, 0, 2> seq_t;
seq_t::log_log_interp interp;
std::vector<float> y(seq_t::size());
for (int i = 0; i < y.size(); ++i) y[i] = some_func(s[i]);
float x = 1.3333f;
float y_i = interp(x); // interpolate y at x