Negative Millisecs()
BlitzMax Forums/BlitzMax Programming/Negative Millisecs()
| ||
So, what's the best way to get around negative Millisecs() ?DebugLog:-2030341154 DebugLog:-2030341121 DebugLog:-2030341081 DebugLog:-2030341059 DebugLog:-2030341027 Convert it to a Long ? ... $FFFFFFFF:Long & MilliSecs() ... That's a bugger... Would've been nice if it returned a Long in the first place... :-p |
| ||
I always mask off the sign bit (& $7FFFFFFF). That way it just wraps. |
| ||
calc sub = value - max negative int add sub + max int, both haneld as long Jezzie: your solution is nice but kind of worthless. Will enforce ABS on all time measures as it will go up and then down again, not really what you expect a time counter to do ;-) |
| ||
Converting to Long seems reasonable, although it only doubles the time until trouble occurs. Do you really need that many milliseconds? It is generally used for small quantities of time. An expression like ( NewTime - OldTime ) is not affected by the "wrap to negative" problem. |
| ||
The bitmask trick is a nice one, and saves the need for Longs at all. |
| ||
I was only using it as a trigger, so the actual value of MilliSecs() isn't important at all. Yeah, the new-old might even be preferable... Ta :-) |
| ||
Yupp new - old is the simplest way to solve it as it solves the negative millisecs issue on its own. At least beside 2 cases: 1. When the millisecs goes over the maximum number and is wrapped to - -> a time difference of 2 billion millisecs 2. When it goes from - to + where it will add those two values instead of substracting one. That thought is not that of a problem unless you have large time differences you measure. |
| ||
If you just do deltatime = newtime-oldtime, then it will still work when it is negative. And to handle the case where it wraps around, which I don't know will work with the above, I just have a check to see if deltatime is too great. If it's like more than 250ms, then I ignore that and recalculate the time. This solution works well for when the user tabs out of the app too. |