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 forhex2dec
(emphasis mine):And the value of
flintmax
is:Quite a bit smaller than your value for
P
. In fact, if we usenum2hex
to look at the two ways you initializeP
, you can see a clear difference:As it turns out, the inexact conversion done by
hex2dec
results in a number that evenly divides into3.0323e+153
, thus giving you a remainder of 0: