我的任务是将浮点数从小数转换为32位二进制表示(IEEE 754。)
以下是我尝试解决任务的问题;它导致了几个在线转换工具让我产生的相同结果,但是有人告诉我,我的解决方案并不合适,因为它编码了一个不正确的指数。"
我是否可以深入了解我的哪些步骤正是导致此问题(如果有)以及为什么我仍然得到正确的(?)结果?
=============================================== ===================
要转换的浮点数(十进制表示法):6.4
二进制小数点前的位置:
6/2 = 3 -> 0
3/2 = 1.5 -> 1
1/2 = 0.5 -> 1
=> 110
二进制的小数位数:
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
0.6 * 2 = 1.2 -> 1
0.2 * 2 = 0.4 -> 0
0.4 * 2 = 0.8 -> 0
0.8 * 2 = 1.6 -> 1
=> 011001100110011001100110011001
标准化:
110,011001100110011001100110011001 * 2^0
1,10011001100110011001100110011001 * 2^2
特点:
2+127 = 129 (Exponent + Bias)
129 / 2 = 64.5 -> 1
64 / 2 = 32 -> 0
32 / 2 = 16 -> 0
16 / 2 = 8 -> 0
8 / 2 = 4 -> 0
4 / 2 = 2 -> 0
2 / 2 = 1 -> 0
1 / 2 = 0.5 -> 1
=> 10000001
标志:0(正面)
结果:
01000000110011001100110011001101
可能导致同样问题的代码段:
public static void main(String[] args) {
float input = IO.readFloat("Please enter a float!\n");
String sign = input < 0 ? "1" : "0";
if (input < 0) {
input *= -1;
} else if (input == 0) {
IO.println("00000000000000000000000000000000");
return;
}
float preDecimalPointPositions = input - (input % 1);
String preDecimalPointPositionsBinary = "";
while (preDecimalPointPositions >= 1 ) {
if (preDecimalPointPositions % 2 == 0) {
preDecimalPointPositionsBinary = "0" + preDecimalPointPositionsBinary;
} else {
preDecimalPointPositionsBinary = "1" + preDecimalPointPositionsBinary;
}
preDecimalPointPositions /= 2;
preDecimalPointPositions = preDecimalPointPositions - (preDecimalPointPositions % 1); // Abschneiden der decimalPlacesstellen
}
float decimalPlaces = input % 1;
String decimalPlacesBinary = ""; //
for (int i = 0; i < 23; i++) { //
decimalPlaces *= 2;
if (decimalPlaces >= 1) {
decimalPlacesBinary += "1";
decimalPlaces -= 1;
} else {
decimalPlacesBinary += "0";
}
}
int exponent;
if (input < 1) {
IO.println("Negative exponents ain't allowed here! =/");
return;
} else {
exponent = preDecimalPointPositionsBinary.length() - 1;
}
String normalized = preDecimalPointPositionsBinary.substring(1) + decimalPlacesBinary;
float characteristic = exponent + 127;
String characteristicBinary = "";
while (characteristic >= 1 ) {
if (characteristic % 2 == 0) {
characteristicBinary = "0" + characteristicBinary;
} else {
characteristicBinary = "1" + characteristicBinary;
}
characteristic /= 2;
characteristic = characteristic - (characteristic % 1);
}
String result = sign + characteristicBinary + normalized.substring(0, 23);
IO.println(result);
}
答案 0 :(得分:1)
将小数位转换为二进制时,第8行表示
0.2 * 2 = 0.4 -> 0
然后继续
0.8 * 2 = 1.6 -> 1
而不是
0.4 * 2 = 0.8 -> 0
行 12,15,18,21和24
上存在同样的问题修复这个应该给你正确的尾数(标准化后)给你正确的结果
01000000110011001100110011001101
您还可以查看this IEEE 754 converter