What is the smallest exact representation of 1/(2^x) that can be represented in the C programming language?
What is the smallest exact representation of 1/(2^x) that can be represented in the C program开发者_StackOverflow中文版ming language?
On most platforms, C's double
is the same as the IEEE 754 double precision format. The closest positive value to zero supported there is 2^-1022 (which is equal to 1/2^1022).
However if you allow user defined types, there is no limit, as you can always express the exponent as a bigint.
Using IEEE-754 double
for arithmetic, the smallest exact value 1/2^n is:
- 2^-1022 if your platform does not have denormal support
- 2^-1023 if your platform has denormal support, but you insist on computing it using 1.0 / 2^n; this is because 2^1023 is the largest representable exact power of two in
double
. - 2^-1074 if your platform has denormal support and you don't mind directly specifying the value, eg with C99 hex floating-point notation:
0x1.0p-1074
or0x0.0000000000001p-1022
.
If you use another type, say long double
on an x86 machine with a compiler that maps that to 80-bit float, the smallest value can be quite a lot smaller (2^-16446, assuming that I did my arithmetic properly =)
If you use the GNU MP library (written in C), then you can represent any value up to the amount of RAM install.
0, that is 1/(2^inf) ;)
More seriously, this is a question of exponent bits in double precision floats. I don't think the C standard itself defines the size, but IEEE 754 does define it to have 11 exponent bits.
Lets ignore denormals for a little while. Since the smallest exponent value is −1022, this should be 1/(2^1022). But then there's the case of denormals, which IIRC should simply not contain any implicit 1 bit. The denormal numbers are thus spread uniformly over the 0..1/(2^1022)-range, giving log2(52) more values IIRC. So, I THINK the final answer should be 1/(2^(1074)).
If you store your variable as a 64-bit negative exponent, 1/2^(2^63 - 1). :)
That's a reeeeally small number.
精彩评论