How to check if a number has reached given precision? So right now I'm working on an algorithm which has to know when to terminate its calculations - namely, it should do it when the number he's acquired in the last step is at least as precise as the user wanted the result to be. Providing I can't just change the number to a stirng and check each character from given till the end or use some functions checking length of the number, how can I do this?
I mean: in $k$-th step we get $x_i=1.548168$. The wanted precision is $p=10^{-4}$. How can I check that there in fact is something present on the 4th place after the dot or farer?
 A: First, be sure that your internal precision is larger than the precision the user could ever possibly want.
There is no simple answer. As your algorithm is working, it's producing a sequence of approximations $x_i$ which converge to the optimal result $x$: $\lim_{i\to\infty}x_i=x$. But unless you know something about the algorithm, you never know when you can stop - after producing $i$ numbers you have no guarantee that $x_{i+1}$ doesn't jump off suddenly.
I'd say the only reasonable way is to modify your algorithm so that it doesn't produce a sequence of approximations, but a sequence of intervals $[y_i,z_i]$ such that $[y_{i+1},z_{i+1}]\subseteq [y_i,z_i]$ and $\lim_{i\to\infty} z_i-y_i = 0$. This way, when your interval is small enough you know you've reached the desired precision

To illustrate it, let's suppose you're computing an approximation of a number given as a infinite continued fraction. Then we know that


*

*The even convergents (before the $n$th) continually increase, but are always less than $x_n$.

*The odd convergents (before the $n$th) continually decrease, but are always greater than $x_n$.


So you always know that the limit is between $x_n$ and $x_{n-1}$.

Perhaps post more details about your algorithm.
A: Typically you just test whether $|x_{i-1}-x_i|<p$.
