除以复数< double>在clang ++与g ++中 [英] Division by complex<double> in clang++ versus g++
问题描述
当我用g ++(4.8.1或4.9.0)或clang ++(3.4)编译以下代码时,得到不同的输出。
When I compile the following code with g++ (4.8.1 or 4.9.0) or clang++ (3.4) I get different outputs.
#include <iostream>
#include <complex>
int main() {
std::complex<double> c = {1.e-162,0};
std::cout << 1.0/c << std::endl;
return 0;
}
g ++:
(1e+162,0)
clang ++: p>
clang++:
(inf,-nan)
这是c中的错误吗?
更新:
感谢您的回答!我报告了该错误: http://llvm.org/bugs/show_bug.cgi?id= 19820
Thank you for your answers! I reported the bug: http://llvm.org/bugs/show_bug.cgi?id=19820
推荐答案
标准说明中 [complex.numbers]
(26.4 / 3):
The standard says in [complex.numbers]
(26.4/3):
如果函数的结果未在数学上定义或不在
中,则范围为其类型的可表示值,其行为是
未定义。
If the result of a function is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.
对于复杂的对象应如何实现除法没有具体说明数字。仅在 [complex.member.ops]
中说:
There are no specifics on how division should be implemented for complex numbers. Only in [complex.member.ops]
it says:
complex<T>& operator/=(const complex<T>& rhs);
效果:将 rhs
的复数值除复数值 * this
,并将商存储在 * this
中。
返回: * this
。
Effects: Divides the complex value rhs
into the complex value *this
and stores the quotient in *this
.
Returns: *this
.
和 [complex.ops]
:
template<class T> complex<T> operator/(const T& lhs, const complex<T>& rhs);
返回: complex< T>(lhs)/ = rhs
。
与 1相反.e-162
是 1.e + 162
,并且此数字在 double <的可表示值范围内/ code>,其行为已得到很好的定义。
As the inverse of 1.e-162
is 1.e+162
and this number is in the range of representable values for a double
, the behavior is well defined.
因此,gcc正确了,而clang有一个错误。
Thus gcc gets it right and clang has a bug.
这篇关于除以复数< double>在clang ++与g ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!