理解浮点表示错误;我的想法有什么问题? [英] Understanding floating point representation errors; what's wrong with my thinking?

查看:134
本文介绍了理解浮点表示错误;我的想法有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解为什么有些数字不能用浮点数表示。

正如我们所知道的,一个正常的浮动将有符号位,指数和尾数。例如,为什么不能在这个系统中准确表示0.1;我认为这样的方法是将10(以bin为单位)放到尾数上,把-2放到指数上。据我所知,这两个数字都可以用尾数和指数精确表示。那么为什么我们不能准确地表示0.1?如果你的指数是十进制的(即它代表10 ^ X),那么你可以精确地表示0.1 - 但是,大多数浮点格式使用二进制指数(即它们代表2 ^ X)。由于没有整数 X Y ,所以 Y *(2 ^ X)= 0.1 ,所以在大多数浮点格式中不能精确地表示0.1。



有些语言的类型都有指数。例如,在C#中,有一个恰当地命名为 decimal 的数据类型,它是一个带有十进制指数的浮点格式,所以它将支持存储一个数字,如0.1其他不常见的属性: decimal 类型可以区分 0.1 0.10 ,对于 x

的所有值来说, x + 1!= x >

对于大多数常见目的,C#也有 float double 浮点类型,因为它们使用二进制指数(如IEEE-754中定义的),所以无法精确地存储0.1。二进制浮点类型使用较少的存储,速度更快,因为它们更易于实现,并且定义了更多的操作。通常, decimal 仅用于所有十进制值的确切表示非常重要的财务值,而存储,速度和操作范围则不是。


I'm having some trouble understanding why some figures can't be represented with floating point number.

As we know, a normal float would have sign bit, exponent, and mantissa. Why can't, for example, 0.1 be represented accurately in this system; the way I think of it would be that you put 10 (1010 in bin) to mantissa and -2 to the exponent. As far as I know, both numbers can be accurately represented in the mantissa and exponent. So why can't we represent 0.1 accurately?

解决方案

If your exponent is decimal (i.e. it represents 10^X), you can precisely represent 0.1 -- however, most floating point formats use binary exponents (i.e. they represent 2^X). Since there are no integers X and Y such that Y * (2 ^ X) = 0.1, you cannot precisely represent 0.1 in most floating point formats.

Some languages have types with both exponents. In C#, for example, there is a data type aptly named decimal which is a floating point format with a decimal exponent so it will support storing a number like 0.1, although it has other uncommon properties: The decimal type can distinguish between 0.1 and 0.10, and it is always true that x + 1 != x for all values of x.

For most common purposes, though, C# also has the float and double floating point types that cannot precisely store 0.1 because they use a binary exponent (as defined in IEEE-754). The binary floating point types use less storage, are faster because they are easier to implement, and have more operations defined on them. In general decimal is only used for financial values where the exact representation of all decimal values is important and the storage, speed, and range of operations are not.

这篇关于理解浮点表示错误;我的想法有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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