Mod function returns 0 for Matlab - 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

Related

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.

Compare symbolic variables

I am trying to compare two symbolic variables (numbers). The whole problem boils down to the following code.
R = vpa(0.555555555555555555555555555);
isAlways(R>R*(1-sym(10^(-10))))
isAlways(R>R*(1-sym(10^(-50))))
Both comparisons should return 1, but the second returns 0.
My solution:
digits(51);
R = vpa(0.555555555555555555555555555);
isAlways(R>R*(1-sym(10^(-10))))
isAlways(R>R*(1-sym(10^(-50))))
Why you encounter this problem
vpa evaluates symbolic inputs with variable-precision floating-point arithmetic (32 significant digits by default)... So what's happening in your case is
>> R = vpa(0.555555555555555555555555555)
R =
0.55555555555555555555555555555556
>> R*(1-sym(10^(-50)))
ans =
0.55555555555555555555555555555556
32 digits are definitely not enough to store the actual value of 1-10^(-50).
How to fix it
Without stressing with vpa() you can declare both R and R * (1 - 10^(-50)) as symbolics (in fact 0.5555555... = 5/9), and compare them:
>> R = str2sym('5/9');
>> X = str2sym('5/9 * (1 - 10^(-50))');
>> isAlways(R > X)
ans =
logical
1

recursive function matlab decimal to binary conversion

I have a function that converts a decimal number to binary but i need to make the function recursive. I don't know how to make this function call itself. Any help will be very appreciated.
This is my function :
function bin = myBinary (dec_nr)
i = 1;
q = floor(dec_nr/2); %this is the quotient, the "floor" function is used to round down
r = rem(dec_nr, 2); % the "rem" function is used to get the remainder which will become the first binary value
bin(i) = num2str(r(i)); % the remainder is converted and stored as a string
while 2 <= q
dec_nr = q;
i = i + 1;
q = floor(dec_nr/2);
r = rem(dec_nr, 2);
bin(i) = num2str(r);
end
bin(i + 1) = num2str(q);
bin = fliplr(bin);
save myBinary
Thank you in advance!
That's pretty simple to do. I would implement this using something called tail recursion. Remember the algorithm for converting a decimal number (assuming unsigned) into binary.
Declare a number you want to convert.
Initially declare an output to be empty.
Until the number is 0...
a. Take the number and divide this by 2
b. If there is a remainder, then attach a bit of 1 from the left, so output <- [1 output]. Else, attach a 0, so output <- [0 output].
output will contain the binary representation of your number.
With recursion, there are two cases you must consider: The base case, where the algorithm stops and gives you a known output, and a recursive case that tells you that you need to keep going, and you call the function again with modified inputs.
As such, you can very easily achieve what you want above by tail recursion by providing a helper function that takes in the input number at a given state (basically the number after being divided by 2 for a number of times) and a binary string that gives you the current state of binary string construction as you're going through and determining each bit.
You would repeatedly call this function and taking the input number and dividing it by 2 until you get a result of 0. You would start by calling the function with the original input number, and an empty string. Make sure that when you send the input number back into the function for the recursive case, you need to truncate any decimal values that result, so you would take the floor.
Therefore, a possible function is very simply:
function bin = bin2dec_recursive(in)
%// Recursive helper
function [out] = recursive_helper(in_number, binstr)
%// Base case
if in_number == 0
out = binstr; %// Just return the current binary string
%// Recursive case
else
%// Recurse - Integer divide input number and concatenate string
out = recursive_helper(floor(in_number/2), [num2str(mod(in_number,2)) binstr]);
end
end
%// call recursive helper
bin = recursive_helper(in, '');
end
What's key is this statement right here:
out = recursive_helper(floor(in_number/2), [num2str(mod(in_number,2)) binstr]);
When we hit the recursive case, we need to call the function again by dividing the input number by 2 and the input binary string is the current string, but we add either a 0 or 1 to the left of the string. We convert the number to a string by num2str.
If you desire to have a double array of single digits, simply remove the num2str call inside the recursive case, and replace '' with [] when you're calling the recursive helper function for the first time at the last line of code above.
With this, here are some examples of it working:
>> bin2dec_recursive(3)
ans =
11
>> bin2dec_recursive(5)
ans =
101
>> bin2dec_recursive(9)
ans =
1001
>> bin2dec_recursive(127)
ans =
1111111
>> bin2dec_recursive(200)
ans =
11001000
Minor Note
Be advised that the recursion limit for MATLAB is 500 calls. Therefore, with recursion, you can only compute binary numbers that are up to a maximum of 500 bits, so this means that the maximum decimal number you can put into the above function is approximately 2500 - 1. Anything more will give you an error saying that MATLAB has reached its recursion limit.

Results not correct when using diff function in Matlab

I'm using the diff Matlab function to get the difference between two successive values. And as shown here in this vector nz in this link as shown in nz the difference between col 261 and 260 is -1342 but when I use this script the result of difference between this coloumns don't appear in the result dnz. So if anyone could advise why this is not working?
This is my attempt:
load('nz.mat');
dnz = diff(nz);
If you type class(nz) you see that your data is unit16. And MATLAB saturates the results when dealing with integer values, i.e. since 0 - 1342 is lower than zero (the smallest value in uint16) it returns zero:
>> dnz=diff(nz);
>> dnz(260)
ans =
0
If you convert it to a class that can accomodate -1342 like int16 you get
>> dnz = diff(int16(nz));
>> dnz(260)
ans =
-1342

Increase Hex2dec or dec2hex output range in Matlab

I have a strange problem with hex2dec function in Matlab.
I realized in 16bytes data, it omits 2 LSB bytes.
hex2dec('123123123123123A');
dec2hex(ans)
Warning: At least one of the input numbers is larger than the largest integer-valued floating-point
number (2^52). Results may be unpredictable.
ans =
1231231231231200
I am using this in Simulink. Therefore I cannot process 16byte data. Simulink interpret this as a 14byte + '00'.
You need to use uint64 to store that value:
A='123123123123123A';
B=bitshift(uint64(hex2dec(A(1:end-8))),32)+uint64(hex2dec(A(end-7:end)))
which returns
B =
1310867527582290490
An alternative way in MATLAB using typecast:
>> A = '123123123123123A';
>> B = typecast(uint32(hex2dec([A(9:end);A(1:8)])), 'uint64')
B =
1310867527582290490
And the reverse in the opposite direction:
>> AA = dec2hex(typecast(B,'uint32'));
>> AA = [AA(2,:) AA(1,:)]
AA =
123123123123123A
The idea is to treat the 64-integer as two 32-bit integers.
That said, Simulink does not support int64 and uint64 types as others have already noted..