有人可以解释为什么以下代码会为test4变量生成与其他3相比不同的输出吗?我已经用gcc版本4.2.1和4.5.3(以及其他介于两者之间)检查过这个。
也许我错过了一些明显的东西,但它看起来非常简单......
#include <stdio.h>
#include <complex.h>
main()
{
double complex test1, test2, test3, test4;
test1 = 81141117.0;
test2 = 81141117.0 + I * 0;
test3 = 81141117 + I * 0.0;
test4 = 81141117 + I * 0;
printf("%ld + %ld I, %ld + %ld I, ", (long)creal(test1), (long)cimag(test1), (long)creal(test2), (long)cimag(test2));
printf("%ld + %ld I, %ld + %ld I\n", (long)creal(test3), (long)cimag(test3), (long)creal(test4), (long)cimag(test4));
}
输出:
81141117 + 0 I, 81141117 + 0 I
81141117 + 0 I, 81141120 + 0 I
似乎只有整数项的test4被提升为浮点而不是声明的double,并且四舍五入正在发挥作用。
答案 0 :(得分:4)
我在Mac OS X 10.7.5(自制GCC)上测试了GCC 4.7.1,系统提供了/usr/bin/gcc
和/usr/bin/clang
,它们都会产生相同的结果报告。
我同意您的分析,即当复数的两个术语都表示为整数时,必须转换为float
而不是double
。 C 2011标准包括以下示例:
¶24示例1如果
<complex.h>
已#include
d,则声明int i = 3.5; double complex c = 5 + 3 * I;
使用值3和c定义和初始化i,值为5. 0 + i3。 0
这清楚地表明您应该能够编写整数表达式并获得有效的double complex
(尽管示例是非规范的)。但是,它没有说明在转换为float
之前是否应将整数值转换为double
,但语言中没有其他地方可以自动执行此操作(显然,您可以强制它),所以它不太可能是预期的解释。
总的来说,我认为这可能是一个可以向海湾合作委员会小组报告的错误(这不是我主张做的事情)。如果您运行gcc --help
,则输出结束消息:
For bug reporting instructions, please see:
<http://gcc.gnu.org/bugs.html>
我会扩展您的示例,如下所示(或者,我确实扩展了您的示例,如下所示):
#include <stdio.h>
#include <complex.h>
int main(void)
{
double complex test1, test2, test3, test4, test5, test6, test7, test8;
test1 = 81141117.0;
test2 = 81141117.0 + I * 0;
test3 = 81141117 + I * 0.0;
test4 = 81141117 + I * 0;
test5 = (float)81141117 + I * 0;
test6 = 81141117 + I * (float)0;
test7 = 81141117.F + I * 0;
test8 = 81141117 + I * 0.F;
printf("%ld + %ld I, %ld + %ld I\n", (long)creal(test1), (long)cimag(test1), (long)creal(test2), (long)cimag(test2));
printf("%ld + %ld I, %ld + %ld I\n", (long)creal(test3), (long)cimag(test3), (long)creal(test4), (long)cimag(test4));
printf("%ld + %ld I, %ld + %ld I\n", (long)creal(test5), (long)cimag(test5), (long)creal(test6), (long)cimag(test6));
printf("%ld + %ld I, %ld + %ld I\n", (long)creal(test7), (long)cimag(test7), (long)creal(test8), (long)cimag(test8));
}
我得到的输出是:
81141117 + 0 I, 81141117 + 0 I
81141117 + 0 I, 81141120 + 0 I
81141120 + 0 I, 81141120 + 0 I
81141120 + 0 I, 81141120 + 0 I
正如您所看到的,最后两行,其中有明确的(float)
强制转换或显式浮点常量,产生与有问题的行相同的结果(但值是合法的)。
您可能(或可能不想)尝试使用CMPLX
,CMPLXF
(以及可能CMPLXL
)宏;我只会将它们添加到报告的示例中,如果它们为某些“有趣的”定义产生“有趣”的值。
编译器版本号:
$ /usr/gcc/v4.7.1/bin/gcc --version
gcc (GCC) 4.7.1
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ /usr/bin/gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ clang --version
Apple clang version 4.1 (tags/Apple/clang-421.11.65) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
$