Re: Machine epsilon: conclusion




"jacob navia" <jacob@xxxxxxxxxxxxxxxx> ha scritto nel messaggio news:468660fe$0$5084$ba4acef3@xxxxxxxxxxxxxxxxx
Following the discussion about the machine epsilon, I have modified the
text as follows. I thank all people that contributed. It was interesting, and I surely learned stuff I did not consider before.
--------------------------------------------------------------
The machine epsilon
-------------------
The standard defines for each floating point representation (float, double, long double) the difference between 1 and the least
value greater than 1 that is representable in the given floating point type. In IEEE754 representation this number has an exponent
value of the bias, and a fraction of 1.
Have you already explained that "a fraction of 1" actually means
"a significand of 1.00000000000000000000000000000000000000000000000000001" ?

In hexadecimal notation this would be
0x1p-52
Another way to define this quantity in terms of the mathematical functions of the C library is:

DBL_EPSILON = nextafter(1.0,2.0) - 1.0;

For the different representations we have in the standard header <float.h>:

#define FLT_EPSILON 1.19209290e-07F // float
#define DBL_EPSILON 2.2204460492503131e-16 // double
#define LDBL_EPSILON 1.084202172485504434007452e-19L //long double
// qfloat epsilon truncated so that it fits in this page...
#define QFLT_EPSILON 1.09003771904865842969737513593110651 ... E-106

These definitions (except the qfloat part) are part of the C99 ANSI standard. For the standard types (float, double and long
double) they should always exist in other compilers.
Add: "For the qfloat type, it invades the user name space, since I
was too lazy to call it _QFLT_EPSILON." Also you should explain by
which magic it doesn't have type double. (If it is because of the
capital E, I don't think anything in the Standard allows you to
treat it differently from a lowercase e).

Here is a program that will find out the machine epsilon for a given floating point representation.

#include <stdio.h>
int main(void)
{
double float_radix=2.0;
Who says thay?
double inverse_radix = 1.0/float_radix;
double machine_precision = 1.0;
double temp = 1.0 + machine_precision;

while (temp != 1.0) {
machine_precision *= inverse_radix;
temp = 1.0 + machine_precision ;
printf("%.17g\n",machine_precision);
}
return 0;
}
Exercises:
1: Explain why in the above program, the value of DBL_EPSILON is not the last number but the number before.
2: An alternative version of the above program is:
#include <stdio.h>
int main(void)
{
volatile double oneplus = 2, epsilon = 1;
while (1 + epsilon/2 > 1) {
epsilon /= 2;
oneplus = 1 + epsilon;
}
epsilon = oneplus - 1;
printf("DBL_EPSILON is %g\n", epsilon);
return 0;
}

Explain why this program prints

DBL_EPSILON is 0

in lcc-win32.
Because its author tested it with MSVC. :-)
(It also prints DBL_EPSILON is 0 with gcc -ansi -pedantic and with gcc -std=c99 -pedantic. The reason is that the left operand of <
is stored in a register which is wider than double, so epsilon gets
halved several other times.)
#include <stdio.h>
#include <stdio.h>
int main(void)
{
volatile double oneplus = 2, epsilon = 1;
while ((oneplus = 1 + epsilon/2) > 1) {
epsilon /= 2;
}
oneplus = 1 + epsilon;
epsilon = oneplus - 1;
printf("DBL_EPSILON is %g\n", epsilon);
return 0;
}
works, provided FLT_RADIX is a power of two.


.



Relevant Pages

  • Re: The machine epsilon
    ... For the different representations ... we have in the standard header: ... FP epsilon if 1+x is the smallest representable number greater ... What it does have, though, in common with other IEEE ...
    (comp.lang.c)
  • Re: Float comparison
    ... he's not free to use a definition that's inconsistent with the one ... He's said several times in response to direct questions that EPSILON ... but it's a poor choice given the existing standard *_EPSILON ... consistent and consistent with the C standard. ...
    (comp.lang.c)
  • Re: Float comparison
    ... snip ... ... You yourself even mentioned the standard definition ... than 1 that is representable in the given floating ... that EPSILON is a real value rather than a fp-object-value, ...
    (comp.lang.c)
  • Machine epsilon: conclusion
    ... Following the discussion about the machine epsilon, ... The standard defines for each floating point representation the difference between 1 and the least value greater than 1 that is representable in the given floating point type. ...
    (comp.lang.c)
  • Re: epsilon intrinsic
    ... a qualified interpretation of the F90 ... Standard that puts this Epsilon discussion in proper perspective. ... I am not alone in valuing your Standards excellence, Richard. ...
    (comp.lang.fortran)