# average calculation with hundredths of seconds?

Page 1 of 1

## 3 Replies - 1068 Views - Last Post: 10 January 2013 - 09:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=306599&amp;s=ed165ad58fc3bd0aa05e10e85b3a7134&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pmec

Reputation: 0
• Posts: 1
• Joined: 10-January 13

# average calculation with hundredths of seconds?

Posted 10 January 2013 - 03:54 PM

Hi guys i'm trying to get the average of two times. In other words I have two times in minutes:seconds:hundredths_of_seconds and i'm trying to get the average of these times but i just cant seem to get the hundredths of seconds right.

for example if i have 00:02:20 and 00:04:40 (minutes:seconds:hundredths) it should return 00:03:30
i was able to get the minutes and the seconds just fine but i am going desperately crazy in trying to get the hundredths of seconds right.

here is the code:

```def lap_average(lap1, lap2):

mins1, secs1, hundred_sec1 = lap1.split(":")
mins2, secs2, hundred_sec2 = lap2.split(":")

minutes = int(mins1) + int(mins2)
seconds = float(secs1) + float(secs2)
h_seconds = float(hundred_sec1) + float(hundred_sec2)
hundredths = int(60000 * minutes + 1000 * seconds + 0.001 * h_seconds)
hundredths = hundredths // 2

secs, hundredths = divmod(hundredths, 1000)
minutes, secs = divmod(secs, 60)
hundredths = ((hundredths/1000)*1000)

print minutes, secs, hundredths

# testing times
lap_average('03:40:00', '05:20:00')
lap_average('03:00:02', '02:00:00')
lap_average('02:25:50', '06:50:75')
lap_average('00:02:00', '00:03:00')
lap_average('00:02:20', '00:04:40')
lap_average('02:40:40', '03:30:30')
lap_average('02:60:30', '60:40:40')
```

Any help in achieving this would be much appreciated.

P.S - please be nice i'm a newbie

Is This A Good Question/Topic? 0

## Replies To: average calculation with hundredths of seconds?

### #2 darek9576

• D.I.C Lover

Reputation: 203
• Posts: 1,731
• Joined: 13-March 10

## Re: average calculation with hundredths of seconds?

Posted 10 January 2013 - 04:26 PM

I dont understand your algorithm. Why dont you just split the data, as you are doing and then add corresponding data and divide by 2.

```minute = (minuteOne + minuteTwo) / 2
etc

```

### #3 sepp2k

• D.I.C Lover

Reputation: 2627
• Posts: 4,182
• Joined: 21-June 11

## Re: average calculation with hundredths of seconds?

Posted 10 January 2013 - 05:43 PM

pmec, on 10 January 2013 - 11:54 PM, said:

```hundredths = int(60000 * minutes + 1000 * seconds + 0.001 * h_seconds)

```

Why are you multiplying seconds by thousand and the hundredths by one thousandth? Shouldn't you 1. be using hundred/one hundredth and 2. only doing one or the other?

That is either you're storing the number in hundredths, then seconds need to be multiplied by one hundred (and minutes by six thousand) and h_seconds doesn't need to be multiplied at all. Or you're storing it in seconds, then seconds doesn't need to be multiplied (and minutes only by 60).

Personally I'd choose to store it in hundredths (like your variable name already suggests anyway). Then you don't need any floats in your code.

Quote

hundredths = ((hundredths/1000)*1000)

That's a noop. If you want to do some kind of rounding, you should be using // instead of /. But I don't really see why you'd want to round there.

darek9576, on 11 January 2013 - 12:26 AM, said:

I dont understand your algorithm. Why dont you just split the data, as you are doing and then add corresponding data and divide by 2.

```minute = (minuteOne + minuteTwo) / 2
etc

```

Because minute might end up as something like 12.5 that way.

This post has been edited by sepp2k: 10 January 2013 - 05:46 PM

### #4 woooee

Reputation: 46
• Posts: 171
• Joined: 21-November 12

## Re: average calculation with hundredths of seconds?

Posted 10 January 2013 - 09:36 PM

FYI duplicate of
http://www.daniweb.c...ime-calculation
and http://forums.devshe...ond-937799.html