Does Linux gcc (5.4.0) plugin obey IEEE754 rules?
My gcc programming environment version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1 ~ 16.04.4)
I am coding as:
#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;
}
and the machine will give the result: 00 00 00 e0
I think this is wrong on IEEE 754; but I do not know why. while the same code in VS 2013 on Windows gives the correct answer:ff ff 7f 4b
Does gcc not support IEEE 754 5.4.0? Or are there some problems in my code?
source to share
Does gcc 5.4.0 not accept IEEE 754?
Or are there some problems in my code?
gcc 5.4.0 and IEEE 754 is not a problem. Of course the code doesn't match
By reordering the functions, but the same code, I get 2 warnings and can replicate the OP's output 00 00 00 e0
warning: implicit function declaration 'show_float' [-Wimplicit-function-declaration]
warning: conflicting types for 'show_float'
I suspect the OP did not post the true code. - or it's not all in one file. Real code usually has a code passing problem double
- due to lack of pre-declaration / definition, but show_float()
expects 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));
}
Fix by declaring prototypes or reordering code.
#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 */
source to share
gcc does not use another single floating point representation, but rather promotes it to a double place somewhere in the program. The conversion probably happens when called show_bytes
, but it could be somewhere else. If you change float to double, you can get the same wrong output again, which will probably work using both compilers.
It may be a bug or gcc optimization, but this is not normal behavior for c compilers, see this question
#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(double x) {
show_bytes((byte_pointer)&x, sizeof(float));
}
int main() {
int y = 0xffffff;
double f = y;
show_float(f);
return 0;
}
Outputs
00 00 00 e0
if you change sizeof(float)
to sizeof(double)
in your program which is not outputting correctly, you can see the whole double to check it out.
for me using paired pairs I get
00 00 00 e0 ff ff 6f 41
source to share