- int asc_desc_time = dive->maxdepth.mm*60/9000;
- dive->meandepth.mm = dive->maxdepth.mm*(dive->duration.seconds-asc_desc_time)/dive->duration.seconds;
+ int depth = dive->maxdepth.mm;
+ int asc_desc_time = depth*60/9000;
+ int duration = dive->duration.seconds;
+
+ /* Some sanity checks against insane dives */
+ if (duration < 2)
+ duration = 2;
+ if (asc_desc_time * 2 >= duration)
+ asc_desc_time = duration/2;
+
+ dive->meandepth.mm = depth*(duration-asc_desc_time)/duration;