Geeksforgeeks C 程序故障排除:IEE 754 表示法为十进制
Troubleshooting Geeksforgeeks C Program: IEE 754 Notation to Decimal
我尝试对此代码进行故障排除,但由于我是 C 语言的新手,我无法破解它。我从这篇文章中提取这个程序 Geeksforgeeks 创建: https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/
每次我在单独的 C++ shell 中运行此程序时,都会收到以下错误:从"无符号 int*"到"int*"的转换无效 [-允许]
有人有什么想法吗?
这是代码:
// C program to convert
// IEEE 754 floating point representaion
// into real value
#include <math.h>
#include <stdio.h>
typedef union {
float f;
struct
{
// Order is important.
// Here the members of the union data structure
// use the same memory (32 bits).
// The ordering is taken
// from the LSB to the MSB.
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} raw;
} myfloat;
// Function to convert a binary array
// to the corresponding integer
unsigned int convertToInt(int* arr, int low, int high)
{
unsigned f = 0, i;
for (i = high; i >= low; i--) {
f = f + arr[i] * pow(2, high - i);
}
return f;
}
// Driver Code
int main()
{
// Get the 32-bit floating point number
unsigned int ieee[32]
= { 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0 };
myfloat var;
// Convert the least significant
// mantissa part (23 bits)
// to corresponding decimal integer
unsigned f = convertToInt(ieee, 9, 31);
// Assign integer representation of mantissa
var.raw.mantissa = f;
// Convert the exponent part (8 bits)
// to a corresponding decimal integer
f = convertToInt(ieee, 1, 8);
// Assign integer representation
// of the exponent
var.raw.exponent = f;
// Assign sign bit
var.raw.sign = ieee[0];
printf("The float value of the given"
" IEEE-754 representation is : n");
printf("%f", var.f);
}
感谢任何愿意提供帮助的人。
要解决这个问题,我所要做的就是删除以下代码中的"无符号":
unsigned int ieee[32]
= { 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0 };
再次感谢您的所有输入!
相关文章:
- Geeksforgeeks C 程序故障排除:IEE 754 表示法为十进制
- (第 1 位 + 第 2 位)以十进制数表示
- 如何将QBytearrayBCD转换为十进制QString表示
- 使用十进制表示法来引用多维阵列
- 如何使用C 将二进制数字的字符串转换为其签名的十进制表示
- 与 IPv4 点分十进制表示法匹配的 lex 模式
- 计算整数的十进制表示形式的不同位数
- C++-如何获得十进制数的二进制表示
- 关键字符的十进制表示
- 从包含表示约 64 GB 值的十进制数的 String 变量中获取值
- C++ 一种方法,该方法将十进制值作为用布尔数组表示的二进制的整数返回
- 以十进制、八进制和十六进制表示法标识整数文本是否与转义字符中的整数文本相同
- Codekata将数字从十六进制转换为十进制整数表示形式
- 十进制数和精确表示
- C++:将 UTF16 字符的十六进制表示转换为十进制(如 python 的 int(hex_data, 16))
- 类型的十进制表示长度的近似值
- 如何使用Clang知道IntegerLiteral是来自十进制还是八进制表示
- Microsoft psapi方法是否使用十进制或二进制表示法返回字节?
- 将整数的十进制值作为其 ASCII 表示形式插入到字符向量中
- 在c++中处理浮点数或双精度数.表示错误.丢失十进制值