如何将int类型转换为ieee754?
how to convert int type to ieee754?
我试图在采用整数类型后打印出 IEEE754,但它没有为我显示正确的答案。
我想在main方法中将整数传递给函数"void ieee(int x)",然后它会打印出IEEE754格式。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int binary(int n, int i)
{
int k;
for (i--; i >= 0; i--)
{
k = n >> i;
if (k & 1)
printf("1");
else
printf("0");
}
}
typedef union
{
int f;
struct
{
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} field;
} myfloat;
void ieee(int x)
{
int i;
myfloat var = (myfloat)x;
printf("%d ",var.field.sign);
binary(var.field.exponent, 8);
printf(" ");
binary(var.field.mantissa, 23);
printf("\n");
}
int main()
{
int x = 3;
ieee(x);
return 0;
}
您正在 int
和 struct
类型之间进行类型双关,其中包含 float
的内部表示。
这会给你错误的答案。
如果您想知道整数的浮点表示形式,可以通过对 float
.
进行先前的转换来获得正确的结果
int x = 3;
myfloat var;
var.f = (float)x;
binary(var.field.exponent, 8);
binary(var.field.mantissa, 23);
此外,请注意不能假定 IEEE 浮点表示法用于 float
。
例如,请参阅此 link:
Macro __STDC_IEC_559__
另一方面,位域不一定在所有实现中都是连续的。
见Bitfield disadvantages。
下面使用联合将float
的表示重新解释为32位无符号整数。这在 C 中是有效的。在 C++ 中,联合不能用于此,并且有必要将字节从 float
复制到整数中,如 memcpy
.
#include <limits.h> // For CHAR_BIT (overkill but demonstrates some portability).
#include <stdint.h>
#include <stdio.h>
static void DisplayFloat(float x)
{
// Use a union to reinterpret a float as a 32-bit unsigned integer.
union { float f; uint32_t u; } t = { x };
// Ensure float and uint32_t are the same width.
_Static_assert(sizeof t.f == sizeof t.u,
"float and uint32_t must be same width.");
// Display the bits of the unsigned integer.
for (int i = sizeof t.u * CHAR_BIT - 1; 0 <= i; --i)
putchar('0' + (t.u >> i & 1));
putchar('\n');
}
int main(void)
{
DisplayFloat(3);
}
我试图在采用整数类型后打印出 IEEE754,但它没有为我显示正确的答案。 我想在main方法中将整数传递给函数"void ieee(int x)",然后它会打印出IEEE754格式。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int binary(int n, int i)
{
int k;
for (i--; i >= 0; i--)
{
k = n >> i;
if (k & 1)
printf("1");
else
printf("0");
}
}
typedef union
{
int f;
struct
{
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} field;
} myfloat;
void ieee(int x)
{
int i;
myfloat var = (myfloat)x;
printf("%d ",var.field.sign);
binary(var.field.exponent, 8);
printf(" ");
binary(var.field.mantissa, 23);
printf("\n");
}
int main()
{
int x = 3;
ieee(x);
return 0;
}
您正在 int
和 struct
类型之间进行类型双关,其中包含 float
的内部表示。
这会给你错误的答案。
如果您想知道整数的浮点表示形式,可以通过对 float
.
int x = 3;
myfloat var;
var.f = (float)x;
binary(var.field.exponent, 8);
binary(var.field.mantissa, 23);
此外,请注意不能假定 IEEE 浮点表示法用于 float
。
例如,请参阅此 link:
Macro __STDC_IEC_559__
另一方面,位域不一定在所有实现中都是连续的。
见Bitfield disadvantages。
下面使用联合将float
的表示重新解释为32位无符号整数。这在 C 中是有效的。在 C++ 中,联合不能用于此,并且有必要将字节从 float
复制到整数中,如 memcpy
.
#include <limits.h> // For CHAR_BIT (overkill but demonstrates some portability).
#include <stdint.h>
#include <stdio.h>
static void DisplayFloat(float x)
{
// Use a union to reinterpret a float as a 32-bit unsigned integer.
union { float f; uint32_t u; } t = { x };
// Ensure float and uint32_t are the same width.
_Static_assert(sizeof t.f == sizeof t.u,
"float and uint32_t must be same width.");
// Display the bits of the unsigned integer.
for (int i = sizeof t.u * CHAR_BIT - 1; 0 <= i; --i)
putchar('0' + (t.u >> i & 1));
putchar('\n');
}
int main(void)
{
DisplayFloat(3);
}