The integer data sructures such as int8 or int16 has NaN values?

 Réponse acceptée

No. Integer variables such as int8 or int16 do not allow NaN as an element. A simple test answers the question.
u = int8([0,10,NaN])
u =
0 10 0
u = int16([0,10,NaN])
u =
0 10 0
If you think about how the variables are stored in Binary form, NaN (or inf) would be impossible to encode in that form, without limiting the dynamic range of the variable.

4 commentaires

inf is not an integer, but unknown can be interpreted as integer and could be the pair of zero, since zero has no pair in the representation
But your interpretation is irrelevant. All that matters is what exists. Were NaN to be an option for a int8 number, then how should that information be stored internally in binary form?
The point is, an int8 number encodes a finite number of values, the integers from -128 to 127. That requires fully 8 bits of information. Were you to try to squeeze an additional piece of information into that variable, then you would lose some dynamic range for the integer, or you would be forced to use at least 9 bits. (You would then possibly want to call it int9.)
This is purely a system design issue of course. Feel free to write your own implementation to store an integer. Of course then you need to write all the operators like plus, minus, times,etc. But if you will use int8 or int16, you need to accept the decisions made by what was (I assume) an IEEE committee.
NaN is just a name, -127 could be interpreted as NaN and could be more useful in lots of situations ...
Something else: NaN*int8(number) = 0, which is very strange! This is abnormal! A programming language can do type conversion or can throw an error in such situations, but this kind of functioning is abnormal, I think
Operations involving the integer data types are defined to work "as if" the values were converted to double, the operation carried out in double, and then the result were converted to the more restrictive data type. You are using a mix of double and int8, so the operation becomes like int8( NaN * double(int8(number)) ) which is like int8( NaN ) and that happens to be 0.

Connectez-vous pour commenter.

Plus de réponses (1)

you can use cell array for that
a=[1 2 nan 3]
idx=isnan(a);
b=num2cell(uint8(a))
b(idx)={nan}
Now you can check the class of each element
s=b{1}
whos s
g=b{2}
whos g

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by