abhinavkulkarni
abhinavkulkarni

Reputation: 2409

MATLAB automatically converts double to int without explicit cast

I would let the output speak for itself:

>> numFiles, meanTangle, sdTangle

numFiles =

         526


meanTangle =

    0.4405


sdTangle =

    0.1285

Now, when I create a vector out of these variables:

>> [numFiles meanTangle sdTangle]

ans =

         526           0           0

Also, just for clarification:

>> class(numFiles)

ans =

int32

>> class(meanTangle)

ans =

double

>> class(sdTangle)

ans =

double

Why does MATLAB convert floats (meanTangle and sdTangle) to int without cast?

Upvotes: 4

Views: 10488

Answers (2)

MrHappyAsthma
MrHappyAsthma

Reputation: 6522

It converts all of your doubles to ints because your array contains a single int. This has to do with a precision issue.

It converts the entire array into type int32:

>> class(ans)

ans =

int32

Upvotes: 5

NIlesh Sharma
NIlesh Sharma

Reputation: 5655

For reasons not explained, combining an integer data type in an array with floating point data is defined by MATLAB to return an integer data type.

Check this for more info Float becomes integer .
Your numFiles is an integer here so It converts all other variables also as integer.

Upvotes: 4

Related Questions