分数在计算机中如何表示? [英] How are fractions represented in computers?

查看:558
本文介绍了分数在计算机中如何表示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于计算机从"1"和"0"的角度来考虑,它们如何计算和表示分数(例如7.50)?我知道Java和JavaScript,如果需要答案,您可以使用它们作为示例.

Since computers think in terms of "1" and "0" how do they calculate and represent fractions such as 7.50 ? I know Java and JavaScript and if required for the answer you can use them as an example .

编辑:我正在观看

Edit: I was watching this MIT video on hashing by Prof. Cormen at 46:31 seconds he explains the multiplication hash function using a modular wheel which is a unit circle with several points in it and the points denote fractions. This prompted me to ask this basic question here in SO .

推荐答案

在计算机上表示整数以外的数字的最常见方法是使用浮点,尤其是IEEE 754浮点.如您所知,整数通常通过使用硬件位来表示二进制数字来表示,因此物理属性(例如,带电或不带电,高电压或低电压,一个方向或另一个方向的磁场)用于代表位(0和1),并且这些位的序列构成一个数字(例如11010),我们用二进制对其进行解释以表示一个数字(11010 2 为16 + 8 + 2 = 26 ).我们通常不会想到它,但是在此数字的右边有一个基数点":"11010".只有当小数点右边有更多位(代表小数)时,才需要小数点.例如,11010.11 2 是16 + 8 + 2 + 1/2 + 1/4 = 26.75.要从整数更改为浮点,我们使基数浮点.除了代表数字的位以外,我们还有一些其他位可以告诉我们将小数点放在何处.

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.

因此,我们可能有3个位(例如010)来表示小数点所处的位置,而其他位(例如1101011)则表示该值.小数点位010可能表示将小数点向左移动两个位置,从而更改为"1101011".改为"11010.11".

So, we might have three bits, say 010, to say where the radix point goes and other bits, say 1101011, to represent the value. The radix-point bits, 010, might say to move the radix point two positions left, changing "1101011." to "11010.11".

在单精度IEEE 754中,有一个符号位(告诉我们+或-),八个指数位和23个值位(用于有效位数"或分数").指数位的值0和255是特殊的.对于指数位的其他值,我们减去127得到的指数范围为-126(将基数点左移126位)到127(将基点点右移127位).有效位数被解释为二进制数字,只是稍稍修改一下:我们先写"1",然后是一个小数点,然后是有效位数的23位,所以我们有类似"1.1101011000…"的内容.或者,您可以将其视为一个整数:"1"然后是23位,没有插入的小数点,为24位二进制数,但是指数被额外的23调整(所以减去150而不是127)

In single-precision IEEE 754, there is one sign bit (that tells us + or -), eight exponent bits, and 23 value bits (for the "significand" or "fraction"). The values 0 and 255 of the exponent bits are special. For other values of the exponent bits, we subtract 127 to get exponents ranging from -126 (shift the radix point 126 bits left) to 127 (shift the radix point 127 bits right). The significand bits are interpreted as a binary numeral, except that we modify them a little: We write "1", then a radix point, then the 23 bits of the significand, so we have something like "1.1101011000…". As an alternative, you can think of this as an integer: "1" then 23 bits with no inserted radix point, making a 24-bit binary numeral, but the exponent is adjusted by an extra 23 (so subtract 150 instead of 127).

在双精度IEEE 754中,有一个符号位,11个指数位和52个有效位.

In double-precision IEEE 754, there is one sign bit, 11 exponent bits, and 52 significand bits.

还有其他一些不太常见的浮点格式.一些较旧的以十六进制为基数(使用指数表示四位而不是一位的移位).浮点格式的一种重要类型是十进制,其中指数表示10的幂.在十进制浮点中,有效位数可以是二进制整数,也可以是二进制编码的十进制数字(其中每个四位表示一个十进制数字) ),也可以是混合符号(根据自定义方案,比特组用于表示少量的十进制数字).

There are other floating-point formats, which are less common. Some older ones use hexadecimal as a base (using the exponent to indicate shifts of four bits instead of one). An important type of floating-point format is decimal, where the exponent indicates powers of 10. In decimal floating point, the significand can be a binary integer or it can be a binary-coded decimal number (where each four bits indicates a decimal digit) or it can be a hybrid (groups of bits are used to indicate a small number of decimal digits according to a customized scheme).

浮点数的一个重要属性是它们不能表示所有实数(当然,即使是在有限范围内),甚至不能表示所有有理数.这迫使数学运算返回四舍五入为可表示数字的结果,对于不熟悉浮点运算的人来说,这不会造成任何问题.此属性进而成为十进制浮点数的功能:它适用于使用通常以十进制处理的货币面额和其他人为关联的数字,因为可以通过谨慎使用十进制浮点数来消除大多数舍入错误.科学家和数学家更多地使用与自然相关的数字或纯数字而不是人类污染的数字,他们倾向于使用二进制浮点,因为它的可用范围更广,并且得到硬件的很好支持.

An important property of floating-point numbers is that they cannot represent all real numbers (even in a finite range, of course) or even all rational numbers. This compels mathematical operations to return results rounded to representable numbers, which causes no end of problems for people unfamiliar with working with floating point. This property in turn becomes a feature of decimal floating point: It is good for working with currency denominations and other human-associated numbers that are usually manipulated in decimal, because most rounding errors can be eliminated by careful use of decimal floating point. Scientists and mathematicians, who work more with nature-associated or pure numbers instead of human-contaminated numbers, tend to prefer binary floating point, because it is more widely available and is well supported by hardware.

还有其他方法可以表示计算机中的非整数.另一个常见的方法是定点.在固定点上,使用已知的固定位置的小数点解释位序列(例如1101011).该位置将固定在对特定应用有用的位置.因此,位1101011可以代表数字11010.11 2 .定点的优点是可以使用标准硬件轻松实现.要添加两个定点数,我们只需将它们视为整数就相加.若要将两个定点数相乘,我们将它们像整数一样相乘,但是结果在小数点后的位置增加了两倍,因此我们要么移位这些位以进行调整,要么编写代码以使结果为用小数点后的已知位数解释这些操作.一些处理器具有通过调整乘法来支持定点来实现此效果的指令.

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的分数适合一个8位字节.

Numbers can also be scaled to integers. For example, to work with United States currency, we simply multiply dollar amounts by 100 and do all arithmetic with integers. The radix point is only inserted when displaying final results (and is interpreted when reading data from humans). Another common scaling is to represent pixel intensities (from 0 to 1) by multiply by 255, so that fractions from 0 to 1 fit into an eight-bit byte.

也有提供扩展精度(使用基本算术类型的多个单位提供附加精度)或任意精度(使用动态数量的单位提供所需精度的软件)的软件.与硬件支持的算法相比,此类软件非常慢,并且通常仅用于特殊目的.此外,扩展精度具有与浮点基本相同的属性;只是舍入误差较小而没有消失.任意精度具有相同的缺陷,不同之处在于它的动态精度可能使您使误差足够小,从而可以在必要的时间间隔内获得最终结果(并证明您已这样做).

There is also software to provide extended precision (use several units of the basic arithmetic type to provide additional precision) or arbitrary precision (use a dynamic number of units to provide as much precision as desired). Such software is very slow compared to hardware-supported arithmetic and is typically used only for special purposes. Additionally, extended precision has essentially the same properties as floating point; it is just that the rounding errors are smaller, not gone. Arbitrary precision has the same flaw except that its dynamic precision may allow you to make the error small enough that you can obtain a final result that is within a necessary interval (with proof that you have done so).

表示非整数的另一种方法是使用分数.您可以存储分子和分母,并以与学校所教的几乎相同的方式执行算术:通过乘以分子和乘以分母来相乘.通过将两个分数转换为具有共同的分母来进行加法,然后添加分子.这种算法是有问题的,因为分母会很快变大,因此您需要扩展精度或任意精度来管理它们.

Another way to represent non-integers is by using fractions. You can store a numerator and a denominator, and perform arithmetic in much the same way taught in school: Multiply by multiplying numerators and multiplying denominators. Add by converting both fractions to have a common denominator, then add numerators. This sort of arithmetic is problematic because denominators very quickly become large, so you need extended precision or arbitrary precision to manage them.

您也可以用符号或复合表达式表示数字.例如,您可以使用代表表示对数字2进行平方根运算的数据结构来存储它,而不是将数字2的平方根存储为数值.使用此类表示法执行除最简单的运算之外的任何运算都需要非常复杂的软件来管理表达式,将它们组合起来,找到约简,等等.这种表示形式用于Maple和Mathematica等专用数学软件中.

You can also represent numbers symbolically or with compound expressions. For example, instead of storing the square root of two as a numerical value, you can store it with a data structure that represents the square root operation applied to the number 2. Performing any but the simplest operations with such representations requires very complicated software to manage expressions, combine them, find reductions, and so on. This sort of representation is used in specialized math software, such as Maple and Mathematica.

最后,您可以按任意方式表示数字.我们的现代处理器是通用计算设备,不受速度和存储容量的限制,因此您可以编写用字符串或数据结构或任何其他技术表示数字的算法.

Finally, you can represent numbers any way you want. Our modern processors are general-purpose computing devices, up to the limits of their speed and storage capacity, so you can write algorithms that represent numbers with strings or data structures or any other technique.

这篇关于分数在计算机中如何表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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