Cancel
Showing results for 
Search instead for 
Did you mean: 

Time comparison problem

Creator
Creator

Dear all,

In my main model I am doing a methcall which will be called at time X, a variable keeps track of the latest X whenever this methcall is generated. When this method is then called because of the event, it compares the Eventcontroller's simulation time to the variable which contains the latest X. The method is not further executed (using return) when both times are not equal. This was done in order to only execute the latest methcall which was made and ignore the older ones.

 

When comparing the current time and the time from the variable, a weird situation occurs. I emulated the "glitch" in a simplistic test model which you can download below. Although in the test model no methods should be ignored, the "glitch" is the same. If you run the model you will encounter a debug stop such as the screenshot. savedTime (contains the latest X) is compared to currentTime, both are and should be equal if you look in the expressions frame on the bottom of the screenshot, but according to the result of the if-case window it sais that both times are not equal!

Why is this if-case not false?

 

Kind regards,

Seppe

comparetimes_debug.JPG

 

 

 

 

3 REPLIES

Re: Time comparison problem

Siemens Phenom Siemens Phenom
Siemens Phenom

Hi!

 

I don't know the reason of such behaviour, maybe a rounding error or whatever. Time is the same as real data type and it's always dangerous to check whether real values are equal.

 

What you can do is using "about equal" operator this way:

if NOT (savedTime ~= currentTime)
	debug;
else
	savedTime := totalTime
	&testMethod.methCall(timeInterval);
end
Regards, Vladimir.

Re: Time comparison problem

Creator
Creator

Ah indeed that must be it!

This is a good solution for the problem,thank you!

 

Kind regards,

Seppe

Re: Time comparison problem

Siemens Phenom Siemens Phenom
Siemens Phenom

It is indeed a rounding problem:

Debugger.png

 

It is always a good idea to compare floating point values using ~= instead of =

_________________________________________________________________
Did you like the answer? Then click the Thumbs Up button.
Did the answer solve your problem? Then accept the answer as solution.