定点数与浮点数
我只是无法理解定点数和浮点数,因为在谷歌上很难阅读它们的定义。但我读过的文章都没有对它们的真正含义提供足够简单的解释。我可以通过例子得到一个简单的定义吗?
I just can't understand fixed point and floating point numbers due to hard to read definitions about them all over Google. But none that I have read provide a simple enough explanation of what they really are. Can I get a plain definition with example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
定点数具有为整数部分(小数点左边的部分)保留的特定位数(或位数)和为小数部分(小数点右边的部分)保留的特定位数。观点)。无论您的数字有多大或多小,每个部分都将始终使用相同的位数。例如,如果您的定点格式为十进制
IIII.FFFFF
,则您可以表示的最大数字将为99999.99999
,最小的非零数字将为00000.00001
。处理此类数字的每一位代码都必须内置有关小数点位置的知识。浮点数不为整数部分或小数部分保留特定位数。相反,它为数字保留一定数量的位(称为尾数或有效数)以及一定数量的位来表示该数字中的位置小数点所在的数字(称为指数)。因此,占用 10 位数字并为指数保留 2 位数字的浮点数可能表示最大值
9.9999999e+50
和最小非零值0.0000001e-49< /代码>。
A fixed point number has a specific number of bits (or digits) reserved for the integer part (the part to the left of the decimal point) and a specific number of bits reserved for the fractional part (the part to the right of the decimal point). No matter how large or small your number is, it will always use the same number of bits for each portion. For example, if your fixed point format was in decimal
IIIII.FFFFF
then the largest number you could represent would be99999.99999
and the smallest non-zero number would be00000.00001
. Every bit of code that processes such numbers has to have built-in knowledge of where the decimal point is.A floating point number does not reserve a specific number of bits for the integer part or the fractional part. Instead it reserves a certain number of bits for the number (called the mantissa or significand) and a certain number of bits to say where within that number the decimal place sits (called the exponent). So a floating point number that took up 10 digits with 2 digits reserved for the exponent might represent a largest value of
9.9999999e+50
and a smallest non-zero value of0.0000001e-49
.定点数仅表示小数点后有固定位数。浮点数允许小数点后的位数变化。
例如,如果您有一种存储数字的方法,要求小数点后恰好有四位数字,那么它就是定点。如果没有这个限制,它就是浮点数。
通常,当使用定点时,程序员实际上使用一个整数,然后假设某些数字超出了小数点。例如,我可能想保留两位数的精度,因此 100 的值实际上意味着 1.00,101 意味着 1.01,12345 意味着 123.45 等。
浮点数更通用,因为它们可以表示非常小或非常大的数字以同样的方式,但是必须有额外的存储空间来存储小数位,这会带来一个小小的损失。
A fixed point number just means that there are a fixed number of digits after the decimal point. A floating point number allows for a varying number of digits after the decimal point.
For example, if you have a way of storing numbers that requires exactly four digits after the decimal point, then it is fixed point. Without that restriction it is floating point.
Often, when fixed point is used, the programmer actually uses an integer and then makes the assumption that some of the digits are beyond the decimal point. For example, I might want to keep two digits of precision, so a value of 100 means actually means 1.00, 101 means 1.01, 12345 means 123.45, etc.
Floating point numbers are more general purpose because they can represent very small or very large numbers in the same way, but there is a small penalty in having to have extra storage for where the decimal place goes.
根据我的理解,定点运算是使用整数完成的。其中小数部分存储在固定数量的位中,或者数字乘以需要多少位小数精度。
例如,如果需要存储数字
12.34
,并且我们只需要小数点后两位精度,则该数字乘以100
得到1234
。当对这个数字进行数学运算时,我们将使用这个规则集。将5620
或56.20
添加到该数字将在数据中产生6854
或68.54
。如果我们想计算定点数的小数部分,我们使用模(%)操作数。
12.34(伪代码):
浮点数在编程中是完全不同的情况。目前的浮点数标准使用 23 位来表示数字数据,8 位表示指数,1 位表示符号。 有关此内容的更多信息,请参阅此维基百科链接。
From my understanding, fixed-point arithmetic is done using integers. where the decimal part is stored in a fixed amount of bits, or the number is multiplied by how many digits of decimal precision is needed.
For example, If the number
12.34
needs to be stored and we only need two digits of precision after the decimal point, the number is multiplied by100
to get1234
. When performing math on this number, we'd use this rule set. Adding5620
or56.20
to this number would yield6854
in data or68.54
.If we want to calculate the decimal part of a fixed-point number, we use the modulo (%) operand.
12.34 (pseudocode):
Floating point numbers are a completely different story in programming. The current standard for floating point numbers use something like 23 bits for the data of the number, 8 bits for the exponent, and 1 but for sign. See this Wikipedia link for more information on this.
术语“定点”是指表示数字的相应方式,在小数点之后(有时在小数点之前)具有固定数量的数字。
通过浮点表示,小数点的位置可以相对于数字的有效数字“浮动”。
例如,具有统一小数点放置约定的定点表示可以表示数字 123.45、1234.56、12345.67 等,而浮点表示还可以表示 1.234567、123456.7、0.00001234567、1234567000000000 等。
The term ‘fixed point’ refers to the corresponding manner in which numbers are represented, with a fixed number of digits after, and sometimes before, the decimal point.
With floating-point representation, the placement of the decimal point can ‘float’ relative to the significant digits of the number.
For example, a fixed-point representation with a uniform decimal point placement convention can represent the numbers 123.45, 1234.56, 12345.67, etc, whereas a floating-point representation could in addition represent 1.234567, 123456.7, 0.00001234567, 1234567000000000, etc.
很少提及我认为定点数的定义特征。主要区别在于浮点数具有由舍入或截断引起的恒定相对(百分比)误差。定点数具有恒定的绝对误差。
使用 64 位浮点数,您可以确定
x+y
的结果偏差永远不会超过 1 位,但是一位有多大呢?嗯,这取决于x
和y
- 如果指数等于 10,则四舍五入最后一位表示错误2^10=1024
,但如果指数为0,则四舍五入一位是2^0=1
的错误。对于定点数,一位总是代表相同的数量。例如,如果小数点前有 32 位,小数点后有 32 位,则意味着截断错误最多只会将答案改变
2^-32
。如果您处理的数字大约等于1
,这很好,这样可以获得很高的精度,但如果您处理的数字具有不同的单位,那就不好了——谁在乎呢?您计算出一古戈尔米的距离,然后最终出现2^-32
米的误差?一般来说,浮点可以让您表示更大的数字,但对于中等大小的数字来说,代价是更高的(绝对)错误。如果您提前知道需要表示的数字有多大,则定点可以获得更高的精度,这样您就可以将小数精确地放在您想要的位置以获得最大精度。但是,如果您不知道正在使用的单位,那么浮点数是更好的选择,因为它们代表的范围很广,而且精度足够好。
There's very little mention of what I consider the defining feature of fixed point numbers. The key difference is that floating-point numbers have a constant relative (percent) error caused by rounding or truncating. Fixed-point numbers have constant absolute error.
With 64-bit floats, you can be sure that the answer to
x+y
will never be off by more than 1 bit, but how big is a bit? Well, it depends onx
andy
-- if the exponent is equal to 10, then rounding off the last bit represents an error of2^10=1024
, but if the exponent is 0, then rounding off a bit is an error of2^0=1
.With fixed point numbers, a bit always represents the same amount. For example, if we have 32 bits before the decimal point and 32 after, that means truncation errors will always change the answer by
2^-32
at most. This is great if you're working with numbers that are all about equal to1
, which gain a lot of precision, but bad if you're working with numbers that have different units--who cares if you calculate a distance of a googol meters, then end up with an error of2^-32
meters?In general, floating-point lets you represent much larger numbers, but the cost is higher (absolute) error for medium-sized numbers. Fixed points get better accuracy if you know how big of a number you'll have to represent ahead of time, so that you can put the decimal exactly where you want it for maximum accuracy. But if you don't know what units you're working with, floats are a better choice, because they represent a wide range with an accuracy that's good enough.
它是创建的,定点数不仅在点(数字)后有一些固定的小数位数,而且在数学上以负幂表示。非常适合机械计算器:
例如,smth 的价格是 USD 23.37(Q=点后 2 位数字。)机器知道该点应该在哪里!
It is CREATED, that fixed-point numbers don't only have some Fixed number of decimals after point (digits) but are mathematically represented in negative powers. Very good for mechanical calculators:
e.g, the price of smth is USD 23.37 (Q=2 digits after the point. ) The machine knows where the point is supposed to be!
取数字 123.456789
数字将为 123.46(假设您将其向上舍入)
浮点可让您以很高的精度表示几乎所有数字。固定的不太精确,但对于计算机来说更简单。
Take the number 123.456789
number would be 123.46 (Assuming you rounded it up)
Floating point lets you represent most every number with a great deal of precision. Fixed is less precise, but simpler for the computer..