Java中浮点数是怎么存储在计算机中的?

孙大诚_SunRobin 2020-05-27 02:13:04

最近研究Java中double类型的存储,比如说0.1,它存储的时候转为二进制,分为指数跟尾数。因为转换的机制,0.1并不能精确的存储,而是指数部分一个很长的无线循环过程。

1. 那么,Java语言中,定义一个变量 double a = 0.1, 它存储的时候,是截取了64位,存在内存中吗?

当我们调试的时候,我们选中一个double类型的变量,会看到这个变量的值是0.1,那么这个0.1是一个~0.1的近似值吗?

2. 还有,有人说2.0-1.1 的真实结果不是输出0.9,而是0.89999999999999999, 但是我们调试的时候,看到的变量值就是0.9,是JVM给自动取近似值了么?


3. double d = 2.4; System.out.println(d); 输出的是2.4,而不是2.3999999999999999。我理解的是,d在二进制存储的时候,是存成了对应2.3999999999999999的二进制序列,那么反过来,这个序列在转成十进制的时候,应该是2.3999999999999999呀,怎么输出了2.4呢?
...全文
357 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
孙大诚_SunRobin 2020-06-03
  • 打赏
  • 举报
回复
引用 3 楼 爱吃鱼的ねこ 的回复:
[quote=引用 2 楼 孙大诚_SunRobin的回复:][quote=引用 1 楼 爱吃鱼的ねこ 的回复:] 我里面写的挺详细的 https://blog.csdn.net/weixin_43982698/article/details/97886752?utm_source=app
你在文章里说, 在内存中,32位的二进制科学记数法不能精确的表示0.1,它与实际的0.1的误差为0.000000001490116119384765625。 在float a = 0.1f; System.out.println(a)的时候,它还是输出0.1,不是0.000000001490116119384765625. 为什么System.out.println(a)不输出真实值呢?[/quote] 额。。。。。 float的精度是7位,它四舍五入不就是0.1么。[/quote] 也对,它是四舍五入了。 println uses Double.toString internally for converting a double to a string, which can then be printed. The Double.toString javadoc says the following: How many digits must be printed for the fractional part of m or a? There must be at least one digit to represent the fractional part, and beyond that as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double. That is, suppose that x is the exact mathematical value represented by the decimal representation produced by this method for a finite nonzero argument d. Then d must be the double value nearest to x; or if two double values are equally close to x, then d must be one of them and the least significant bit of the significand of d must be 0. The important part here is "as many, but only as many, more digits as are needed to uniquely distinguish the argument value from adjacent values of type double". This means that as long as 2.4 is closer to the actual value in d than to any other possible double value, 2.4 will be printed.
孙大诚_SunRobin 2020-05-29
  • 打赏
  • 举报
回复
引用 1 楼 爱吃鱼的ねこ 的回复:
我里面写的挺详细的 https://blog.csdn.net/weixin_43982698/article/details/97886752?utm_source=app
你在文章里说, 在内存中,32位的二进制科学记数法不能精确的表示0.1,它与实际的0.1的误差为0.000000001490116119384765625。 在float a = 0.1f; System.out.println(a)的时候,它还是输出0.1,不是0.000000001490116119384765625. 为什么System.out.println(a)不输出真实值呢?
爱吃鱼的ねこ 2020-05-29
  • 打赏
  • 举报
回复
引用 2 楼 孙大诚_SunRobin的回复:
[quote=引用 1 楼 爱吃鱼的ねこ 的回复:] 我里面写的挺详细的 https://blog.csdn.net/weixin_43982698/article/details/97886752?utm_source=app
你在文章里说, 在内存中,32位的二进制科学记数法不能精确的表示0.1,它与实际的0.1的误差为0.000000001490116119384765625。 在float a = 0.1f; System.out.println(a)的时候,它还是输出0.1,不是0.000000001490116119384765625. 为什么System.out.println(a)不输出真实值呢?[/quote] 额。。。。。 float的精度是7位,它四舍五入不就是0.1么。
爱吃鱼的ねこ 2020-05-27
  • 打赏
  • 举报
回复
我里面写的挺详细的 https://blog.csdn.net/weixin_43982698/article/details/97886752?utm_source=app

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧