“Java中的浮点型”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) |
Jihongchang(讨论 | 贡献) |
||
第6行: | 第6行: | ||
int intBits = Float.floatToIntBits(12345.12346f); //Bit Representation of the Float | int intBits = Float.floatToIntBits(12345.12346f); //Bit Representation of the Float | ||
− | System.out. | + | System.out.printf("intBits:%d\n", intBits); //1178657918 |
String binaryString = Integer.toBinaryString(intBits); | String binaryString = Integer.toBinaryString(intBits); | ||
− | System.out. | + | System.out.printf("binaryString:%s\n", binaryString); |
− | + | String completeBinaryString = String.format("%32s", binaryString).replace(' ', '0'); | |
+ | System.out.printf("binaryString补零后:%s\n", completeBinaryString); | ||
− | } | + | |
+ | System.out.println("格式化显示:"); | ||
+ | int i = 0; | ||
+ | System.out.printf("%-3d|", i++); | ||
+ | while (i < completeBinaryString.length()) { | ||
+ | System.out.printf("%-3d|", i++); | ||
+ | } | ||
+ | System.out.println(); | ||
+ | |||
+ | int j = 0; | ||
+ | System.out.printf("%-3c|", completeBinaryString.charAt(j++)); | ||
+ | while (j < completeBinaryString.length()) { | ||
+ | System.out.printf("%-3c|", completeBinaryString.charAt(j++)); | ||
+ | } | ||
+ | System.out.println("\n"); | ||
+ | |||
+ | }//end main | ||
} | } | ||
</syntaxhighlight><syntaxhighlight lang="console"> | </syntaxhighlight><syntaxhighlight lang="console"> | ||
− | 1178657918 | + | intBits:1178657918 |
− | 1000110010000001110010001111110 | + | binaryString:1000110010000001110010001111110 |
− | 31 | + | binaryString补零后:01000110010000001110010001111110 |
+ | 格式化显示: | ||
+ | 0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 | | ||
+ | 0 |1 |0 |0 |0 |1 |1 |0 |0 |1 |0 |0 |0 |0 |0 |0 |1 |1 |1 |0 |0 |1 |0 |0 |0 |1 |1 |1 |1 |1 |1 |0 | | ||
</syntaxhighlight>Float.floatToIntBits(float value) 方法返回的 int 型数据,就是 float 输入参数的二进制形式转换成对应的十进制值。 | </syntaxhighlight>Float.floatToIntBits(float value) 方法返回的 int 型数据,就是 float 输入参数的二进制形式转换成对应的十进制值。 | ||
+ | === 十进制小数转二进制小数 === | ||
=== 验证二进制字符串对应浮点数 === | === 验证二进制字符串对应浮点数 === | ||
− | Java中浮点数也按照IEEE754标准存储,上面的 对于 | + | Java中浮点数也按照IEEE754标准存储,上面的 对于 float,对于上面的浮点型“12345.12345f”之所以输出 31 bit 二进制,是因为首位的符号位(0-正,1-负)被隐藏了, |
+ | |||
+ | 完整的形式如下: | ||
{| class="wikitable" | {| class="wikitable" | ||
!符号位 | !符号位 | ||
− | ! | + | ! colspan="8" |指数 |
− | ! | + | ! colspan="23" |有效数 |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
!1 | !1 | ||
第89行: | 第83行: | ||
!29 | !29 | ||
!30 | !30 | ||
− | |||
!31 | !31 | ||
− | |||
!32 | !32 | ||
− | 2<sup>0</sup> | + | |- |
+ | !+/- | ||
+ | !2<sup>7</sup> | ||
+ | !2<sup>6</sup> | ||
+ | !2<sup>5</sup> | ||
+ | !2<sup>4</sup> | ||
+ | !2<sup>3</sup> | ||
+ | !2<sup>2</sup> | ||
+ | !2<sup>1</sup> | ||
+ | !2<sup>0</sup> | ||
+ | !2<sup>-1</sup> | ||
+ | !2<sup>-2</sup> | ||
+ | !2<sup>-3</sup> | ||
+ | !2<sup>-4</sup> | ||
+ | !2<sup>-5</sup> | ||
+ | !2<sup>-6</sup> | ||
+ | !2<sup>-7</sup> | ||
+ | !2<sup>-8</sup> | ||
+ | !2<sup>-9</sup> | ||
+ | !2-10 | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
+ | ! | ||
|- | |- | ||
|0 | |0 | ||
+ | |1 | ||
|0 | |0 | ||
|0 | |0 | ||
|0 | |0 | ||
+ | |1 | ||
+ | |1 | ||
|0 | |0 | ||
|0 | |0 | ||
+ | |1 | ||
|0 | |0 | ||
|0 | |0 | ||
第107行: | 第135行: | ||
|0 | |0 | ||
|0 | |0 | ||
+ | |1 | ||
+ | |1 | ||
+ | |1 | ||
|0 | |0 | ||
+ | |1 | ||
|0 | |0 | ||
|0 | |0 | ||
|0 | |0 | ||
|0 | |0 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|1 | |1 | ||
|1 | |1 | ||
|1 | |1 | ||
+ | |1 | ||
+ | |1 | ||
+ | |1 | ||
+ | |0 | ||
|} | |} |
2022年11月12日 (六) 07:40的版本
怎样查看浮点型的二进制形式
以float为例:
public class Test2 {
public static void main(String[] args) {
int intBits = Float.floatToIntBits(12345.12346f); //Bit Representation of the Float
System.out.printf("intBits:%d\n", intBits); //1178657918
String binaryString = Integer.toBinaryString(intBits);
System.out.printf("binaryString:%s\n", binaryString);
String completeBinaryString = String.format("%32s", binaryString).replace(' ', '0');
System.out.printf("binaryString补零后:%s\n", completeBinaryString);
System.out.println("格式化显示:");
int i = 0;
System.out.printf("%-3d|", i++);
while (i < completeBinaryString.length()) {
System.out.printf("%-3d|", i++);
}
System.out.println();
int j = 0;
System.out.printf("%-3c|", completeBinaryString.charAt(j++));
while (j < completeBinaryString.length()) {
System.out.printf("%-3c|", completeBinaryString.charAt(j++));
}
System.out.println("\n");
}//end main
}
intBits:1178657918
binaryString:1000110010000001110010001111110
binaryString补零后:01000110010000001110010001111110
格式化显示:
0 |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |13 |14 |15 |16 |17 |18 |19 |20 |21 |22 |23 |24 |25 |26 |27 |28 |29 |30 |31 |
0 |1 |0 |0 |0 |1 |1 |0 |0 |1 |0 |0 |0 |0 |0 |0 |1 |1 |1 |0 |0 |1 |0 |0 |0 |1 |1 |1 |1 |1 |1 |0 |
Float.floatToIntBits(float value) 方法返回的 int 型数据,就是 float 输入参数的二进制形式转换成对应的十进制值。
十进制小数转二进制小数
验证二进制字符串对应浮点数
Java中浮点数也按照IEEE754标准存储,上面的 对于 float,对于上面的浮点型“12345.12345f”之所以输出 31 bit 二进制,是因为首位的符号位(0-正,1-负)被隐藏了,
完整的形式如下:
符号位 | 指数 | 有效数 | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 |
+/- | 27 | 26 | 25 | 24 | 23 | 22 | 21 | 20 | 2-1 | 2-2 | 2-3 | 2-4 | 2-5 | 2-6 | 2-7 | 2-8 | 2-9 | 2-10 | |||||||||||||
0 | 1 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |