STD :: FPCLASSIFY的错误结果使用Valgrind进行长时间的双重双重结果

Wrong result of std::fpclassify for long double using Valgrind

本文关键字:结果 长时间 FPCLASSIFY 错误 STD Valgrind      更新时间:2023-10-16

i以奇怪的行为碰到。在此程序中,我尝试检查是浮点值等于零:

#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)

是错误还是预期的行为?

upd
此处可用的程序的源代码。
构建选项: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 ... model name : Intel(R) Core(TM) i7-4700MQ CPU @ 2.40GHz ...

这是带有Valgrind的预期行为 - 已知的限制。内部长双重只用64位(双重(精度表示。

请参阅此处。