是否有任何标准规定了输出应该是什么?
例如这段代码:
#include <stdio.h>
#include <math.h>
int main(int argc, char** argv) {
float a = INFINITY;
float b = -INFINITY;
float c = NAN;
printf("float %f %f %f\n", a, b, c);
printf("int %d %d %d\n", (int) a, (int) b, (int) c);
printf("uint %u %u %u\n", (unsigned int) a, (unsigned int) b, (unsigned int) c);
printf("lint %ld %ld %ld\n", (long int) a, (long int) b, (long int) b);
printf("luint %lu %lu %lu\n", (unsigned long int) a, (unsigned long int) b, (unsigned long int) c);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在gcc版本4.2.1(Apple Inc. build 5664)上编译目标:i686-apple-darwin10
输出:
$ gcc test.c && ./a.out
float inf -inf nan
int -2147483648 -2147483648 -2147483648
uint 0 0 0
lint -9223372036854775808 -9223372036854775808 -9223372036854775808
luint 0 9223372036854775808 9223372036854775808
Run Code Online (Sandbox Code Playgroud)
这很奇怪.(int)+ inf <0!?!
Mat*_*hen 15
保罗说,这是未定义的:
从§6.3.1.4开始:
6.3.1.4实际浮动和整数
当实际浮动类型的有限值转换为除_Bool之外的整数类型时,小数部分被丢弃(即,该值被截断为零).如果整数部分的值不能用整数类型表示,则行为未定义.50)
无穷大不是有限的,并且整数部分不能以整数形式表示,因此它是未定义的.
| 归档时间: |
|
| 查看次数: |
6842 次 |
| 最近记录: |