为什么0.1 * 3!= 0.3 [英] Why 0.1 * 3 != 0.3

查看:165
本文介绍了为什么0.1 * 3!= 0.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
为什么不能用二进制精确地表示十进制数字?

Possible Duplicate:
Why can't decimal numbers be represented exactly in binary?

我正在阅读这个问题,并在评论(关于共产主义鸭子的回答).确保我不会迷失方向后,我发现自己很好奇.

I was reading this question and found this in the comments (on The Communist Duck's answer). After making sure that I'd not lost my mind I find myself curious.

那为什么是0.1 * 3!= 0.3?

So why is 0.1 * 3 != 0.3 ?

(请注意,问题与游戏开发以及游戏开发人员应熟悉的数学领域有关)

(note, the question was regarding game development and which fields of math a game developer should be familiar with)

推荐答案

原因是,只有2的幂的数字才能用简单的二进制表示形式精确表示.

Ther reason is that only numbers that are powers of 2 can be represented precisely with a simple binary representation.

二进制数的各个数字是2的不同幂,因此任何数字都是2的不同幂的总和.

The individual digits of a binary number are different powers of 2 and any number is thus a sum of different powers of 2.

0.1不对应于2的幂,并且不能与2的不同幂相加.因此,它只能以一定的误差表示.您可以在此处

0.1 does not correspond to a power of 2 and can not be summed up from different powers of 2. Thus it can be represented only with a certain error. You can find a more elaborate explanation and examples here

如果需要精确的数字,则通常必须使用整数.我不确定其他语言,但是在Java中,有几个类旨在解决那些对精度至关重要的应用程序(例如财务计算)确切解决此问题的问题.最突出的是 BigInteger BigDecimal

If you need precise numbers, you are mostly bound to use integers. I am not sure about other languages, but in java there are several classes designed to overcome exactly this issue for applications where precision is essential,like in financial computations. Most prominent are BigInteger and BigDecimal

这篇关于为什么0.1 * 3!= 0.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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