Avoiding the possible data loss error
int secs_remaining=(Time[0]+PeriodSeconds())-TimeCurrent();
// Either in the "function" style
int secs_remaining = int( Time[0] - TimeCurrent() ) + PeriodSeconds();
// or in the "C" style
int secs_remaining = (int)( Time[0] - TimeCurrent() ) + PeriodSeconds();
What is the best way to avoid the "possible loss of data..." error from this line?
int secs_remaining=(Time[0]+PeriodSeconds())-TimeCurrent();
Typecasting!
// Either in the "function" style
int secs_remaining = ( int( Time[0] ) + PeriodSeconds() ) - int( TimeCurrent() );
// or in the "C" style
int secs_remaining = ( (int) Time[0] + PeriodSeconds() ) - (int) TimeCurrent();
EDIT: I have to wonder however, if one can have a negative datetime variable, namely is "Time[0] - TimeCurrent()" valid?
EDIT2: Maybe my original post is more correct than my updated "efficient" version:
int secs_remaining = ( int( Time[0] ) + PeriodSeconds() ) - int( TimeCurrent() );
// or in the "C" style
int secs_remaining = ( (int) Time[0] + PeriodSeconds() ) - (int) TimeCurrent();
EDIT: I have to wonder however, if one can have a negative datetime variable, namely is "Time[0] - TimeCurrent()" valid?
Yes, a datetime variable can store a negative number (to all intents and purposes, datetime is a long) but if you try to print a negative datetime it will show "wrong datetime"
EDIT: Well, maybe not, because in this case it is just a time difference which perfectly fits an "int".
Yes, but converting a negative "datetime" into an "int" my cause a loss. I've never actually tested that myself!
In theory, yes. Both ways, positive or negative. Around 68 years / Year 2038 will max out the int.
I just take the hit on the 4 bytes of extra memory and store a long.
Hopefully, this "extra" chatter will not confuse the OP!
Meh, posts 2 and 3 will set them on the right path so they don't have to read very far.
The rest is relevant discussion for those who are interested (probably just you and me!)
| This is why I typedef it and make the code self-documenting | #define SECONDS uint #define HR2400 86400 SECONDS time(datetime when=0){ return SECONDS(when == 0 ? TimeCurrent() : when) % HR2400; } datetime date(datetime when=0){ return datetime( (when == 0 ? TimeCurrent() : when) - time(when) ); } |
What is the best way to avoid the "possible loss of data..." error from this line?
int secs_remaining=(Time[0]+PeriodSeconds())-TimeCurrent();