There are special formats (base-128) designed for transmitting integers used in protobufs and elsewhere. They're advantageous when most the integers are small (they need a single byte for smallest numbers and may waste one byte for others).
I wonder if there's something similar for floating point numbers under the assumption that most of them are actually small integers?
To address the answer by Alice: I was thinking about something like
void putCompressedDouble(double x) {
int n = (int) x;
boolean fits = (n == x);
putBoolean(fits);
if (fits) {
putCompressedInt(n);
} else {
putUncompressedLong(Double.doubleToLongBits(x));
}
}
This works (except for the negative zero, which I really don't care about), but it's wasteful in case of fits == true
.