 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Marcus Guest
|
Posted: Thu Sep 14, 2006 9:10 am Post subject: Rounding time algo |
|
|
Hi, was wondering if anyone knows a good algo for rounding time to a
desired interval.
For instance, I have data set that is timestamped in second resolution
and I need to modify that data to be timestamped for an x minute
interval. So if time t = 93601, and x = 5 then t would be rounded up
to 94000... if x were 1, t would equal 93700, etc. All time is integer
based.
Any help would be really appreciated.
Marcus |
|
| Back to top |
|
 |
Mark P Guest
|
Posted: Thu Sep 14, 2006 9:10 am Post subject: Re: Rounding time algo |
|
|
Marcus wrote:
| Quote: | Hi, was wondering if anyone knows a good algo for rounding time to a
desired interval.
For instance, I have data set that is timestamped in second resolution
and I need to modify that data to be timestamped for an x minute
interval. So if time t = 93601, and x = 5 then t would be rounded up
to 94000... if x were 1, t would equal 93700, etc. All time is integer
based.
Any help would be really appreciated.
Marcus
|
I don't understand your math. If x = 5 and you want to round to 5
minute intervals, then a value t in seconds should be a multiple of 5 *
60 = 300 seconds. But 94000 is not divisible by 300. It looks like
you're assuming 100 seconds per minute based on your rounded values.
In any event, we can phrase this as a more general question: how do you
round a value n to a multiple of m? Since it looks like you want to
round up, you can do the following:
int remainder = n % m;
int rounded_n = n + (remainder ? (m - remainder) : 0); |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|