As you pointed out, irrationals never have a terminating representation. So let's restrict our attention to rationals.
I believe that a rational number $p/q$ (in lowest terms) is representable as a terminating floating point expression in base $b$ if and only if each prime factor of $q$ is a divisor of $b$.
This is because if you write the fractional part of a number as $x=0.a_1a_2a_3\ldots$, then it terminates if there is integral $n$ with $x=0.a_1a_2a_3\ldots a_n$ in base $b$. That's the same as saying $b^n x=m\in \mathbb{Z}$, so you can write
$$x=\frac{m}{b^n}$$
Supposing for simplicity that the original number is between $0$ and $1$ (so you only have to deal with the fractional part--that's okay because adding integers doesn't affect the part past the radix point), this means you can write the number as a fraction whose denominator is a power of $b$ (this form will probably not be in lowest terms).
That's another way to say it--there is a terminating base $b$ floating point representation of $x$ if and only if $x=m/b^n$ for some integers $m$ and $n$.
In contrast with other programming languages from its time, Pascal (the programming language)
also supports a set type, implemented as a bit pattern. The bit patterns associated with
the Pascal set type are $256$ bits wide, but this limitation is not essential and can been replaced
with other (larger) values eventually.
See Wikipedia for a reference.
A rather detailed description of the set type implementation can be found
as well .
So we have the following practice:
- a bit pattern in a computer is a set type
We also know that
- a bit pattern in a computer is a natural number type
Indeed, everybody knows that a natural number can be represented as a binary i.e. a bit pattern.
The word "type" has been employed here in order to avoid confusion with other
(i.e the standard mathematical) "set" and "number" definitions.
More precisely: the
hereditarily finite sets
are in one-to-one correspondence with the natural numbers. And the latter fact is independent of
computers.
Examples.
$$
\begin{array}{l}
0 = 000 = \{\} \\
1 = 001 = \{0\} = \{\{\}\} \\
2 = 010 = \{1\} = \{\{\{\}\}\} \\
3 = 011 = \{0\; 1\} = \{\{\}\{\{\}\}\} \\
4 = 100 = \{2\} = \{\{\{\{\}\}\}\} \\
5 = 101 = \{0\; 2\} = \{\{\}\{\{\{\}\}\}\} \\
6 = 110 = \{1\; 2\} = \{\{\{\}\}\{\{\{\}\}\}\} \\
7 = 111 = \{0\; 1\; 2\} = \{\{\}\{\{\}\}\{\{\{\}\}\}\} \\
\cdots
\end{array}
$$
The above is related to the following reference, by Alexander Abian and Samuel LaMacchia:
If the curly brackets $\{\}$ are replaced by square brackets $\left[\,\right]$ then another
important fact is observed:
- a set type is is a natural number type is a sorted natural numbers array type
If now we devise
an equivalent of the elementary number operations with sorted arrays,
then we have virtual unlimited precision at our disposal. That this approach indeed works, shall be demonstrated at hand of the OP's question.
Here is a link to the complete (Delphi Pascal) program that does the job:
And here is a link to the number $3^{100000}$ itself, which is too large to fit into
MSE's margins:
The screen output of the program is the number of digits, the first, the middle and the last digit:
47713
1 2 1
So, indeed, as
Lucian says in a comment: it's "obvious" that the digit in the middle is a $\large\, 2$ .
Note. A power like $\,3^{100000}\,$ sounds quite impressive, but with a smart
algorithm, the number of operations is only $\,\ln_2(100000)\approx17$ . For
real numbers $\,x\,$ and a natural $\,n\,$ it goes as follows:
function power(x : double; n : integer) : double;
var
m : integer;
p, y : double;
begin
m := n; y := x; p := 1;
while m > 0 do begin
if (m and 1) > 0 then p := p * y;
m := m shr 1; { m := m / 2 }
y := y * y;
end;
power := p;
end;
Wikipedia reference:
Efficient computation with integer exponents .
BONUS. In view of the above, the following answer is interesting:
Reference is made to a
paper by Kaye and Wong,
where on page 499 we read:
It was observed in 1937 by Ackermann [1] that $\mathbb{N}$ with the membership relation
defined by
$n \in m$ iff the $n$th digit in the binary representation of $m$ is $1$
satisfies ZF$-$inf. This interpretation, formalized in ZF with $\omega$ in place of $\mathbb{N}$
yields a bijection between $\omega$ and the collection $V_\omega$ of hereditarily finite sets.
Best Answer
There is no known solution for all numbers, but I found a quick solution for a significant amount of cases, which is useful for optimizing programs (try this, if it fails do something else, like squaring repeatedly).
Part 1: define edge cases
Let's construct a number whose fractional part has $z$ leading zeros followed by $n$ consecutive ones, and see to what power $2^k$ do we need to raise it to get it between 2 and 4. (This leads to a more complete solution, you'll see later.)
To produce a chain of $n$ ones in base 2, all we need is to subtract 1 from the $n$th power of 2. For example $2^4-1=15$ and the base 2 representation of 15 is $1111$.
The next thing to do is to divide it by $2^n$ to move the 1-digits to the right side of the decimal point. To add $z$ leading zeros, just divide it by $2^z$. And finally, +1 to make it a number between 1 and 2.
$$2 < \left(\frac{2^n-1}{2^{n+z}}+1\right)^{2^k} < 4$$
Part 2: isolate $k$
For brevity, let's call the part in the brackets $m$:
$$2 < m^{2^k} < 4$$
Take the logarithm (base 2) of the inequality:
$$1 < 2^k\log_2(m) < 2$$
Divide by the logarithm:
$$\frac{1}{\log_2(m)} < 2^k < \frac{2}{\log_2(m)}$$
Take the logarithm again:
$$- \log_2(\log_2(m)) < k < 1 - \log_2(\log_2(m))$$
Part 3: find the integer values of $k$
Since $k$ is a whole number, and there is only one valid value of $k$ that solves this inequality at any given point $(z,n)$, we can take the lower limit and round it up:
$$k = \lceil - \log_2(\log_2(m))\rceil = - \lfloor\log_2(\log_2(m))\rfloor = -\operatorname{msb}(\log_2(m))$$
Where $\operatorname{msb}(x)$ is the position of the most significant bit (digit) of $x$, which for $0<\log_2(m)<1$ is negative. (Note that $\lfloor\log_2(x)\rfloor = \operatorname{msb}(x)$ is correct only for this range.)
$1<m<2$, and in this range $\log_2(x) \approx x-1$, but remember that $\log_2(x) > x-1$ in this range, so it will throw the $\operatorname{msb}$ function result off in some cases (it will be too big by +1).
$$k \approx -\operatorname{msb}(m-1) = -\operatorname{msb}\left(\frac{2^n-1}{2^{n+z}}\right) = z+1$$
For very small numbers ($z>2$) the approximation is always wrong, this makes the result reliable in this range. Manually testing some cases of small $z$ and $n$ leads to a complete answer:
$$k = \begin{cases} 1 & & z = 0\\ z+1 & n = 1 & z > 0\\ z+1 & n = 2 & 1 ≤ z ≤ 2\\ z & n = 2 & z > 2\\ z & n > 2 & z > 0 \end{cases}$$
Part 4: fill in the gaps
Until now we focused on very specific numbers, now let's take care of all the numbers in between.
Let $C(z,n)$ be the set of all numbers whose base 2 representation starts with:
$$1.\underbrace{0000000000}_{z\text{ zeros}}\underbrace{111111111}_{n\text{ ones}}0$$
For a fixed value of $z$, if $k$ is the same for $n=x$ and $n=x+1$, then it is also the same for every number in between (in $C(z,x)$), because if two numbers reach the range between 2 and 4 when raised to the same power, then all the numbers between them will also reach that range with the same power. Otherwise there are 2 possible answers with a difference of 1.
To sum it up, every number $1<x<2$ is in a set of the form $C(z,n)$. By counting the consecutive zeros ($z$) after the decimal point and then the consecutive ones ($n$) we can either know the answer with certainty or at least know that it is one of two numbers, as detailed in this table: