The most common way to represent numbers other than integers on computers is by using floating point, particularly IEEE 754 floating point. As you may be familiar with, integers are commonly represented by using hardware bits to represent binary numerals, so physical properties (such as charge or lack of charge, high voltage or low voltage, a magnetic field in one direction or another) are used to represent bits (0 and 1), and a sequence of those bits makes a numeral (such as 11010), which we interpret in binary to represent a number (110102 is 16+8+2 = 26). We do not usually think of it, but there is a “radix point” to the right of this numeral: “11010.” We only need the radix point when we have more bits to the right of it, which represent fractions. For example, 11010.112 is 16 + 8 + 2 + 1/2 + 1/4 = 26.75. To change from integers to floating point, we make the radix point float. In addition to the bits representing the numeral, we have some additional bits that tell us where to put the radix point.
因此,我们可能有三位(例如 010)来表示小数点的位置,而其他位(例如 1101011)则表示该值。小数点位 010 可能表示将小数点向左移动两个位置,更改为“1101011”。至“11010.11”。
在单精度 IEEE 754 中,有 1 个符号位(告诉我们 + 或 -)、8 个指数位和 23 个值位(用于“有效数字”或“小数”)。指数位的值 0 和 255 是特殊的。对于指数位的其他值,我们减去 127 以获得范围从 -126(小数点左移 126 位)到 127(小数点右移 127 位)的指数。有效数位被解释为二进制数字,只不过我们稍微修改了一下:我们写“1”,然后是小数点,然后是有效数的 23 位,所以我们有类似“1.1101011000…”的东西。作为替代方案,您可以将其视为一个整数:“1”然后是 23 位,没有插入小数点,形成 24 位二进制数字,但指数会额外调整 23(因此减去 150 而不是 127) 。
在双精度 IEEE 754 中,有 1 个符号位、11 个指数位和 52 个有效位。
还有其他不太常见的浮点格式。一些较旧的使用十六进制作为基数(使用指数来指示四位而不是一位的移位)。浮点格式的一种重要类型是十进制,其中指数表示 10 的幂。在十进制浮点中,有效数可以是二进制整数,也可以是二进制编码的十进制数(其中每四位表示一个十进制数字) )或者它可以是混合的(根据定制方案使用位组来指示少量十进制数字)。
浮点数的一个重要属性是它们不能表示所有实数(当然,即使是在有限范围内),甚至不能表示所有有理数。这迫使数学运算返回四舍五入为可表示数字的结果,这对于不熟悉浮点运算的人来说会带来无穷无尽的问题。此属性反过来又成为十进制浮点的一个功能:它非常适合处理货币面额和通常以十进制操作的其他与人类相关的数字,因为大多数舍入错误可以通过仔细使用十进制浮点来消除。科学家和数学家更多地使用与自然相关的数字或纯数字,而不是人类污染的数字,他们往往更喜欢二进制浮点,因为它更广泛可用并且得到硬件的良好支持。
There are other ways to represent non-integer numbers in computers. Another common method is fixed point. In fixed point, a sequence of bits, such as 1101011, is interpreted with a radix point at a known, fixed position. The position would be fixed at a position useful for a specific application. So the bits 1101011 could stand for the number 11010.112. An advantage of fixed point is that it is easily implemented with standard hardware. To add two fixed-point numbers, we simply add them as if they were integers. To multiply two fixed-point numbers, we multiply them as if they were integers, but the result has twice as many positions after the radix point, so we either shift the bits to adjust for this or we write our code so that the results of such operations are interpreted with the known number of bits after the radix point. Some processors have instructions to support fixed point by adjusting multiplications for this effect.
数字也可以缩放为整数。例如,要使用美国货币,我们只需将美元金额乘以 100 并使用整数进行所有算术运算。小数点仅在显示最终结果时插入(并在读取人类数据时进行解释)。另一种常见的缩放方法是通过乘以 255 来表示像素强度(从 0 到 1),以便从 0 到 1 的分数适合一个八位字节。
还有一些软件可以提供扩展精度(使用基本算术类型的多个单位来提供附加精度)或任意精度(使用动态数量的单位来提供所需的精度)。与硬件支持的算术相比,此类软件非常慢,并且通常仅用于特殊目的。此外,扩展精度与浮点本质上具有相同的属性;只是舍入误差变小了,而不是消失了。任意精度也有同样的缺陷,只不过它的动态精度可以让你把误差做得足够小,这样你就可以获得在必要区间内的最终结果(可能有证据证明你已经这样做了)。
表示非整数的另一种方法是使用分数。您可以存储分子和分母,并以与学校教授的方式大致相同的方式执行算术:通过乘以分子和乘以分母来进行乘法。通过将两个分数转换为具有公分母来进行加法,然后添加分子。这种算术是有问题的,因为分母很快就会变大,因此您需要扩展精度或任意精度来管理它们。
您还可以用符号或复合表达式来表示数字。例如,您可以使用表示应用于数字 2 的平方根运算的数据结构来存储它,而不是将二的平方根存储为数值。使用此类表示执行除最简单的运算以外的任何运算都需要非常复杂的软件来存储管理表达式、组合表达式、求约简等等。这种表示法用于专门的数学软件,例如 Maple 和 Mathematica。
最后,您可以以任何您想要的方式表示数字。我们的现代处理器是通用计算设备,达到了速度和存储容量的极限,因此您可以编写用字符串或数据结构或任何其他技术表示数字的算法。