为什么2个浮点数之间的这种简单划分不适用于Java? [英] Why does this simple division between 2 floats not work with java?

查看:96
本文介绍了为什么2个浮点数之间的这种简单划分不适用于Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.out.println((26.55f/3f));

System.out.println((float)( (float)26.55 / (float)3.0 ));

等。

返回结果8.849999 。不是应该的8.85。

returns the result 8.849999. not 8.85 as it should.

有人可以解释吗,还是我们都应该避免使用浮点数?

Can anyone explain this or should we all avoid using floats?

推荐答案

每个程序员应了解的浮点算法


问:为什么我的数字(例如0.1 + 0.2
)加起来是0.3,而
我得到一个奇怪的结果,例如
0.30000000000000004?

Q: Why don’t my numbers, like 0.1 + 0.2 add up to a nice round 0.3, and instead I get a weird result like 0.30000000000000004?

A:因为在内部,计算机使用
格式(二进制浮点数)来表示
不能准确地表示
之类的数字,例如0.1、0.2或0.3。

A: Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

到站点

这篇关于为什么2个浮点数之间的这种简单划分不适用于Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆