到底什么是浮动?
是的,本世纪的菜鸟问题...但说实话,我实际上不知道浮点值实际上是什么...更重要的是,浮点值和整数之间的区别。
提前致谢!
Yeah, the noob question of the century... But in all seriousness, I actually have no idea what a float value actually is... And more importantly, the difference between a float and an integer.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请参阅
基本上,浮点数据类型是一种以科学记数法存储数值的方法。您可能会写 2.3789 x 10^12,但由于计算机以二进制工作,因此它是二进制科学记数法。
在软件中,浮点值用大小来换取绝对精度(您只能分散这么多位)。
整数是……嗯……整数。最右边(低位)代表 2^0(例如 1),下一位代表 2^1(2s),接下来的 2^2(4)等等。最左边(高位)位表示符号(0 为正,1 为负)。这给我们带来了负值:它们以所谓的补码表示法表示。要获得正数的补码:
因此,一个八位正 1(二进制为 00000001)通过
00000001
转换为其负表示(11111111)原始值。11111110
翻转其位11111111
Add 1补码表示法的优点是加法和减法使用相同的电路:减法是通过加法实现的:使用 are +1/-1上面的例子,结果为零:添加最右边的位,得到二进制 10(十进制 2)。将其带到下一列,重复添加。进位通过符号位传播,给出最终值 00000000。
曾经有使用其他表示形式的系统,但大多数(如果不是全部)现代计算机使用补码表示法。
当然,还有位顺序(大端与小端),但那是另一个故事了。
See
Basically, the floating point data type is a way of storing a numeric value in scientific notation. You might write 2.3789 x 10^12, but since computers work in binary, it's a binary scientific notation.
Floating point values, in software, trade magnitude for absolute precision (you've only got so many bits to spread around).
Integers are...well..integers. The rightmost (low-order bit) represents 2^0 (e.g., 1), the next bit represents 2^1 (2s), the next 2^2 (4) and so forth. The leftmost (high-order) bit represents the sign (0 is positive, 1 is negative). Which brings us to negative values: they are represented in what's called two's-complement notation. To get the two's complement of a positive number:
So an eight-bit positive 1 (in binary, 00000001) get converted to its negative representation (11111111) by
00000001
The original value.11111110
Flip its bits11111111
Add 1The advantage of two's complement notation is that addition and subtraction use the same circuitry: subtraction is implemented via addition: Using are +1/-1 example above, yields zero: add the rightmost bits giving you a binary 10 (decimal 2). Carry that to the next column, repeat the addition. The carry propagates out through the sign bit giving you the final value of 00000000.
There used to be systems that used other representations, but most (if not all) modern computers use two's-complement notation.
Then, of course, there is bit-order (big-endian v. little-endian) but that's another story.
我想扩展尼古拉斯·凯里在他的回答中提到的一些内容:
这就是为什么我们称它们为“浮点数”——我们允许小数点“浮动”,具体取决于我们要写入的数字有多大。
我们举一个十进制的例子。假设给你 5 个单元格来写下一个数字:_ _ _ _ _ 。如果不使用小数点,则可以表示 0 到 99999 之间的数字。但是,可以表示的最小差值是 1。
现在您决定需要写下美元金额,因此添加两位数的小数点从左起:_ _ _ 。 _ _ 这称为定点算术。现在您只能写从 0 到 999.99 的数字,但您现在可以表示 1 美分或 0.01 的差异。
如果您想使用此程序来支付日常小额开支和所得税怎么办?您可以允许小数点“浮动”并使用以下数字之一表示其位置: [_] _ _ _ _
例如,您的银行利息可能是 [3] 4 7 6 5 代表 4.765,您的杂货账单可能是[2] 5 9 8 2 (59.82),您的租金支付 1 8 7 5 9 ( 875.9 ) 和您的所得税申报表 [0] 2 3 8 9 (2389)。您甚至可以允许小数点位于数字之外,如下所示:[-1] 4 5 9 8 表示 4598x10 = 45,980。
请注意,您现在可以表示非常小和非常大的数字,但无法准确表示所有数字。例如,在写入 [0]2389 时,我们失去了分。
更传统的做法是始终将浮点数视为科学记数法,例如 4.598x10^4,其中“4.598”称为有效数,“4”是指数,“10”是基数。其他人提到的链接有有关实际存储格式的更多详细信息。
I want to expand on something that Nicholas Carey mentioned in his answer:
This is the reason why we call them "floating point numbers" - we allow the decimal point to "float" depending on how big the number that we want to write is.
Let's give an example in decimal notation. Suppose that you are given 5 cells to write down a number: _ _ _ _ _ . If you don't use decimal points, then you can represent numbers from 0 to 99999. However, the smallest difference that you can represent is 1.
Now you decide that you need to write down dollar amounts so you add a decimal point two digits from the left: _ _ _ . _ _ This is called fixed point arithmetic. Now you can only write numbers from 0 to 999.99, but you can now represent a difference of one cent or 0.01
What if you wanted to use this program for both small day-to-day expenses and for your income tax? You can allow the decimal point to 'float' and represent it's position using one of the digits: [_] _ _ _ _
For example, your bank interest may be [3] 4 7 6 5 representing 4.765, your grocery bill may be [2] 5 9 8 2 (59.82), your rent payment 1 8 7 5 9 ( 875.9 ) and your income tax return [0] 2 3 8 9 (2389). You may even allow the decimal point to venture outside the digits like this: [-1] 4 5 9 8 represents 4598x10 = 45,980.
Note that you can now represent both very small and very big numbers, but you cannot represent all numbers exactly. For example, in writing [0]2389 we loose the cents.
It is more conventional to always think of floating point numbers in scientific notation like 4.598x10^4 where '4.598' is called the significand, '4' is the exponent, and '10' is the radix. The links mentioned by others have more detail on the actual storage format.
在这种情况下,维基百科是你最好的朋友:
虽然您可能应该从 数据类型概述
此外,数据类型的实现因语言和处理器架构而异(例如,整数在不同架构上的位数不同),但如果使用 C 作为规范(C是系统编程的事实上的标准,并且在抽象方面被认为“略高于”汇编语言),那么这些资源应该具有进一步的启发性:
In this case, Wikipedia is your best friend:
Though you should probably start just on the data types overview
Also, implementations of data types vary by language and processor architectures (e.g. integers are different numbers of bits on different architectures), but if using C as canon (C is the de-facto standard for system programming and is considered "just-above" assembly language in terms of abstraction) then these resources should be further enlightening:
基本术语:
浮点值是允许有小数点的实数。
整数值没有小数点(如果您愿意,可以是整数)
In basic terms:
Floating point value is a real number which is allowed to have decimal points.
Integer value has no decimal points (a whole number if you like)
让我帮你谷歌一下!
http://en.wikipedia.org/wiki/Primitive_data_type#Floating-point_numbers
对于专业人士和初学者来说,这本书非常有趣!享受。
Let me google this for you!
http://en.wikipedia.org/wiki/Primitive_data_type#Floating-point_numbers
Pretty interesting read for pros and beginners! Enjoy.
每个计算机科学家都应该了解的浮点运算知识
http://download.oracle.com/docs/ cd/E19957-01/806-3568/ncg_goldberg.html
使用 IEEE 浮点运算进行数值计算
http://www.amazon.com/Numerical-Computing-Floating-Point-Arithmetic/ dp/0898714826/ref=wl_it_dp_o?ie=UTF8&coliid=IUNMIY7UE6ZS2&colid=2UDAD4OEO371J
IEEE 754-1985
http://en.wikipedia.org/wiki/IEEE_754-1985
What every computer scientist should know about floating-point arithmetic
http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html
Numerical Computing with IEEE Floating-Point Arithmetic
http://www.amazon.com/Numerical-Computing-Floating-Point-Arithmetic/dp/0898714826/ref=wl_it_dp_o?ie=UTF8&coliid=IUNMIY7UE6ZS2&colid=2UDAD4OEO371J
IEEE 754-1985
http://en.wikipedia.org/wiki/IEEE_754-1985
float 是一种数据类型。
http://en.wikipedia.org/wiki/Floating_point
float is a datatype.
http://en.wikipedia.org/wiki/Floating_point