计算转换十进制浮点时,哪些号码会出现问题? [英] Calculate which numbers cause problems when converting decimal to floating point?

查看:122
本文介绍了计算转换十进制浮点时,哪些号码会出现问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,从二进制转换成一些数字浮点时有可能出现问题。

I know when converting some numbers from binary to floating point there can be problems.

例如:1.4的结果1.39999。 6.2为6.1999999,6.6是6.599999,等等。

For example: 1.4 results in 1.39999. 6.2 is 6.1999999, 6.6 is 6.599999, etc.

有没有一种方法来计算该数字将导致这些问题?就像从1-50,000建立某种形式的小号preadsheet或数据库的数字不准确转换?

Is there a way to calculate which numbers will cause these problems? Like create some sort of spreadsheet or database with numbers from 1-50,000 which don't convert exactly?

推荐答案

在浮点运算的错误根源在于浮点数的存储方式。这取决于美元,你正在使用(通常是单(浮动)或双precision)p $ pcision。双precision花车会占用更多空间,但大约两倍precise。

The errors in floating point calculations are rooted in the way that floating point numbers are stored. Depending on which precision you are using (usually either single (float) or double-precision). Double-precision floats take more space, but are roughly twice as precise.

浮点数通常存储在一个IEEE格式并因此只有最显著比特被包括

Floating point numbers are typically stored in an IEEE format and thus only the most significant bits are included.

在存储在此方式重新present浮点数的比特串,有包括几个不同的部分。首先,有指示的数目是否为正或负符号位。接着,对数的指数部分被存储(在单precision浮筒,这需要8比特)。指数后,所有剩下的位重新present数的显著数字。因此,该位1千万01000000000000000000000重新present -0.5符号位(第一个)表示该数字为负数。接下来的八位是指数。通常情况下,指数存储与偏见,这样小的数字可能pcisely存储$ P $除了路数。要做到这一点,用于计算指数为-127(如果一个8位的指数)+指数位PTED作为一个整数(在本例中128)间$ P $。所有剩余位表示的数目的显著数字开头的那些地方,并从那里向右移动,我们在半每次切断值(1为1,但01是1/2和001是1/4)在我们的情况下总数为-0.5(-1为符号位* 2 ^ 1为指数* 0.5为剩余位)

In a string of bits stored in this way to represent a floating point number, there are different parts that comprise the number. First, there is a sign bit indicating whether the number is positive or negative. Next, the exponent portion of the number is stored (in single-precision floats, this takes 8 bits). After the exponent, all remaining bits represent the significant digits of the number. Thus, the bits 1 10000000 01000000000000000000000 represent -0.5 The sign bit (first one) signifies that the number is negative. The next eight bits are the exponent. Usually, the exponent is stored with a bias so that small numbers may be stored precisely in addition to large ones. To do this, the exponent used for calculations is -127 (if an 8 bit exponent) + the exponent bits interpreted as an integer (in our case 128). All remaining bits signify the significant digits of the number starting with the ones place and moving rightward from there we cut the value in half each time (1 is 1, but 01 is 1/2 and 001 is 1/4) in our case the total number is -0.5 (-1 for the sign bit * 2^1 for the exponent * 0.5 for the remaining bits)

有关进一步的例子,这里是使用复选框转换器指示位。在最坏的情况,你可以写一个机器人来测试所有你关心的组合。

For further example, here is a converter that uses checkboxes to indicate the bits. At worst, you could write a bot to test all of the combinations you care about.

在一般情况下,如果不能以不直接由二的幂的组合的一小部分进行说明,将是圆形的。 (如果你不能得到它的1/2 + 1/4 1/8 +等它可能会被四舍五入)

In general, if it cannot be described with a fraction that is not directly made of combinations of powers of two, it will be rounded. (If you can't get it with 1/2 + 1/4 + 1/8 etc. it will probably be rounded)

这篇关于计算转换十进制浮点时,哪些号码会出现问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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