cpp: how to compute epsMachine in -Ofast

51 Views Asked by At

How do I compute the correct value for the machine round-off unit of a given typename in presence of arbitrary compiler optimization flags? If impossible, let's restrict the set of possible compiler flags to all optimization flags set by -Ofast.

Background

The following code

template<typename Tfloat>
Tfloat get_eps_mach(){
    Tfloat x=1, q=0.5, u=q;
    while( x!=(x+q*u) ){ u*=q; }
    return u;
}

will return ridiculously small numbers for the machine round-off unit u when the compiler option -Ofast is used. I understand why (it is due to -ffast-math, which grants the compiler permission to manipulate the comparison and incrementation), but I seek a way so that I can compute u for arbitrary numbers correctly. Notice: There is no personal flavor in what the term correct means here, because the value u is formally well-defined from the bit representation of the number type (mantissa length in particular).

While in my programming model that bit representation shall be black-box and inattainable and I do even not want to assume that the number type uses a mantissa or whatever, I still insist to hold an agnostic function that will return the correct value of u. (Hence it would also be a pointless "solution" to propose overloading the function for every common number type. Reason for my interest are:

  1. that subsequent parameters (e.g., optimal truncations, optimal perturbations, reasonable tolerances, etc) will be computed as formulas of u.
  2. that I do not want to worry that any future or user-defined number types will be needed at any point in the future, for which then the get_eps_mach function may not return the correct value, regardless what compiler options are used whatsoever.
0

There are 0 best solutions below