Just a small little program. Can not figure out what am I doing wrong.
#include <stdio.h>
#include <limits.h>
Note that said:
#include <float.h>
int main()
{
double max = FLT_MAX;
double sub = 16703.627681;
double result = max - sub;
printf("%f - %f = %f\n", max, sub, result);
return 0;
}
Output:
340282346638528859811704183484516925440.000000 - 16703.627681 =
340282346638528859811704183484516925440.000000
Any help would be highly appreciated.
/* The values FLT_EPSILON, DBL_EPSILON, and LDBL_EPSILON are defined
to be, for each type, the smallest x > 0.0 such that 1.0+x > x.
These suggests (but does not guarantee) that for a value y > 0.0,
y*(1.0+x) is the smallest value larger than y. These values are
closely linked to the number of significant bits in the
representation of the type. There is a limit FLT_DIG, DBL_DIG, or
LDBL_DIG representing the guaranteed number of (decimal) digits of
precision. Check the following program. */
#include <stdio.h>
#include <float.h>
#include <math.h>
int main()
{
double max = FLT_MAX;
double sub = 16703.627681;
double diff;
printf
("The following values are all dependent on the implementation.\n"
"They may or may not be similar to the values for your "
"implementation.\n\n");
printf("FLT_DIG = %d\n", FLT_DIG);
printf("FLT_MAX = %.*g, FLT_EPSILON = %.*g,\n"
"FLT_MAX - FLT_MAX/(1.0+FLT_EPSILON) = %.*g,\n"
"log10(1.0/FLT_EPSILON - 1.0) = %.*g\n\n",
FLT_DIG, FLT_MAX,
FLT_DIG, FLT_EPSILON,
FLT_DIG, FLT_MAX - FLT_MAX / (1.0 + FLT_EPSILON),
FLT_DIG, log10(1.0 / FLT_EPSILON - 1.0));
printf("DBL_DIG = %d\n", DBL_DIG);
printf("DBL_MAX = %.*g, DBL_EPSILON = %.*g,\n"
"DBL_MAX - DBL_MAX/(1.0+DBL_EPSILON) = %.*g,\n"
"log10(1.0/DBL_EPSILON - 1.0) = %.*g\n\n",
DBL_DIG, DBL_MAX,
DBL_DIG, DBL_EPSILON,
DBL_DIG, DBL_MAX - DBL_MAX / (1.0 + DBL_EPSILON),
DBL_DIG, log10(1.0 / DBL_EPSILON - 1.0));
printf("LDBL_DIG = %d\n", LDBL_DIG);
printf("LDBL_MAX = %.*Lg, LDBL_EPSILON = %.*Lg,\n"
"LDBL_MAX - LDBL_MAX/(1.0+LDBL_EPSILON) = %.*Lg,\n"
"log10(1.0/LDBL_EPSILON - 1.0) = %.*g\n\n",
LDBL_DIG, LDBL_MAX,
LDBL_DIG, LDBL_EPSILON,
LDBL_DIG, LDBL_MAX - LDBL_MAX / (1.0 + LDBL_EPSILON),
DBL_DIG, log10(1.0 / LDBL_EPSILON - 1.0));
diff = max * (1. - 1. / (1.0 + DBL_EPSILON));
printf("max is a double with value %.*g.\n"
"We expect the next lower distinguishable double to\n"
" differ from max by about %.*g.\n"
"The original poster wanted to distinguish a value %.*g\n"
"less than max, but this value is only"
" %.*g * the (likely) smallest significant difference.\n",
DBL_DIG, max, DBL_DIG, diff, DBL_DIG, sub,
DBL_DIG, sub / diff);
return 0;
}
[Output]
The following values are all dependent on the implementation.
They may or may not be similar to the values for your implementation.
FLT_DIG = 6
FLT_MAX = 3.40282e+38, FLT_EPSILON = 1.19209e-07,
FLT_MAX - FLT_MAX/(1.0+FLT_EPSILON) = 4.05648e+31,
log10(1.0/FLT_EPSILON - 1.0) = 6.92369
DBL_DIG = 15
DBL_MAX = 1.79769313486232e+308, DBL_EPSILON = 2.22044604925031e-16,
DBL_MAX - DBL_MAX/(1.0+DBL_EPSILON) = 3.99168061906944e+292,
log10(1.0/DBL_EPSILON - 1.0) = 15.653559774527
LDBL_DIG = 18
LDBL_MAX = 1.18973149535723177e+4932, LDBL_EPSILON =
1.08420217248550443e-19,
LDBL_MAX - LDBL_MAX/(1.0+LDBL_EPSILON) = 1.28990947194073851e+4913,
log10(1.0/LDBL_EPSILON - 1.0) = 18.9648897268308
max is a double with value 3.40282346638529e+38.
We expect the next lower distinguishable double to
differ from max by about 7.55578592223147e+22.
The original poster wanted to distinguish a value 16703.627681
less than max, but this value is only 2.21070684809276e-19 * the
(likely) smallest significant difference.