I want to split a range with double borders into N>=2 equal or near equal intervals.
I found a suitable function in GNU Scientific Library:
make_uniform (double range[], size_t n, double xmin, double xmax)
{
size_t i;
for (i = 0; i <= n; i++)
{
double f1 = ((double) (n-i) / (double) n);
double f2 = ((double) i / (double) n);
range[i] = f1 * xmin + f2 * xmax;
}
}
However, when
xmin = 241141 (binary 0x410D6FA800000000)
xmax = 241141.0000000001 (binary 0x410D6FA800000003)
N = 3
the function produces
[0x410D6FA800000000,
0x410D6FA800000000,
0x410D6FA800000002,
0x410D6FA800000003]
instead of desired
[0x410D6FA800000000,
0x410D6FA800000001,
0x410D6FA800000002,
0x410D6FA800000003]
How achieve uniformity without resorting to long arithmetics (i already have a long arithmetics solution but it is ugly and slow)? Bit twiddling and x86 (x86-64, so no extended precision) assembler routines are acceptable.
UPDATE:
General solution is needed, without premise that xmin, xmax have equal exponent and sign:
xminandxmaxmay be of any value except infinity and NaN (possibly also excluding denormalized values for sake of simplicity).xmin < xmax(1<<11)-1>=N>=2- i'm ready for major (in 2-3 orders) performance loss