什么类型的数字可以用二进制浮点表示? [英] What types of numbers are representable in binary floating-point?
问题描述
我知道, 1 / pow(2,n)
,其中 n
是一个整数的小数部分可以精确地表示浮点数字。这意味着如果我给自己加上3200万次 1/32
,我会得到 1,000,000
。
那么像 1 /(32 + 16)
?这是两个两个权力的总和,这个工作吗?或者它是 1/32 + 1/16
的工作?这是我困惑的地方,所以如果有人可以澄清,我会感激。
- 如果分母的素因子分解只包含2,则可以用二进制表示一个数字。(即分母是一种权力的二)
$ b $所以1 /(32 + 16)
不能用二进制表示,因为分母的因子是3。但是1/32 + 1/16 = 3/32
就是。
也就是说,可以用浮点型表示。例如,在IEEE
double
所以1/2 + 1/2 ^ 500
中只有53位尾数,是不可表示的。
因此,只要指数的范围不超过53次幂,就可以做两次幂的和。
将其推广到其他基础:
-
如果分母的素因式分解仅由2和5组成,那么数字可以用10进行精确表示。
-
有理数<如果
X 的分母的素因式分解,code> X
code>只包含N
因式分解中的素数。
I've read a lot about floats, but it's all unnecessarily involved. I think I've got it pretty much understood, but there's just one thing I'd like to know for sure:
I know that, fractions of the form
1/pow(2,n)
, withn
an integer, can be represented exactly in floating point numbers. This means that if I add1/32
to itself 32 million times, I would get exactly1,000,000
.What about something like
1/(32+16)
? It's one over the sum of two powers of two, does this work? Or is it1/32+1/16
that works? This is where I'm confused, so if anyone could clarify that for me I would appreciate it.解决方案The rule can be summed up as this:
- A number can be represented exactly in binary if the prime factorization of the denominator contains only 2. (i.e. the denominator is a power-of-two)
So
1/(32 + 16)
is not representable in binary because it has a factor of 3 in the denominator. But1/32 + 1/16 = 3/32
is.That said, there are more restrictions to be representable in a floating-point type. For example, you only have 53 bits of mantissa in an IEEE
double
so1/2 + 1/2^500
is not representable.So you can do sum of powers-of-two as long as the range of the exponents doesn't span more than 53 powers.
To generalize this to other bases:
A number can be exactly represented in base 10 if the prime factorization of the denominator consists of only 2's and 5's.
A rational number
X
can be exactly represented in baseN
if the prime factorization of the denominator ofX
contains only primes found in the factorization ofN
.
这篇关于什么类型的数字可以用二进制浮点表示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-