Is there a ready-to-go function in C++?
Perhaps. If not, it is easy enough to form a function to assigned a sequence number to each double
value.
Assuming matching FP/integer for endian & size and typical FP layout like double64, the below is valid -INF
to INF
.
// Return a sequence number for each `double` value.
// Numerically sequential `double` values will have successive (+1) sequence numbers.
uint64_t double_sequence(double x) {
uint64_t u64;
memcpy(&u64, &x, sizeof u64);
if (u64 & 0x8000000000000000) {
u64 ^= 0x8000000000000000;
return 0x8000000000000000 - u64;
}
return u64 + 0x8000000000000000;
}
Is there a function to retrieve the number of available distinct values within a range?
Simply subtract the sequence numbers. +1 or -1 depending on if an open or closed range.
double_sequence(1.0) - double_sequence(0.0) + 1 --> 0x3ff0000000000001
double_sequence(48.0) - double_sequence(-48.0) + 1 --> 0x8090000000000001
Notes:
Keep in mind that FP are logarithmically distributed overall and linear within powers of 2.
For about half of all FP, |x| < 1.0
.
There are as many FP numbers 0.5 to 1.0 as between 16.0 to 32.0.
There are over twice as many double
in the [-48.0 ... 48.0]
versus [0.0 ... 1.0]
range, primarily due to negative values.