3 Replies - 479 Views - Last Post: 10 January 2013 - 09:36 PM Rate Topic: -----

#1 pmec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • 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


Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: average calculation with hundredths of seconds?

Posted 10 January 2013 - 05:43 PM

View Postpmec, 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.


View Postdarek9576, 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

Was This Post Helpful? 1
  • +
  • -

#4 woooee  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 78
  • 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
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1