Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function to retrieve the number of available distinct values within a range?

I'm using double precision floating point variables within an application I'm making.

I normalize some ranges of values. Going from (for example; I've many ranges) -48.0 to 48.0 to 0.0 to 1.0, using this simply function:

double ToNormalizedParam(double nonNormalizedValue, double min, double max, double shape) {
    return pow((nonNormalizedValue - min) / (max - min), 1.0 / shape);
}

I'd like to know the differences in available and distinct values mapping from a range to another.

Is there a ready-to-go function in C++? I've looked at numeric_limits, but I can't find anything useful.

like image 465
markzzz Avatar asked Dec 06 '25 07:12

markzzz


1 Answers

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.

like image 106
chux - Reinstate Monica Avatar answered Dec 07 '25 21:12

chux - Reinstate Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!