I'm sorry, but you are simply wrong. When a number is TOO small, it does not turn into a NaN. It turns into either zero, or -inf. Which it is depends on whether you are talking about how near a number is to -infinity or to zero.
So I kept on dividing by 2, stopping only when an underflow happened, and both values are now zero. Are they EXACTLY zero?
So yes. They are EXACTLY zero.
There are many computations that will produce a NaN. NaNs are created where a result is indeterminate. For example, there is no way to consistently define the value 0/0 in an intelligent way using limits. So we say that 0/0 is indeterminate.
SImilarly, we cannot define the ratio of two infinities as anything valid. Nor can we dinfine the difference of two infinities. So we have
You would also guess that things like sin(inf) have no value we could assign. This is because sin(X) is a periodic function, so as we take a limit, we get nothing meaningful.
But it is NEVER true that a number will be so small as to become NaN.
Could you have a situation where a ratio approaches zero, and then at some point suddenly creates a NaN? Well yes. But this is not because the number was getting too small. For example...
MATLAB is smart enough to know this limit to be zero. But now try this:
So the ratio becomes smaller and smaller. Eventually, it becomes 0. And then, suddenly, we get a NaN. WHAT HAPPENED? I thought I just said this cannot happen?
In fact, I was correct. What happened was at the point where X overflowed to inf, then X^2 is also inf. Now MATLAB computes inf/inf, and returns a NaN. The problem was NOT that the ratio was too small, but that the numerator AND the denominator were BOTH infinities. And what did I say inf/inf will produce? Correct: NaN.
Similarly, when numbers approach -infinity, they do not suddeny become NaN. Instead, they become -inf at the point of overflow for a double. But never NaN.
Whatever is producing NaNs, this we cannot guess, since you tell us too little information.
You do ask two other questions. Technically, the smallest number representable in MATLAB is realmin/2^52.
Below the level of realmin, we wander into the realm of what are called denormalized numbers. And we can go down a little ways until we get to realmin/2^52. Beyond that we just underflow to 0.
Can you search for NaNs, and then replace them with some small number? Of course. But you cannot tell MATLAB to do this automtically with all arrays, as a matter of normal operation.
The display format shows that third element as 0.000, but it is truly just realmin.