Han*_*Kan 3 c linux floating-point gcc ieee-754
我的编程环境是gcc版本5.4.0 20160609(Ubuntu 5.4.0-6ubuntu1~16.04.4)
我的代码如下:
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len){
int i;
for (i = 0; i<len; i++)
printf(" %.2x", start[i]);
printf("\n");
}
void show_float(float x){
show_bytes((byte_pointer)&x, sizeof(float));
}
int main(){
int y = 0xffffff;
float f = y;
show_float(f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并且机器给出了结果: 00 00 00 e0
根据IEEE 754,我认为这是不对的; 但我不知道为什么.虽然Windows 2013中的VS 2013中的相同代码给出了正确的答案:ff ff 7f 4b
gcc 5.4.0不采用IEEE 754吗?或者我的代码中有问题吗?
gcc 5.4.0不采用IEEE 754吗?
或者我的代码中有问题吗?
gcc 5.4.0和IEEE 754不是问题.当然代码不符合要求
通过重新排序函数,但相同的代码,我得到2个警告,并可以复制OP的输出 00 00 00 e0
警告:隐式声明函数'show_float'[-Wimplicit-function-declaration]
警告:'show_float'的冲突类型
我怀疑OP没有发布真正的代码. - 或者不是全部在一个文件中.真正的代码通常存在代码传递问题double- 由于缺少先前的声明/定义,但show_float()期望a float.
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len){
int i;
for (i = 0; i<len; i++)
printf(" %.2x", start[i]);
printf("\n");
}
int main(){
int y = 0xffffff;
float f = y;
show_float(f); // code lacks proto-type, so assumes it needs to pass a double
return 0;
}
void show_float(float x){
show_bytes((byte_pointer)&x, sizeof(float));
}
Run Code Online (Sandbox Code Playgroud)
通过声明原型或重新订购代码来修复.
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len);
void show_float(float x);
/* the 3 functions in any order */
Run Code Online (Sandbox Code Playgroud)