[sf-lug] [PYCLASS] Still on exercise 2.1

Charles-Henri Gros chgros at coverity.com
Sat Oct 4 07:40:24 PDT 2008


> /* Set some constants */
> MIN = 60
> HOUR = MIN*60
> easy_pace = 8*MIN+15
> tempo_pace = 7*MIN+12
> start_time = 6*HOUR+52*MIN
> 
> secs = easy_pace * 1 + tempo_pace * 3 + easy_pace * 1
> print "%d:%02d" % ( (secs/ MIN), (secs%MIN) )
> 
> /* Now add start time plus run time and convert to hours, minutes and seconds */
> total = start_time + secs
> print "%d:%02d:%02d" % ( (total/HOUR), (total%HOUR/MIN), (total%HOUR%MIN) )
> 
> The reason why this can be considered "more correct" is that time is
> always represented in seconds in UNIX systems (eg, the well known UNIX
> epoch).

Another reason is that floating point arithmetic is often approximate
(e.g. 1/60 hour doesn't have a finite decimal representation), while
integer is exact as long as it doesn't overflow (overflow in seconds is
about 60 years for 32-bit signed integer) and you don't divide (which
you don't here)

-- 
Charles-Henri




More information about the sf-lug mailing list