[Tutor] problem with time module

Tim Peters tim.peters at gmail.com
Mon Aug 16 19:35:02 CEST 2004


[Dick Moores]
> Please forgive me for persisting here, but mktime(t) is not the inverse
> function of localtime() in the same precise way that pow(n,2) and sqrt
> (n) are inverses of each other (for n >= 0):
>
> >>> from math import sqrt
> >>> n = 78.56
> >>> print pow(sqrt(n),2)
> 78.56
> >>> print sqrt(pow(n,2))
> 78.56
>
> Thus my misunderstanding of the doc.

Au contraire:

>>> from time import mktime as m, localtime as lt
>>> m(lt(0))
0.0
>>> m(lt(1))
1.0
>>> m(lt(2))
2.0
>>> m(lt(123456789))
123456789.0
>>>

They're exact inverses, so long as localtime's argument is an integral
value (has no fractional seconds).  Because of floating-point rounding
errors, they're closer to being exact inverses than sqrt and pow(_,
2)!

>>> from math import sqrt
>>> pow(sqrt(2), 2)
2.0000000000000004
>>>

> ...
> And thanks very much for your assistance, Mr. Peters.

My father died a long time ago -- I'm just Uncle Timmy <wink>.


More information about the Tutor mailing list