c++ - 使用 Valgrind 的 std::fpclassify for long double 的错误结果

标签 c++ gcc valgrind

我遇到了奇怪的行为。在这个程序中,我尝试检查浮点值是否为零:

#include <cstdlib>
#include <cmath>
#include <iostream>

int main ()
{
    float       fa (0), fb (0);
    double      da (0), db (0);
    long double la (0), lb (0);

    cout << "Float:       " << (FP_ZERO == fpclassify (fa - fb) ) << endl;
    cout << "Double:      " << (FP_ZERO == fpclassify (da - db) ) << endl;
    cout << "Long double: " << (FP_ZERO == fpclassify (la - lb) ) << endl;
    cout << "Float:       " << (FP_ZERO == fpclassify (fa - 42) ) << endl;
    cout << "Double:      " << (FP_ZERO == fpclassify (da - 42) ) << endl;
    cout << "Long double: " << (FP_ZERO == fpclassify (la - 42) ) << endl;

    return EXIT_SUCCESS;
}

程序的结果是可预测的:

$ ./llvlg 
Float:       1
Double:      1
Long double: 1
Float:       0
Double:      0
Long double: 0

但是,如果我通过 Valgrind 启动程序,长双零的结果将是错误的:

$ valgrind ./llvlg 
==7521== Memcheck, a memory error detector
==7521== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==7521== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==7521== Command: ./llvlg
==7521== 
Float:       1
Double:      1
Long double: 0
Float:       0
Double:      0
Long double: 0
==7521== 
==7521== HEAP SUMMARY:
==7521==     in use at exit: 0 bytes in 0 blocks
==7521==   total heap usage: 2 allocs, 2 frees, 73,728 bytes allocated
==7521== 
==7521== All heap blocks were freed -- no leaks are possible
==7521== 
==7521== For counts of detected and suppressed errors, rerun with: -v
==7521== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

这是错误还是预期的行为?

更新:
可用的程序源代码 here .
构建选项:g++ main.cpp -O0 -o llvlg
我的环境:

  • $ uname -a Linux tiptop 4.8.0-53-generic#56-Ubuntu SMP Tue May 16 00:23:44 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

  • $ gcc --version gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005

  • $ valgrind --version valgrind-3.12.0.SVN

  • $ cat/proc/cpuinfo ... 型号名称:Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz ...

最佳答案

这是 Valgrind 的预期行为 - 一个已知的限制。内部 long double 仅以 64 位(双)精度表示。

参见 here .

关于c++ - 使用 Valgrind 的 std::fpclassify for long double 的错误结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44316523/

相关文章:

c++ - 围绕显式模板实例化的困惑

c++ - Mac C/C++ 上的 Eclipse

c - valgrind 发现无效写入和读取的数量令人难以置信

c++ - 为什么Callgrind使原子负载永无止境

c++ - 使用 Clang 编译 Boost 时出错

c++ - MSYS2 上的 gcc 8.2.1 出现 Eigen 编译错误

c++ - "call"看似跳入自身的指令

c - while 循环条件中大小为 1 的读取无效

c++ - 具有 std::string 属性的默认移动构造函数行为

c - 枚举类型值未正确比较