了解 heisenbug 示例:寄存器与主内存的不同精度 [英] Understanding heisenbug example: different precision in registers vs main memory

查看:21
本文介绍了了解 heisenbug 示例:寄存器与主内存的不同精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了关于 heisenbug 的 wiki 页面,但不明白这个例子.能有没有人详细解释一下?

I read the wiki page about heisenbug, but don't understand this example. Can anyone explain it in detail?

一个常见的例子heisenbug 是程序编译时出现的错误优化编译器,但不是在没有编译相同程序时优化(通常用于使用调试器检查它).调试时,优化程序通常会保留的值寄存器通常被推送到主内存.这可能会影响,例如,浮点比较的结果,因为内存中的值可能更小范围和精度高于寄存器中的值.

One common example of a heisenbug is a bug that appears when the program is compiled with an optimizing compiler, but not when the same program is compiled without optimization (as is often done for the purpose of examining it with a debugger). While debugging, values that an optimized program would normally keep in registers are often pushed to main memory. This may affect, for instance, the result of floating-point comparisons, since the value in memory may have smaller range and accuracy than the value in the register.

推荐答案

这是最近发布的一个具体示例:

Here's a concrete example recently posted:

无限循环 heisenbug:如果我添加它就会退出打印输出

这是一个非常好的样本,因为我们都可以复制它:http://ideone.com/rjY5kQ

It's a really nice specimen because we can all reproduce it: http://ideone.com/rjY5kQ

这些错误非常依赖于平台的非常精确的功能,以至于人们也发现它们很难重现.

These bugs are so dependent on very precise features of the platform that people also find them very difficult to reproduce.

在这种情况下,当省略打印输出"时,程序会在 CPU 寄存器内执行高精度比较(高于存储在 double 中的).但是为了打印出值,编译器决定将结果移动到主内存,这会导致精度的隐式截断.当它使用该截断值进行比较时,它就成功了.

In this case when the 'print-out' is omitted the program performs a high precision comparison inside the CPU registers (higher than stored in a double). But to print out the value the compiler decides to move the result to main memory which results in an implicit truncation of the precision. When it uses that truncated value for the comparison it succeeds.

#include <iostream>
#include <cmath>

double up = 19.0 + (61.0/125.0);
double down = -32.0 - (2.0/3.0);
double rectangle = (up - down) * 8.0;

double f(double x) {
    return (pow(x, 4.0)/500.0) - (pow(x, 2.0)/200.0) - 0.012;
}

double g(double x) {
    return -(pow(x, 3.0)/30.0) + (x/20.0) + (1.0/6.0);
}

double area_upper(double x, double step) {
    return (((up - f(x)) + (up - f(x + step))) * step) / 2.0;
}

double area_lower(double x, double step) {
    return (((g(x) - down) + (g(x + step) - down)) * step) / 2.0;
}

double area(double x, double step) {
    return area_upper(x, step) + area_lower(x, step);
}

int main() {
    double current = 0, last = 0, step = 1.0;

    do {
        last = current;
        step /= 10.0;
        current = 0;

        for(double x = 2.0; x < 10.0; x += step) current += area(x, step);

        current = rectangle - current;
        current = round(current * 1000.0) / 1000.0;
        //std::cout << current << std::endl; //<-- COMMENT BACK IN TO "FIX" BUG
    } while(current != last);

    std::cout << current << std::endl;
    return 0;
}

已验证的错误和修复仍然显示:20-Feb-17

Verified bug and fix still exhibit: 20-Feb-17

这篇关于了解 heisenbug 示例:寄存器与主内存的不同精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆