相关疑难解决方法(0)

浮点数学是否破碎?

请考虑以下代码:

0.1 + 0.2 == 0.3  ->  false
Run Code Online (Sandbox Code Playgroud)
0.1 + 0.2         ->  0.30000000000000004
Run Code Online (Sandbox Code Playgroud)

为什么会出现这些不准确之处?

language-agnostic math floating-point floating-accuracy

2798
推荐指数
28
解决办法
28万
查看次数

如何在单元测试中避免浮点舍入错误?

我正在尝试为一些简单的向量数学函数编写单元测试,这些函数在单精度浮点数的数组上运行.这些函数使用SSE内在函数,当在32位系统上运行测试时,我会得到误报(至少我认为)(测试通过64位).当操作通过数组运行时,我累积越来越多的舍入错误.这是一个单元测试代码和输出的片段(我的实际问题如下):

测试设置:

static const int N = 1024;
static const float MSCALAR = 42.42f;

static void setup(void) {
    input = _mm_malloc(sizeof(*input) * N, 16);
    ainput = _mm_malloc(sizeof(*ainput) * N, 16);
    output = _mm_malloc(sizeof(*output) * N, 16);
    expected = _mm_malloc(sizeof(*expected) * N, 16);

    memset(output, 0, sizeof(*output) * N);

    for (int i = 0; i < N; i++) {
        input[i] = i * 0.4f;
        ainput[i] = i * 2.1f;
        expected[i] = (input[i] * MSCALAR) + ainput[i];
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我的主要测试代码调用要测试的函数(用于生成expected数组的相同计算)并根据expected …

c floating-point unit-testing sse floating-accuracy

1
推荐指数
1
解决办法
1960
查看次数