Hi everyone,
Does anyone know what is the exact value of "e" that the C
language uses in the log() function. I ask as I am coparing the
respective function (C vs C#) to see if there are any differences.
I guess it's understandable, although sad, that you don't know that your
question and the goal you are pursuing are meaningless. But, in any case:
/* You can modify the code below to run with your implementation.
* If you just want to see what one typical implementation does,
* check the output at the end of the code */
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <stdlib.h>
#define HAVE_LOGF
/* #define HAVE_LOGL */
const long double e = 2.7182818284590452353602874713526624977572L;
int main(void)
{
float xf = e, yf;
double xd = e, yd;
long double xld = e, yld;
int hexfracdigits;
if (FLT_RADIX != 2) {
fprintf(stderr,
"If your implementation uses a base other than 2,\n"
"I'll let you play with it yourself. The rest of my\n"
"code assumes FLT_RADIX == 2.\n");
exit(EXIT_FAILURE);
}
printf("There is, of course, no value that C uses for\n"
"e, since that is an implementation detail\n"
"for whatever library your program is linked\n"
"to. The library need not be written in C,\n"
"and can use whatever tricks it wants.\n\n"
"Here are some approximations for e on this implementation.\n"
"First we'll check what how good it could get.\n\n");
hexfracdigits = FLT_MANT_DIG / 4;
printf("FLT_DIG = %d, FLT_MANT_DIG/4 = %d\n"
"a float value for e might be\n"
"%.*e (decimal) %.*a (hex digits * power of 2)\n\n",
FLT_DIG, hexfracdigits, FLT_DIG, xf, hexfracdigits, xf);
hexfracdigits = DBL_MANT_DIG / 4;
printf("DBL_DIG = %d, DBL_MANT_DIG/4 = %d\n"
"a double value for e might be\n"
"%.*e (decimal) %.*a\n\n",
DBL_DIG, hexfracdigits, DBL_DIG, xd, hexfracdigits, xd);
hexfracdigits = LDBL_MANT_DIG / 4;
printf("LDBL_DIG = %d, LDBL_MANT_DIG/4 = %d\n"
"a long double value for e might be\n"
"%.*Le (decimal) %.*La\n\n",
LDBL_DIG, hexfracdigits, LDBL_DIG, xld, hexfracdigits, xld);
printf("Now let's look at the exp() family for some estimate\n"
"of what this implementation actually uses.\n\n");
#if defined(HAVE_LOGF)
printf("This implementation has the float version, so\n"
"let's see what expf() returns.\n");
xf = expf(1);
yf = logf(xf);
hexfracdigits = FLT_MANT_DIG / 4;
printf("xf = expf(1), %.*e %.*a\n"
"logf(xf) = %.*e %.*a\n\n",
FLT_DIG, xf, hexfracdigits, xf, FLT_DIG, yf, hexfracdigits,
yf);
#endif
printf("Now let's check the double version.\n");
xd = exp(1);
yd = log(xd);
hexfracdigits = DBL_MANT_DIG / 4;
printf("xd = exp(1), %.*e %.*a\n"
"log(xd) = %.*e %.*a\n\n",
DBL_DIG, xd, hexfracdigits, xd, DBL_DIG, yd, hexfracdigits,
yd);
#if defined(HAVE_LOGL)
printf("This implementation has the long double version, so\n"
"let's see what expl() returns.\n");
xld = expl(1);
yld = logl(xld);
hexfracdigits = LDBL_MANT_DIG / 4;
printf("xd = expl(1), %.*Le %.*La\n"
"logl(xdl) = %.*Le %.*La\n\n", LDBL_DIG, xld, hexfracdigits,
xld, LDBL_DIG, yld, hexfracdigits, yld);
#endif
return 0;
}
[Output]
There is, of course, no value that C uses for
e, since that is an implementation detail
for whatever library your program is linked
to. The library need not be written in C,
and can use whatever tricks it wants.
Here are some approximations for e on this implementation.
First we'll check what how good it could get.
FLT_DIG = 6, FLT_MANT_DIG/4 = 6
a double value for e might be
2.718282e+00 (decimal) 0xa.df8540p-2 (hex digits * power of 2)
DBL_DIG = 15, DBL_MANT_DIG/4 = 13
a float value for e might be
2.718281828459045e+00 (decimal) 0xa.df85458a2bb48p-2
LDBL_DIG = 18, LDBL_MANT_DIG/4 = 16
a long double value for e might be
2.718281828459045235e+00 (decimal) 0xa.df85458a2bb4a9b0p-2
Now let's look at the exp() family for some estimate
of what this implementation actually uses.
This implementation has the float version, so
let's see what expf() returns.
xf = expf(1), 2.718282e+00 0xa.df8540p-2
logf(xf) = 9.999999e-01 0xf.fffff0p-4
Now let's check the double version.
xd = exp(1), 2.718281828459045e+00 0xa.df85458a2bb48p-2
log(xd) = 1.000000000000000e+00 0x8.0000000000000p-3