浮点数和整数......?
为什么我们需要处理器中的整数和浮点数?谢谢
why do we need integers and floating in processor?thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
为什么我们需要处理器中的整数和浮点数?谢谢
why do we need integers and floating in processor?thank you
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
整数是用来计数的,浮点数是用来计算的。我们在数学中都有它们(它们分别称为整数和实数),因此我们在算法和程序中也需要它们。故事结束。
当然,大多数 fp 数字实现的范围大于大多数整数实现的范围,但我明天可以发明一种语言,其中允许 512 位整数,但只允许 16 位浮点数(1 个符号位,3 个指数位) ,12 个有效位)。整数在除法下仍然没有闭合,浮点数仍然无法用于计数,因为虽然 fp 数字有后继函数,但实数上没有后继函数,我们喜欢假装 fp 数字是实数。
不,整数对处理器来说并不容易,处理器对位进行基本的布尔逻辑运算。如果处理器 X1 的整数运算速度比 fp 运算速度快,则通过内存组进行搜索将找到反例。
我们甚至不需要分数的 fp 数字,我们可以使用整数对来表示分子和分母。
整数的绝对精度是我们使用它们进行计数的原因。出于所有实际目的,现有 fp 实现的精度就足够了(现在,有一个疯狂的主张引起了分歧!)
问候
Mark
Integers are for counting, floating-point numbers are for calculating. We have them both in maths (where they are called integers and real numbers respectively) so we need them in algorithms and in programs too. End of story.
Sure, the range of most fp number implementations is larger than the range of most integer implementations but I could invent a language tomorrow in which I allow 512-bit integers but only 16-bit floating-point numbers (1 sign bit, 3 exponent bits, 12 significand bits). The integers are still not closed under division and the floating-point numbers are still no use for counting because while there is a successor function on fp numbers there isn't on real numbers and we like to pretend that fp numbers are a close implementation of real numbers.
No, integers are not easier on the processor, the processor does fundamental boolean logic operations on bits. And if processor X1 does integer arithmetic faster than fp arithmetic, a trawl through the memory banks will find a counter example.
We don't even need fp numbers for fractions, we could use pairs of integers to represent numerator and denominator.
The absolute precision of integers is why we use them for counting. For all practical purposes the precision of existing fp implementations is enough (now, there's a wild claim to attract disagreement !)
Regards
Mark
它们都只是数字,所以您可能认为不需要区分。但是,在许多语言中,进行整数数学运算时都可以进行优化 - 使用 CPU 指令可以进行加法、减法、乘法和除法。同样,有些指令对浮点数执行类似的操作,但由于数字在机器中的表示方式不同,并且操作也不同,因此将处理器中明显的整数和浮点数之间的区别冒泡起来是有意义的编程语言本身。
C#、Java、C++ 和其他语言都有不同的类型来处理整数和浮点数。 Javascript 做出了相反的选择 - 没有特殊的整数类型,如果我没记错的话,所有数字都是浮点数。
至于为什么需要整数和浮点数 - 浮点数允许更广泛的值范围,尽管在极端情况下(例如天文数字)精度会下降。您无法在浮点数学中精确表示 1.37999933247474x10e24 。另一方面,整数为一组固定数字提供精度和速度。
They are all just numbers, so you might think you don't need to distinguish. But, in many languages there's an optimization possible when doing integer math - addition, subtraction, multiplying and dividing are possible with CPU instructions. Likewise there are instructions that do simnilar operations on floating point numbers, but because the numbers are represented differently in the machine, and operations are different, then it makes sense to bubble up the distinction between integers and floats that is apparent in the processor, into the programming language itself.
C#, Java, C++, and other languages all have distinct types for handling integers versus floats. Javascript makes the opposite choice - there is no special integer type, and if I am not mistaken, all numbers are floats.
As for why you need ints and floats - floats allow a much wider range of values, although at extreme ends (eg, astronomical quantities) the precision falls off. You can't represent 1.37999933247474x10e24 precisely in floating point math. On the other hand, ints offer precision and speed for a fixed set of numbers.
整数更容易占用处理器资源,而且通常更快。这在很多年前是一件大事,当时处理器甚至没有内置的浮点功能。现在没那么多了,但在紧凑的代码中差异仍然很大。
通常您只需要整数即可。
Integers are easier on processor resources, and often faster. This was a big deal many years ago, when processors didn't even come with built-in floating point capabilities. Not so much now, but the differences can still be significant in tight code.
Integers are often all that you need.
浮点值的范围比整数大得多。此外,它们还可以表示小数值。然而,这些功能的提供是以精度损失为代价的。
编辑:(我所说的精度损失)
整数算术总是精确的,只要不提供导致溢出或除以零的操作数即可。
浮点算术的情况并非如此,在简单运算中使用此类值时,某些部分的值可能会丢失。其原因是浮点值提供的巨大范围使得在[相对]较小的存储空间(通常为 8 或 16 字节)的情况下不可能表示该范围内的所有连续值。
Floating point values have a [much] greater range than integers. Also, they can represent fractional values. These features however are provided at the cost of a loss of precision.
Edit: (what I mean by loss of precision)
Integer arithmetic is always exact, so long that one doesn't provide operand which cause an overflow, or a division by zero.
This is not the case with floating point arithmetic, whereby some portions of values may be lost when using such values in simple operations. The reason for this is that the tremendous range offered by floating point values, is such that it is impossible to represent all contiguous values within the range, given the [relatively] small storage (typically 8 or 16 bytes).
整数是编程任务中最常用的东西。它们可以代表内存地址。从一个整数算到下一个整数很容易:只需加一即可。
浮点值用于近似实数。实数是连续数学中最常见的类型。连续数学用于表示现实世界。 (因此术语“实数”。)
浮点值通常不能用作整数。您无法轻松地从 X 数到下一个大于 X 的数字。它们会四舍五入,并且无法保证 X + 1 甚至与 X 是不同的数字。一般来说,如果两个浮点数可能不同是由不同的操作序列产生的,即使表达式应该是相等的。
浮点数是不可预测的,就像现实生活一样。整数是有序且高效的,就像计算机一样。
Integers are the most common things to use in programming tasks. They can represent memory addresses. It's easy to count from one integer to the next: just add one.
Floating-point values are used to approximate real numbers. Real numbers are the most common kind of thing in continuous math. Continuous math is used to represent the real world. (Hence the terminology "real number.")
Floating-point values cannot usually be used as integers. You can't easily count from X to the next number greater than X. They round off, and there is no guarantee that X + 1 is even a different number than X. Generally speaking, two floating-point numbers might be different if they were produced by different sequences of operations, even if the expressions are supposed to be equal.
Floating-point numbers are unpredictable, like real life. Integers are ordered and efficient, like computers.
在大多数应用中,通过仔细定义需要以什么精度表示什么范围的值,并乘以适当的缩放因子,可以用整数替换浮点数。然而,这是额外的开发工作,仅在小型嵌入式平台(即小型微控制器)上才值得,因为小型嵌入式平台无法在可用时间内进行浮点算术计算。
使用浮点数,只要保持在可用的范围和精度内,您就可以在大多数情况下无需考虑值的表示形式。不幸的是,这是相当危险的,因为这样当你离开安全区域时你可能不会注意到。
In most applications floating-point numbers can be replaced by integers, by carefully defining what range of values needs to be represented at what precision, and multiplying with appropriate scaling factors. However, this is additional development effort, which is only worthwhile on small embedded platforms (i.e. small microcontrollers) which can't do the calculations in floating-point arithmetic in the available time.
With floating-point numbers you can get away without thinking about the representation of values most of the time, as long as you stay within the available range and precision. Unfortunately this is rather dangerous, because that way you may not notice when you leave the safe region.
略有不同的观点:整数对于数字量有用,而浮点数对于模拟量有用。举个例子,在查看港口中的船只时,使用整数来计数船只,使用浮点数来表示水位。
A slightly different perpective: Integers are useful for digital quantities, while floats are useful for analogue quantities. An example, while looking at boats in the harbour, use ints to count the boats, use floats to represent the water level.