gpt4 book ai didi

java - 将 float 从十进制表示转换为 32 位二进制(IEEE 754 浮点)时,指数 (?) 不正确

转载 作者:行者123 更新时间:2023-11-30 07:02:47 28 4
gpt4 key购买 nike

我的任务是将 float 从十进制转换为 32 位二进制表示形式 (IEEE 754。)

以下是我尝试解决该任务的尝试;它导致了与几个在线转换工具给我带来的相同结果,但是我被告知我的解决方案不好,因为“它编码了错误的指数。”

我能否深入了解我的哪个步骤到底导致了此问题(如果有)以及为什么我仍然得到正确的(?)结果?

================================================== ===================

要转换的 float (十进制表示):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);
}

最佳答案

将小数位转换为二进制时,第 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 查看

关于java - 将 float 从十进制表示转换为 32 位二进制(IEEE 754 浮点)时,指数 (?) 不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40614179/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com