error occurs when an integer is multiplied by a float

7 views (last 30 days)
when I am multiplying a relatively large integer variable (called time_counter ) by a relatively small float variable ( dt ), an error of ( time_counter*dt ) may come out by giving me a wrong result ,which is 9223372036854775807, i.e. the largest value for int64. However, the correct result should be between 2000 and 3000. In the following is the exact code:
% function []= shrinking_obstacle_FHN_PolarCoordinate_high(rh)
rh=20; epsilon=1.1500; delta=-1.01;
isplot=false;
% addpath('/public/home/gaoxiang/scripts/matlab');
%%parameter setting
% medium excitability
R1= rh; N1= ceil(R1/(0.1)); drho=R1/N1;
N2= N1+ceil(40/drho); N= N2-N1; R2= N2*drho;
length_theta=2*pi; M= ceil(2*pi*(R1+R2)*0.5/drho); dtheta= 2*pi/M;
dt= 0.02*0.05;
% parameter in poloar coordinates
alpha0=ones(N,M); alpha1= ones(N,M); alpha2= ones(N,M);
for i= 1:N
alpha0(i,:)= ((i-1+N1)*dtheta)^(-2);
alpha1(i,:)= 1-0.5/(i-1+N1);
alpha2(i,:)= 1+0.5/(i-1+N1);
end
% parameters for time iteration
% use class "FHN_Karma" to initiates variables and parameters
SO= FHN_Karma(N,M,1);
SO.delta_Karma= delta; SO.gamma_Karma= 0; SO.epsilon_Karma= epsilon; SO.diffusion_Karma= 1;
% rest everwhere first
[u_rest, v_rest, u_excited, v_refractory]= SO.rest_state;
% pre-allocate variables
MyselectGPU; SO.copy_to_gpu; alpha0=gpuArray(alpha0); alpha1=gpuArray(alpha1); alpha2=gpuArray(alpha2);
%%initiate excitation
% excite a counter-clockwise spiral
wave_width=round(10/rh/dtheta);
SO.u_Karma(:,1:wave_width)=u_excited;
SO.v_Karma(:,1:wave_width)=repmat(fliplr(linspace(v_rest,v_refractory*0.3,wave_width)),N,1);
SO.v_Karma(:,wave_width+1:M)=repmat(linspace(v_rest,v_refractory*0.3,M-wave_width),N,1);
% parameters
u_front=(u_rest+u_excited)*0.5;
%%dir to store figures
if (isplot)
% figure directory
root_dir=pwd;
fig_dir= strcat(root_dir,'/',strcat('delta',num2str(delta,'%4.2f'),strcat('eps',num2str(epsilon,'%6.4f'))));
if (~isdir(fig_dir))
mkdir(fig_dir);
end
[theta,rho] = meshgrid(2*pi*(0:1/M:1),(N1:1:(N2-1))*drho); [X,Y] = pol2cart(theta,rho);
end
%%gradually shrink obstacle from rh
% initiate parameter
repeat_num= 3+1; T_index= 0; probe= fix(M*3/8); T_probe_excited_time= int64(0); T_repeat= zeros(1,repeat_num); T=NaN;
% time iteration
for time_counter=0:1:round((log(5/20)/log(0.97))*4e3/dt)
% no-flux B.C. at both radial boundaries
u_i_plus_1= [SO.u_Karma(2:end,:);SO.u_Karma(end,:)]; u_i_minus_1= [SO.u_Karma(1,:);SO.u_Karma(1:end-1,:)];
% periodic B.C. at the azimuth boundaries
u_j_plus_1= [SO.u_Karma(:,2:end),SO.u_Karma(:,1)]; u_j_minus_1= [SO.u_Karma(:,end),SO.u_Karma(:,1:end-1)];
% reaction-diffusion equations
SO.u_new_Karma= SO.u_Karma+dt*(alpha0.*(u_j_plus_1+u_j_minus_1)+alpha1.*u_i_minus_1+alpha2.*u_i_plus_1-2*(1+alpha0).*SO.u_Karma)/(drho*drho)+dt*SO.func_f;
SO.v_Karma= SO.v_Karma+dt*SO.func_g;
% plot and measure phase singularity
if (isplot && ~mod(time_counter,round(10/dt)))
% plot
mesh(X(:,:),Y(:,:),horzcat(SO.u_Karma(:,end),SO.u_Karma));
shading interp; colormap(jet); axis([-R2,R2,-R2,R2], 'square');
grid off; box on;
title(strcat('rh=',num2str(R1,'%5.3f'),' at t=',num2str(time_counter*dt,'%i')),'FontSize',20);
% print figures
print ('-djpeg',strcat(fig_dir,'\','rh',num2str(R1,'%5.3f'),'t',num2str(round(time_counter*dt),'%i'),'.jpeg'));
if (time_counter*dt>1000)
u_path=gather(SO.u_new_Karma(1,:));v_path=gather(SO.v_Karma(1,:));
PS= phase_singularity_given_path(u_path,v_path,u_front,0.5*(max(max(SO.v_Karma))+min(min(SO.v_Karma))));
if ( ~mod(PS,2) && max(max(SO.u_new_Karma))<u_front )
break;
end
end
end
% measure Tp and shrink obstacle
if ((SO.u_Karma(1,probe)<=u_front) && (SO.u_new_Karma(1,probe)>u_front))
T_index= T_index+1;
T_repeat(T_index)= (time_counter-T_probe_excited_time)*dt;
T_probe_excited_time= time_counter;
if (T_index>=repeat_num)
% plot
if (isplot)
mesh(X(:,:),Y(:,:),horzcat(SO.u_Karma(:,end),SO.u_Karma));
shading interp; colormap(jet); axis([-R2,R2,-R2,R2], 'square');
grid off; box on;
title(strcat('rh=',num2str(R1,'%5.3f'),' at t=',num2str(time_counter*dt,'%i')),'FontSize',20);
% save the plot
savefig(strcat(fig_dir,'\','rh',num2str(R1,'%5.3f'),'t',num2str(round(time_counter*dt),'%i'),'.fig'));
end
% measure T and cn
T(end+1)= sum(T_repeat(3:repeat_num))/(repeat_num-3+1);
% shrink obstacle
% decrease dx and dt by decreasing factor, therefore, shrinking obstacle
% while dtheta remains, since dtheta is small enough from the beginning
if (R1<=7)
grid_factor=0.99;
repeat_num=3+6;
else
grid_factor=0.97;
end
drho=drho*grid_factor; R1=N1*drho; R2=N2*drho;
dt= dt*grid_factor^2;
% log R1 on rh
rh(end+1)=R1;
display(R1);
% reset counters for T
T_index= 0;
% regenerate meshgrids for plotting
[theta,rho] = meshgrid(2*pi*(0:1/M:1),(N1:1:(N2-1))*drho); [X,Y] = pol2cart(theta,rho);
end
end
% next round
SO.u_Karma= SO.u_new_Karma;
end
SO.gather_from_gpu;
T=T(2:end); rh=rh(1:end-1);
omega= 2*pi./T;
cn=omega.*rh;
save data.mat;
What is the possible reason for this? And how can I correct it? Thanks in advance!
  5 Comments
Walter Roberson
Walter Roberson on 30 Sep 2015
I do not find source for FHN_Karma anywhere.
Is the multiplication happening on the GPU?
Please give us an example of a pair of numeric values that lead to the difficulty, showing the class() of each one.
Xiang Gao
Xiang Gao on 30 Sep 2015
Thanks for your time!
The object FHN_Karma is just to clarify some unrelated variables.
And yes, some computing is on the GPU. dt is involved into some computing on the GPU. But the multiplication of dt and time_counter are not involved.
Sorry for not saving any data files about errors. But I will try to repeat the computing to give you an example of the pair of the numeric values and their class. However, this might take some time.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!