56 # define MAX_MANTISSA_BYTES ((DBL_MANT_DIG + 7 + 7) / 8) 57 # define MAX_EXP ((DBL_MAX_EXP + 1) / 8) 58 # define MAX_MANTISSA (1 << (DBL_MAX_EXP & 7)) 60 # define MAX_MANTISSA_BYTES ((DBL_MANT_DIG + 1 + 1) / 2) 61 # define MAX_EXP ((DBL_MAX_EXP + 1) / 2) 62 # define MAX_MANTISSA (1 << ((DBL_MAX_EXP & 1) * 4)) 64 # error FLT_RADIX is a value not currently handled (not 2 or 16) 74 v = scalbn(v, (exp & 7) + 1);
76 v = ldexp(v, (exp & 7) + 1);
97 bool negative = (v < 0.0);
105 if (exp <= 6 && exp >= -7) {
106 unsigned char b =
static_cast<unsigned char>(exp + 7);
107 if (negative) b |=
static_cast<unsigned char>(0x80);
110 if (exp >= -128 && exp < 127) {
111 result += negative ? char(0x8e) : char(0x0e);
112 result += char(exp + 128);
114 if (exp < -32768 || exp > 32767) {
117 result += negative ? char(0x8f) : char(0x0f);
118 result += char(
unsigned(exp + 32768) & 0xff);
119 result += char(
unsigned(exp + 32768) >> 8);
123 int maxbytes = min(MAX_MANTISSA_BYTES, 8);
125 size_t n = result.size();
127 unsigned char byte =
static_cast<unsigned char>(v);
128 result += char(byte);
131 }
while (v != 0.0 && --maxbytes);
133 n = result.size() - n;
136 result[0] =
static_cast<unsigned char>(result[0] | ((n - 1) << 4));
147 unsigned char first = *(*p)++;
148 if (first == 0 && *(*p) == 0) {
153 bool negative = (first & 0x80) != 0;
154 size_t mantissa_len = ((first >> 4) & 0x07) + 1;
156 int exp = first & 0x0f;
158 int bigexp =
static_cast<unsigned char>(*(*p)++);
163 exp = bigexp | (
static_cast<unsigned char>(*(*p)++) << 8);
172 if (
size_t(end - *p) < mantissa_len) {
178 static double dbl_max_mantissa = DBL_MAX;
181 if (exp > dbl_max_exp ||
182 (exp == dbl_max_exp &&
183 double(static_cast<unsigned char>((*p)[-1])) > dbl_max_mantissa)) {
189 while (mantissa_len--) {
191 v += double(static_cast<unsigned char>(*--q));
195 if (exp) v = scalbn(v, exp * 8);
196 #elif FLT_RADIX == 16 197 if (exp) v = scalbn(v, exp * 2);
199 if (exp) v = ldexp(v, exp * 8);
209 if (negative) v = -v;
static int base256ify_double(double &v)
Hierarchy of classes which Xapian can throw as exceptions.
functions to serialise and unserialise a double
double unserialise_double(const char **p, const char *end)
Unserialise a double serialised by serialise_double.
Indicates an error in the std::string serialisation of an object.
InternalError indicates a runtime problem of some sort.
std::string serialise_double(double v)
Serialise a double to a string.
Various assertion macros.