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) 69 if (
rare(!isfinite(v))) {
75 v = v > 0 ? DBL_MAX : -DBL_MAX;
87 v = scalbn(v, (exp & 7) + 1);
89 v = ldexp(v, (exp & 7) + 1);
110 bool negative = (v < 0.0);
112 if (negative) v = -v;
118 if (exp <= 6 && exp >= -7) {
119 unsigned char b =
static_cast<unsigned char>(exp + 7);
120 if (negative) b |=
static_cast<unsigned char>(0x80);
123 if (exp >= -128 && exp < 127) {
124 result += negative ? char(0x8e) : char(0x0e);
125 result += char(exp + 128);
127 if (exp < -32768 || exp > 32767) {
130 result += negative ? char(0x8f) : char(0x0f);
131 result += char(
unsigned(exp + 32768) & 0xff);
132 result += char(
unsigned(exp + 32768) >> 8);
136 int maxbytes = min(MAX_MANTISSA_BYTES, 8);
138 size_t n = result.size();
140 unsigned char byte =
static_cast<unsigned char>(v);
141 result += char(byte);
144 }
while (v != 0.0 && --maxbytes);
146 n = result.size() - n;
149 result[0] =
static_cast<unsigned char>(result[0] | ((n - 1) << 4));
160 unsigned char first = *(*p)++;
161 if (first == 0 && *(*p) == 0) {
166 bool negative = (first & 0x80) != 0;
167 size_t mantissa_len = ((first >> 4) & 0x07) + 1;
169 int exp = first & 0x0f;
171 int bigexp =
static_cast<unsigned char>(*(*p)++);
176 exp = bigexp | (
static_cast<unsigned char>(*(*p)++) << 8);
185 if (
size_t(end - *p) < mantissa_len) {
191 static double dbl_max_mantissa = DBL_MAX;
194 if (exp > dbl_max_exp ||
195 (exp == dbl_max_exp &&
196 double(static_cast<unsigned char>((*p)[-1])) > dbl_max_mantissa)) {
202 while (mantissa_len--) {
204 v += double(static_cast<unsigned char>(*--q));
208 if (exp) v = scalbn(v, exp * 8);
209 #elif FLT_RADIX == 16 210 if (exp) v = scalbn(v, exp * 2);
212 if (exp) v = ldexp(v, exp * 8);
222 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.