簡體   English   中英

浮點負無窮大的std :: exp在Visual C ++ 2013中為x64生成返回負無窮大

[英]std::exp of float negative infinity returns negative infinity for x64 builds in Visual C++ 2013

使用std::exp計算e^-infinity在使用infinity的float表示並使用Visual C ++ 2013構建x64二進制文件時返回-infinity 。我希望它返回0,這是Win32版本或版本所發生的情況std::exp需要double

以下代碼(構建為x64)演示了此問題。

#include <limits>
#include <iostream>

int main(const int argc, const char** argv) {
    std::cout << "exp of float -infinity: " << std::exp(-std::numeric_limits<float>::infinity()) << std::endl;
    std::cout << "exp of double -infinity: " << std::exp(-std::numeric_limits<double>::infinity()) << std::endl;
}

用於編譯的命令行選項(取自Visual Studio):

/GS /Wall /Gy /Zc:wchar_t /Zi /Gm- /Od /sdl /Fd"x64\Release\vc120.pdb" /fp:precise /D "_MBCS" /errorReport:prompt /WX /Zc:forScope /Gd /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Fp"x64\Release\NumericLimitsTest.pch" 

以上產出:

exp of float -infinity: -1.#INF
exp of double -infinity: 0

為什么會這樣?

我通常會說這是一些描述的錯誤,因為C ++ 11 F.9.3.1 C99的cmath功能,而C99在F.9.3.1中清楚地表明exp(−∞) returns +0 但是,請記住,該標准的附件中指出:

定義__STDC_IEC_559__的實現應符合本附件中的規范。

該宏似乎沒有在MSVC中的32位或64位模式中定義,所以它可能不是一個錯誤,你可能會運氣不好。 改變/fp:strict/fp:precise之間的浮點模式也不會使事情變得更好。

在所有情況下,結果似乎在32位和64位目標之間有所不同,並且基於僅表明expcompute the base-e exponential of x ,似乎沒有要求如何,它似乎沒問題。


如果您正在快速修復之后,使用pow函數似乎會生成正確的結果:

#define DBL_E 2.71828182845904523536
#define FLT_E 2.71828182845904523536f
std::cout
    << "exp of float -infinity: "
    << std::pow(FLT_E, -std::numeric_limits<float>::infinity())
    << std::endl;
std::cout
    << "exp of double -infinity: "
    << std::pow(DBL_E,-std::numeric_limits<double>::infinity())
    << std::endl;

無論你是否有64-bit/32-bitdebug/releasefp:precise/fp:strict ,這都會為兩條線路產生零,但是,無論是否有保證,我都說不出來。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM