#include <support.h>
Public Methods | |
support () | |
declare but do not define the constructor. More... | |
~support () | |
declare but do not define the destructor. More... | |
support (const support &rhs) | |
declare but never define copy constructor. More... | |
size_t | UnsignedValWidth (const size_t val) |
Calculate the number of bits needed to represent an unsigned value. More... | |
Static Public Methods | |
size_t | valWidth (const int val) |
Calculate the number of bits needed to represent an integer value. More... | |
size_t | vecWidth (const int *vec, const size_t N) |
Calculate the minimum number of bits needed to represent the values in an integer vector. More... | |
void | roundToInt (int *intVec, const double *realVec, const size_t len) |
Round an array of doubles to three decimal places and multiply by 1000, resulting in an integer that reflects the double value. More... | |
void | decimalToInt (int *intVec, const double *realVec, const size_t len) |
Convert a decimalized array of doubles (e.g., where there are two base ten fractional digits (e.g., 6.02, 3.14, 1.15) into integer form. More... | |
Static Private Methods | |
size_t | nearestPower2Width_ (size_t val) |
int | roundVal_ (const double val) |
Represent a real value with three fractional digits in integer form. More... |
These functions are the object oriented version of global functions. They are all pure functions (e.g., no state in the function or in the class) and static.
Bit width calculation
In compression applications an attempt is made to represent a data set in a minimal number of bits. For these applications it must also be possible to decompress the data. If the values in the data set are stored in bit fields of different widths, information about these fields must be stored as well.
Unlike compression algorithms, where decompresson must be taken into account, this code simple calculates the sum of the bit fields needed to represent a set of signed integer values. This is useful for estimating how closely a wavelet transform approximated a data set.
Double to integer conversion
The compression algorith uses integer to integer lossless wavelet transforms. Since the financial data I am interested in is in real form, it needs to be converted to integer. This is done preserving three fractional digits.
The constructors and destructor are declared but not defined, which will result in a link error if an instance of this class is created (which is not the intent in the design).
Definition at line 70 of file support.h.
|
declare but do not define the constructor.
|
|
declare but do not define the destructor.
|
|
declare but never define copy constructor.
|
|
Calculate the number of bits needed to represent an unsigned value.
Definition at line 165 of file support.cpp. 00166 { 00167 size_t width = nearestPower2Width_( val ); 00168 00169 return width; 00170 } // valWidth |
|
Convert a decimalized array of doubles (e.g., where there are two base ten fractional digits (e.g., 6.02, 3.14, 1.15) into integer form.
This is done my multiplying by 100 and truncating. No rounding is done, since this function assumes that here are only two significant fractional digits. The Definition at line 113 of file support.cpp. Referenced by main().
00116 { 00117 if (intVec != 0 && realVec != 0) { 00118 for (size_t i = 0; i < len; i++) { 00119 intVec[i] = (int)(realVec[i] * 100.0); 00120 } 00121 } 00122 } // roundToInt |
|
Definition at line 127 of file support.cpp. Referenced by UnsignedValWidth(), and valWidth().
00128 { 00129 size_t width = 0; 00130 if (val > 0) { 00131 width = 1; 00132 size_t power = 1; 00133 while (power < val && width < 32) { 00134 power = power << 1; 00135 width++; 00136 } 00137 } 00138 00139 return width; 00140 } // nearestPower2Width_ |
|
Round an array of doubles to three decimal places and multiply by 1000, resulting in an integer that reflects the double value. For equity time series this kind of rounding is useful for "old style" pre-decimalization time series where the fractional values represent increments by 1/16. This kind of rounding is also useful for adjusted time series (e.g., the time series is adjusted by a split and/or dividends are reinvested). Since this function results in an integer vector with integer values reflecting three decimal places, this function should not be used for decimalized values (which only have two decimal places). Definition at line 93 of file support.cpp. 00096 { 00097 if (intVec != 0 && realVec != 0) { 00098 for (size_t i = 0; i < len; i++) { 00099 intVec[i] = roundVal_( realVec[i] ); 00100 } 00101 } 00102 } // roundToInt |
|
Represent a real value with three fractional digits in integer form. To accomplish this the real number is rounded and then multiplied by 1000.0. The rounding used is so proper rounding:
Proper rounding is unbiased (that is, in a random sample of numbers the same number of values are rounded up as are rounded down). Some examples:
12.4567 is rounded to 12.457 and converted to the integer 12457. 42.1234 is rounded to 42.123 and converted to the integer 42123. 127.1235 is rounded to 127.124 and converted to the integer 127124. While this function does what I intended, it does seem to do it in a lot of operations. There is probably a faster way to do this by directly manipulating IEEE floating point. While this might be faster, it is more complex. Definition at line 51 of file support.cpp. Referenced by roundToInt().
00052 { 00053 int intPart = static_cast<int>(val); 00054 double fracPart = val - intPart; 00055 int threeDigits = fracPart * 1000.0; 00056 int fourDigits = fracPart * 10000.0; 00057 int forthDigit = fourDigits % 10; 00058 int thirdDigit = threeDigits % 10; 00059 00060 double roundVal = 0.001; 00061 if (forthDigit < 5) { 00062 roundVal = 0.0; 00063 } 00064 else if (forthDigit == 5) { 00065 if ((thirdDigit & 0x1) == 0) { 00066 roundVal = 0.0; 00067 } 00068 } 00069 double newVal = val + roundVal; 00070 double intRslt = newVal * 1000.0; 00071 return intRslt; 00072 } // roundVal_ |
|
Calculate the number of bits needed to represent an integer value. A sign bit is added, so that positive numbers always have a leading zero and negative numbers have a leading one. Definition at line 150 of file support.cpp. Referenced by vecWidth().
00151 { 00152 size_t wholeNum = (val < 0) ? -val : val; 00153 size_t width = 1 + nearestPower2Width_( wholeNum ); 00154 00155 return width; 00156 } // valWidth |
|
Calculate the minimum number of bits needed to represent the values in an integer vector.
Definition at line 177 of file support.cpp. Referenced by calcPacketWidth(), costwidth::costCalc(), delta_calc(), main(), rollingCompressWindow(), and wave_calc().
00178 { 00179 size_t totalWidth = 0; 00180 if (vec != 0) { 00181 for (size_t i = 0; i < N; i++) { 00182 totalWidth += valWidth( vec[i] ); 00183 } 00184 } 00185 return totalWidth; 00186 } // vecWidth( int *) |