“Java中的浮点型”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
第156行: 第156行:
 
|'''<big>0</big>'''
 
|'''<big>0</big>'''
 
|}⑦而指数(阶码)的部分就是:'''13'''+127=140<sub>(10)</sub>=10001100<sub>(2)</sub>
 
|}⑦而指数(阶码)的部分就是:'''13'''+127=140<sub>(10)</sub>=10001100<sub>(2)</sub>
 
 
 
 
 
 
 
 
 
 
=== 验证二进制字符串对应浮点数 ===
 
Java中浮点数也按照IEEE754标准存储,上面的 对于 float,对于上面的浮点型“12345.12345f”之所以输出 31 bit 二进制,是因为首位的符号位(0-正,1-负)被隐藏了,
 
 
完整的形式如下:
 
{| class="wikitable"
 
!符号位
 
! colspan="8" |指数
 
! colspan="23" |有效数
 
|-
 
!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
 
|-
 
!+/-
 
!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
 
|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
 
|}
 

2022年11月12日 (六) 10:20的版本

怎样查看浮点型的二进制形式

以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 输入参数的二进制形式转换成对应的十进制值。


十进制小数转二进制小数

“12345.12345f”中,

①整数部分“12345”应用除2取余得到的二进制形式是“11000000111001”;(参见:十进制和其他进制之间的转换

②小数部分“0.12345”应用乘2取整得到的二进制形式是“0.0001111111011010011……”。(参见:十进制小数转 IEEE 754 单精度浮点数

③合在一起,12345.12345(10)=11000000111001.0001111110011010011……(2)

④用科学计数法表示,就是11000000111001.0001111110011010011……(2)=1.10000001110010001111110011010011……×213

⑤作为二进制小数,存储的时候不保存整数位的1(因为用科学计数法表示,整数部分一定是1),所以作为尾数存储的二进制部分就是小数点“.”后面的“10000001110010001111111011010011……”

⑥此时能够发现和前面打印出的二进制部分中有效数(或者说尾数)的部分存储内容是相同的:

符号位 指数/阶码 有效数/尾数
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
+/- 十进制指数+127(正负幂的偏置修正值)转二进制 2-1 2-2 2-3 2-4 2-5 2-6 2-7 2-8 2-9 2-10 2-11 2-12 2-13 2-14 2-15 2-16 2-17 2-18 2-19 2-20 2-21 2-22 2-22
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

⑦而指数(阶码)的部分就是:13+127=140(10)=10001100(2)