Originally Posted by
flp1969
That's interesting, Malcom! I completely forgot about linear interpolation!
Code:
#include <stdlib.h>
#include <float.h>
#define lerp(min_, max_, norm) \
( (min_)*(1.0 - (norm)) + (max_)*(norm) )
double symmetrical_rand ( void )
{
double d;
d = ( double ) rand() / RAND_MAX;
return lerp ( -1.0 + DBL_EPSILON, 1.0 - DBL_EPSILON, d );
}
There is one more number between 1.0-DBL_EPSILON and 1.0:
Code:
#include <float.h>
#include <stdio.h>
void printhex(void *x, size_t s) {
for(size_t i = 0; i < s; i++)
printf("%02X ",((unsigned char *)x)[i]);
putchar('\n');
}
int main(int argc, char *argv[]) {
double a = 1.0;
double b = 1.0-DBL_EPSILON/2;
double c = 1.0-DBL_EPSILON;
printf("%e\n", DBL_EPSILON);
printf("%32.30e\n", a);
printf("%32.30e\n", b);
printf("%32.30e\n", c);
printhex(&a, sizeof(a));
printhex(&b, sizeof(b));
printhex(&c, sizeof(c));
}
Gives the following output:
Code:
2.220446e-16
1.000000000000000000000000000000e+00
9.999999999999998889776975374843e-01
9.999999999999997779553950749687e-01
00 00 00 00 00 00 F0 3F
FF FF FF FF FF FF EF 3F
FE FF FF FF FF FF EF 3F