When numerical data type changes while assigning new data?

I just came across something concerning data types which I find astonishing.
i1 = int8( 10 ); class( i1 )
i1 = 10.1; class( i1 )
i1 is int8 after the first line, but changes to double in the 2nd line
im1 = int8( [1 2 3]); class(im1)
im1(1) = 1.1; class(im1)
im1 is int8 after 1st line and remains int8 in the 2nd line, i.e. the floating point value is casted to int8
Why is the behaviour different depending on the matrix size? Is this explained somewhere in the documentation?
I use R2015b.

 Respuesta aceptada

Stephen23
Stephen23 el 26 de Sept. de 2017
Editada: Stephen23 el 26 de Sept. de 2017
"Why is the behaviour different depending on the matrix size?"
It doesn't, and there is nothing astonishing happening at all. You are trying to compare allocating a variable with indexing, which are two totally different operations. Lets have a look at them...
Allocation
Your first example totally reallocates the variable, i.e. replaces the old variable with a new one that is totally independent of the previous one (which is permitted because MATLAB is a weakly-typed language):
>> X = 'hello' % allocate a char to a variable named X
X = hello
>> class(X)
ans = char
>> X = 2 % allocate a double to a variable named X
X = 2
>> class(X)
ans = double
The fact that X already existed is totally irrelevant: X = 2; replaces any already-existing variable entirely, and even uses a new memory location (the old memory location is thus freed up).
"i1 is int8 after the first line, but changes to double in the 2nd line"
No, the variable stored in memory does not "change" type: you created a new variable, and discarded the old one. The two variables are totally unrelated to each other in memory, only by chance you happened to have given them the same name.
Indexing
In contrast indexing takes an existing array and puts some value/s into that array (or gets them out):
>> X(1) = 3
X = 3
>> class(X)
ans = double
>> X(1) = uint8(4)
X = 4
>> class(X)
ans = double
Indexing does not create a new variable in memory (unless you are changing the size of the array, in which case the whole array is copied to a new location): indexing simply allocates value/s to the location/s that you specified. MATLAB does an implicit type conversion if required, to keep the value/s the same (limited by the range of the destination data class, of course).

3 comentarios

The only thing that can be confusing about matlab type conversion is that matlab has implicit narrowing conversions. That is if you combine two different types the result will be of the smallest type. E.g. add a double and an int8, you'll get an int8 as a result.
Most other languages have implicit widening conversions to avoid data loss. Add a double and an int8 an you get a double as all int8 can be safely converted to double whereas most double cannot be converted to int8 with no loss of precision.
Stephen23
Stephen23 el 26 de Sept. de 2017
Editada: Stephen23 el 26 de Sept. de 2017
The narrowing is confusing for some situations, but then again double does not cover all uint64 or int64 values without loss, so it is not clear what data type should be used in those cases... I work with large integers reasonably regularly, so it for me it is certainly not desirable that all integers should map to doubles.
In any case, implicit type conversions:
  • when indexing, convert to the destination class.
  • when concatenating, refer to these explanations:
convert to the left-most class (when concatenating)
Not at all!. The result is the narrowest of the class being concatenated, wherever it occurs in the concatenation
>> [0.1, uint8(4)]
ans =
1×2 uint8 row vector
0 4
>> [uint8(4), 0.1]
ans =
1×2 uint8 row vector
4 0
There are a lot more double numbers that can't be represented as (u)int64 (all non integers) than there are (u)int64 than can't be represented as numbers (anything above flintmax).
I say it is confusing because matlab is the only language I know that narrows implicitly. All others (Java, C/C++/C# and co., python, ruby) do the opposite. Coming from these, it's something you have to watch out for.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 26 de Sept. de 2017

Editada:

el 26 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by