.NET中的浮点数范围? [英] Range of floating point numbers in .NET?
问题描述
一本书的摘录:
浮点值由一个24位的
尾数和一个8位的尾数组成签署了
指数。精度大约是七个十进制数字。
的值
的范围从
-3.402823×10 ^ 38到3.402823×10 ^ 38
A float value consists of a 24-bit signed mantissa and an 8-bit signed exponent. The precision is approximately seven decimal digits. Values range from -3.402823 × 10^38 to 3.402823 × 10^38
如何计算此范围?有人可以解释二进制算术吗?
How to calculate this range? Can someone explain the binary arithmetic?
推荐答案
我一定会读Richard所指向的文章。但是,如果您需要一个简单的解释,希望对您有所帮助:
I would definitely read the article to which Richard points. But if you need a simpler explanation, I hope this helps:
基本上,正如您所说的那样,有1个符号位,8个指数位和23个分数位。
然后,使用此等式(来自维基百科)
Basically, as you said, there is 1 sign bit, 8 bits for exponent, and 23 for fraction. Then, using this equation (from Wikipedia)
N =(1-2s)* 2 ^(x-127)*(1 + m * 2 ^ -23)
其中 s
是符号位, x
是指数(减去127的偏差),而 m
是作为整数处理的小数部分(上面的公式将整数转换为适当的分数值)。
where s
is the sign bit, x
is the exponent (minus the 127 bias), and m
is the fractional part treated as a whole number (the equation above transforms the whole number into the appropriate fraction value).
请注意, 0xFF
的指数值被保留以表示无穷大。因此,实际值的最大指数为 0xFE
。
Note, that the exponent value of 0xFF
is reserved to represent infinity. So the largest exponent of a real value is 0xFE
.
您会看到最大值为
N =(1-2 * 0)* 2 ^(254-127)*(1 +(2 ^ 23-1)* 2 ^ -23 )
N = 1 * 2 ^ 127 * 1.999999
N = 3.4 x 10 ^ 34
最小值相同,但设置了符号位,这将简单地取反该值,从而得到 -3.4 X 10 ^ 34
。
The minimum value would be the same but with the sign bit set, which would simply negate the value to give you -3.4 X 10^34
.
这篇关于.NET中的浮点数范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!