为什么我们需要整数和浮点数?谢谢
7 回答
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
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.
稍微不同的观点:整数对数字量很有用,而浮点数对模拟量很有用。举个例子,在查看港口的船只时,使用整数来计数船只,使用浮点数来表示水位。
在大多数应用程序中,浮点数可以用整数代替,方法是仔细定义需要以什么精度表示的值范围,并乘以适当的比例因子。然而,这是额外的开发工作,只有在无法在可用时间内进行浮点运算计算的小型嵌入式平台(即小型微控制器)上才值得。
只要您保持在可用范围和精度范围内,您就可以在大多数情况下不用考虑值的表示来摆脱浮点数。不幸的是,这是相当危险的,因为这样您在离开安全区域时可能不会注意到。
它们都只是数字,所以你可能认为不需要区分。但是,在许多语言中,在进行整数数学运算时可以进行优化 - CPU 指令可以进行加法、减法、乘法和除法。同样,也有对浮点数进行类似操作的指令,但是由于数字在机器中的表示方式不同,并且操作也不同,因此将处理器中明显的整数和浮点数之间的区别冒泡是有意义的,编程语言本身。
C#、Java、C++ 和其他语言都有不同的类型来处理整数和浮点数。Javascript 做出了相反的选择——没有特殊的整数类型,如果我没记错的话,所有数字都是浮点数。
至于为什么需要整数和浮点数 - 浮点数允许更广泛的值,尽管在极端情况下(例如,天文数量)精度会下降。您不能在浮点数学中精确地表示 1.37999933247474x10e24 。另一方面,整数为一组固定的数字提供精度和速度。
整数更容易占用处理器资源,而且通常更快。许多年前,这是一件大事,当时处理器甚至没有内置浮点功能。现在不是那么多,但在紧凑的代码中差异仍然很大。
整数通常是你所需要的。
浮点值的范围比整数更大。此外,它们可以表示小数值。然而,这些特性是以损失精度为代价的。
编辑:(我的意思是失去精度)
整数运算总是精确的,只要不提供导致溢出或被零除的操作数。
浮点运算不是这种情况,在简单的操作中使用这些值时,某些部分的值可能会丢失。这样做的原因是浮点值提供的巨大范围使得不可能表示范围内的所有连续值,因为[相对]较小的存储空间(通常是 8 或 16 字节)。