系统相关
首页 > 系统相关> > Linux gcc(5.4.0)中的浮点数是否符合IEEE754规则?

Linux gcc(5.4.0)中的浮点数是否符合IEEE754规则?

作者:互联网

我的编程环境是
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;
}

然后机器给出结果:00 00 00 e0

根据IEEE 754,我认为这是不对的;但我不知道为什么.
虽然Windows 2013中的VS 2013中的相同代码给出了正确的答案:ff ff 7f 4b

gcc 5.4.0不采用IEEE 754吗?
或者我的代码中有问题吗?

解决方法:

Does gcc 5.4.0 not adopt the IEEE 754?
Or are there some problem in my code?

gcc 5.4.0和IEEE 754不是问题.当然代码不符合要求

通过重新排序函数,但是相同的代码,我得到2个警告,并且可以复制OP的输出00 00 00 e0

警告:隐式声明函数’show_float'[-Wimplicit-function-declaration]
警告:’show_float’的冲突类型

我怀疑OP没有发布真正的代码. – 或者不是全部在一个文件中.真正的代码通常有代码传递的问题 – 由于缺少先前的声明/定义,但show_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));
}

通过声明原型或重新订购代码来修复.

#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 */

标签:c-3,linux,floating-point,gcc,ieee-754
来源: https://codeday.me/bug/20190828/1751058.html