If statement check not working correctly (possible floating point error.)

2 views (last 30 days)
I am writing a function file, that at one point includes an if statement. (if curious, it is a monte-carlo simulation for damage per second with an ability in a game)
Background: (long introduction, im sorry)
The algorithm adds a column to an array, [0;t] where t is the time during the simulation (this is conditional based on some parameter 'as'). In a later section, it checks all elements in the second row of this array for if the time t is equal to the value in the second row in the jth column, and if it is: either adds 0.25 to the value of time and 1 to the corresponding value in the first row, or removes that row (determined through a random number). The time is then stepped up by a value dt, chosen so that 0.25 and as are divisible by dt.
Section from the code: (complete code to reproduce available if needed)
if mod(t,as)==0;
HAarr=[HAarr [0;t]];
end
[m1,m2]=size(HAarr);
for j=1:m2
arrchk=HAarr(2,j)
if arrchk==t
check=rand;
if HAarr(1,j)<burizacount
%there is also a deterministic way for the [1;0.25] to be added which is this check
HAarr(:,j)=HAarr(:,j)+[1;0.25];
elseif check>0.35+cirribon
HAarr(:,j)=HAarr(:,j)+[1;0.25];
else
removevec=[removevec j];
end
end
end
HAarr(:,removevec)=[];
t=t+dt;
Part of a while loop, I have cut out irrelevant sections to keep it to just the parts related to the if statement problem
Every 'as' seconds a new entity is created and stored in an array HAarr, which records number of total hits (starting at 0), and time it will hit (immediately, hence at time t). The 'hitting' part is the for loop section, hence why 0 hits, with an immediate hit.
Then if the time is equal to the time it will hit, it will either go on to hit again in 0.25 seconds (due to random chance or some deterministic manner), or decay. For loop used as there may be multiple entities stored in the array.
The problem:
When I set the value of the discretistion step dt to certain values, the check for if the (2,j)th element of the array is equal to the current time, EVEN if at format long they appear identical, and through the use of the debugging systems in the editor were shown to be identical. Additionally they could not possibly be different, as dt divides into 0.25 and into 'as', and since an entity is created at 0, as, 2as, etc, and can then be updated by multiples of 0.25, it's clear that the second row of HAarr can only contain values in the form n*as+m*0.25, which is divisible by dt. Furthermore the value of time starts at 0 and is increased by steps of dt, thus t is always divisble by dt. Yet the check for if 0.5=0.5 fails every time for any dt I have tested, other than 0.25 (I have tested 0.05, 0.025, 0.01). What makes it additionally confusing is the check for 0.25=0.25 does work, and it only fails for 0.5. Even more confusing is that it does not fail for ALL t>0.25, as for certain larger values of t the check works appropriately.
This suggests to me that the issue lies in some form of floating point number error or rounding error, which seems strange as the highest number of significant figures any number reaches is below 10 in the function file. If this is the case (or if it is not, and you know what the issue is), what steps can be taken to fix this problem. I have spent the past few hours in debugging trying to figure out how two variables are (and MUST be) equal, but the if statement fails to properly and consistently get the check right.
Thank you for reading.

Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!