Peano
Loading...
Searching...
No Matches
CompressedFloatingPointNumbers.cpph
Go to the documentation of this file.
1#include <cmath>
2#include <limits>
3
4
5template <class T>
7 const double& value,
8 char& exponent,
9 T& mantissa
10) {
11 int integerExponent;
12 double significant = std::frexp(value , &integerExponent);
13
14 const int shiftExponent = std::numeric_limits<T>::digits-1;
15 const double shiftMantissa = std::pow( 2.0,shiftExponent );
16
17 if (integerExponent-shiftExponent <= std::numeric_limits<char>::min()) {
18 exponent = 0;
19 mantissa = 0;
20 }
21 else {
22 exponent = static_cast<char>( integerExponent-shiftExponent );
23 mantissa = static_cast<T>( std::round(significant*shiftMantissa) );
24 }
25}
26
27
28template <class T>
30 const char& exponent,
31 const T& mantissa
32) {
33 return std::ldexp(mantissa,exponent);
34}
void decompose(const double &value, char &exponent, T &mantissa)
Takes a double and returns the exponent and the mantissa.
double compose(const char &exponent, const T &mantissa)