MATLAB automatically converts double to int without explicit cast - matlab

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?

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

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.

Related

convert char to double in matlab: str2double

X= 1:63;
n = 6;
% Y = int2bit(X,n)
y=dec2bin(X, n)
with this example I tried str2double(y) and got NaN
What is a problem?
str2double will only convert text that represents real or complex scalar values. Where y is a char array of binary values. It is basically interpreting y as one large integer. Hence, it will return NaN or Inf depending on the version of MATLAB you are using.
You can use convertCharsToStrings and then use str2double
e.g
for i = 1:length(y)
tempvar = convertCharsToStrings(y(i,:));
x1(i) = str2double(tempvar);
end
OR if you just want to convert all string into double then use
arrayfun(#(x)str2double(convertCharsToStrings(x)),y,'Uniformoutput',false)

Symbolic limit calculation, numeric value desired

I am trying to calculate a limit operation of a function inside. Here is what I did:
x = 0;
f = (cos(x)*cos(h/2)*sin(h/2))/(h/2) - (sin(x)*sin(h/2)*sin(h/2))/(h/2);
limit(f,h,0)
ans =
1
limit(f,h,1)
ans =
2*cos(1/2)*sin(1/2)
I want to see what the numeric value of 2*cos(1/2)*sin(1/2) is. How do I obtain this value?
You can use double to evaluate the final expression:
double(limit(f,h,1))
ans =
0.8415
limit is a symbolic function, so it outputs symbolic functions. You can use double (or single or whatever numeric type you want) to convert to a number.

Matlab Double cast from Uint64

I am casting a 64bit fixed point number to floating point. How should that be done in Matlab? The following code gives different results. What is the difference between typecast and the double(x)
temp = 2^32*uint64(MSB) + uint64(LSB);
out_0(1, 1) = typecast(temp, 'double');
out_1(1, 1) = double(temp);
An Example:
temp = 4618350711997530112
data = typecast(temp, 'double')
data =
5.9194
>> double(temp)
ans =
4.6184e+18
If you want to maintain the same number, you should definitely use double to convert it:
double Convert to double precision.
double(X) returns the double precision value for X.
Whereas typecast maintains the internal representation, i.e. the bytes are maintained the same but or differently interpreted:
typecast Convert datatypes without changing underlying data.
Y = typecast(X, DATATYPE) convert X to DATATYPE. If DATATYPE has
fewer bits than the class of X, Y will have more elements than X. If
DATATYPE has more bits than the class of X, Y will have fewer
elements than X.
Note that it is only possible to use typecast when the number of bytes are the same, which is not true for double as it tries to represent the same number as close as possible in double precision. For example, you cannot typecast uint32 to double, but you can typecast two uint32 to one double number. If you use double to convert it, you will obtain respectively one and two doubles.
C++ equivalent
X = double(uint64(123));
=> int64_t x = 123; double X = x;
X = typecast(uint64(123), 'double')
=> int64_t x = 123; double X = reinterpret_cast<double>(x);
In addition, because it seems you have two 32-bit uint values MSB and LSB; To convert them to uint 64 you can use typecast.
U = typecast([MSB,LSB],'uint64')
Then conversion to double as suggested by m7913d
D = double(U)
So you see typecast has a very different function compared to double.

Why is the type of integer "3" a float in MATLAB?

I am new to MATLAB and am confused with types. Why is 3 the float and not integer??
>> isa(3, 'float')
ans =
logical
1
>> isa(3, 'integer')
ans =
logical
0
In the MATLAB documentation on Numeric Types, you can read that
By default, MATLAB stores all numeric values as double-precision floating point.
Therefore, isa(3, 'float') (or isfloat(3) ) returns true.
You can store a number explicitly as an integer:
isinteger(uint8(3))
ans =
logical
1
This example from the MATLAB documentation should also be very helpful.
You can use any of the integer types in MATLAB, which are
int8
int16
int32
int64
uint8
uint16
uint32
uint64

Mod function returns 0 for Matlab

I have a problem with the mod function output in Matlab. I am trying to perform some calculations for ECC double and add algorithm. I am reading data from a file and storing it in a variable and then performing some operations. All works smoothly except that I get 0 in temp1 when I use mod(X2,P). However if I put in values stored in X2(3.0323e+153) and P(1.1579e+77) on command window (mod( 3.0323e+153, 1.1579e+77)), I get the correct values. Can anyone please help me? Below is the part of script which is problematic.
P = hex2dec('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F');
line = fread(fileID,[1,67],'*char');
while ~feof(fileID)
PX = line(4:67);
X = hex2dec(PX);
X2 = X^2;
temp1= mod(X2 , P)
end
line = fread(fileID,[1,69],'*char');
end
fclose(fileID);
I think the problem lies with how you're initializing P. From the documentation for hex2dec (emphasis mine):
d = hex2dec('hex_value') converts hex_value to its floating-point integer representation. The argument hex_value is a hexadecimal integer stored as text. If the value of hex_value is greater than the hexadecimal equivalent of the value returned by flintmax, then hex2dec might not return an exact conversion.
And the value of flintmax is:
>> flintmax
ans =
9.007199254740992e+15
Quite a bit smaller than your value for P. In fact, if we use num2hex to look at the two ways you initialize P, you can see a clear difference:
>> P = hex2dec('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F');
>> num2hex(P)
ans =
4ff0000000000000
>> num2hex(1.1579e+77)
ans =
4fefffda293c30de
As it turns out, the inexact conversion done by hex2dec results in a number that evenly divides into 3.0323e+153, thus giving you a remainder of 0:
>> mod(3.0323e+153, P)
ans =
0
>> mod(3.0323e+153, 1.1579e+77)
ans =
8.795697942083107e+76