c++ - How to deal with excess precision in floating-point computations? -


In my numerical simulation, I have the same code as the following snippet

  double x; Do {x = / * some calculation * /; } While (x & lt; = 0.0); / * Some algorithms require X to 0 * /  

to place some precise compilers (like GCC) on some platforms (for example, Linux, X77), possible That X is calculated in more than double precision ("with additional precision") ( updates : when I talk about precision here, my exact / and / Category means.) In these circumstances, it is assumed that the comparison ( x ) returns False even if the goal for next time precise Double X, then it becomes zero. (And there is no guarantee that there is no goal at an arbitrary point at X time.)

is there any way to perform

  • that is in comparison

    • is portable,
    • works in code which is underlined,
    • it does not have any effect and
    • does not Has not some arbitrary boundary (0, EPS) excluded?

    I ( x & lt; std :: numeric_limits & lt; double & gt;: denorm_min () ) but it seems that SSE2 with math The loop is slowed down while working. (I know that a calculation can be deformed slowly, but I did not expect them to just roam and compare.)

    Update: is an option Using the x force variable in memory, before typing

     } while comparing (* ((volatile Double *) & amp; x) & lt; = 0.0);  

    However, depending on the application and optimization implemented by the compiler, this solution can also offer a noticeable overhead.

    Update: The problem with any tolerance is that it is quite arbitrary, i.e. it depends on a specific application or context. I prefer to compare without more precision, so that I do not have to make any additional assumptions in the documentation of my library functions or apply some arbitrary epistles.

  • (double) x) & lt; = 0.0 Em> work - at least according to the standard.

    C99: TC3, 5.2.4.2.2 §8:

    Except assignments and artists (all additional ranges and exact), common arithmetic conversions and floating constants Under floating operands and values, the values ​​of operation are evaluated for a format, whose range and accuracy can be necessary accordingly. [...]


    If you are using GCC on x86, you can see the flag -mpc32 , -mpc64 < / Code> and -mpc80 to set the accuracy of floating-point operations for stable, double and extended double precision.


    Comments