查看“Java中的浮点型”的源代码
←
Java中的浮点型
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
=== 怎样查看浮点型的二进制形式 === 以float为例:<syntaxhighlight lang="java"> 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 } </syntaxhighlight><syntaxhighlight lang="console"> 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 | </syntaxhighlight>Float.floatToIntBits(float value) 方法返回的 int 型数据,就是 float 输入参数的二进制形式转换成对应的十进制值。 === 十进制小数转二进制小数 === “12345.12345f”中, 整数部分“12345”应用除2取余得到的二进制形式是“11000000111001”;(参见:[[十进制和其他进制之间的转换]]) 小数部分“0.12345”应用乘2取整得到的二进制形式是“”。(参见:[[十进制小数转 IEEE 754 单精度浮点数]]) === 验证二进制字符串对应浮点数 === Java中浮点数也按照IEEE754标准存储,上面的 对于 float,对于上面的浮点型“12345.12345f”之所以输出 31 bit 二进制,是因为首位的符号位(0-正,1-负)被隐藏了, 完整的形式如下: {| class="wikitable" !符号位 ! colspan="8" |指数 ! colspan="23" |有效数 |- !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 |- !+/- !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 |}
返回至
Java中的浮点型
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息