skelzer / electricsleep

Automatically exported from code.google.com/p/electricsleep
Other
0 stars 0 forks source link

wrong sleep average #143

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What version of ElectricSleep are you using?
Beta 0.7.0
On what android device and android version?
Droid 

What steps will reproduce the problem?
1. Not sure..run program for extended period of time.
2.
3.

What is the expected output? What do you see instead? My average for duration 
of sleep is not accurate. I rarely sleep over 7 hrs and it shows my duration at 
16+ hours. It is not possible and should be corrected.

Please provide any additional information below.

Original issue reported on code.google.com by criscoa...@gmail.com on 24 May 2011 at 3:55

GoogleCodeExporter commented 8 years ago
About this issue and issue 145.

I have the same problem with version 0.7
I took a look at the source code and found the bug.

In the home activity (HomeActivity.java), you compute the total sleep time (l. 
92-99) and then you take the average by dividing by the number of sleep (l. 
102).
This is fine as long as there is no overflow.
Here it is the case. You sum up the sleep time (in ms) in an int, l. 88 :
int avgDuration = 0;

As the maximum value for an int is 2^31-1 (2147483647), if you sleep an average 
of 8 hours (28800000ms), it gives you a correct average for the first 
2147483647/28800000 ~= 74 nights. After that the value is bogus.

A quick simple fix is to declare avgDuration as a long in line 88 :
long avgDuration = 0;

It is even better as SleepRecord.getTimespanText() takes a long as first 
parameter so you will avoid a cast. You might consider using a long for 
avgFellAsleep as well.

With a long, assuming an average of 8 hours per night, one could sleep 
(2^63-1)/28800000 ~= 320 255 973 501 nights, so the average will be accurate 
during more than 800 000 000 years!

Original comment by nicolabr...@gmail.com on 30 Jun 2011 at 3:06

GoogleCodeExporter commented 8 years ago
Ive dropped development on this project for some time. If you supply the fix, I 
might update the market app (despite catching ire from quite a few people, I am 
sure)

Original comment by jondwil...@gmail.com on 30 Jun 2011 at 3:21

GoogleCodeExporter commented 8 years ago
Well here it is.

I also changed the way you compute the average. Since a division is very 
time-consuming, I take the inverse of count only once and then multiply each 
stat by it.

Original comment by nicolabr...@gmail.com on 1 Jul 2011 at 7:25

Attachments:

GoogleCodeExporter commented 8 years ago
Hey Jon,

Change the int to an long for all ms records should fix the problem.

Best,
Edison

Original comment by wzsddtc on 12 Jul 2011 at 8:38

GoogleCodeExporter commented 8 years ago
nicola, i applied your fix in r132. thank you.

Original comment by jondwil...@gmail.com on 14 Aug 2011 at 5:00