0

I've been playing around with the time.time() function, which prints the time elapsed since . When printing its result, I've tried multiplying it with a number on the power of 10. I've expected that I will eventually see a number with zeroes at the end (e.g. 1516895254.85 * 10000 = 15168952548500). Instead, it went as far as printing a 309-digit number, with no zeroes at the end, when reaching the function's precision.

>>> print('{0}'.format(int(time() * (10**299))))

151688275429037515979926334429405401106271490533817517185371446220795471128402317934144559501851174348970520458946580306822182798812776130098014987340202895198224161052094530708120342982016788576011755725325408112135784020427445752709662939740525586044947507255593760381150928852505533344315172483047700299776

At 310, it didn't start printing zeroes, but gave me the error message:

OverflowError: long int too large to convert to float

It seems impossible that python's time measurement would be THAT precise. Does anyone know what is happening? Are these random digits? Or am I misunderstanding something here?

Edit: Thank you for your answers, sorry for asking a duplicate question.

lehoo
  • 21
  • 3
  • Use `from decimal import Decimal`. – goodvibration Jan 25 '18 at 15:53
  • This probably varies by OS. I'm on Windows and multiplying by 10e6 seems to be sufficient to get a whole number (or something that looks like it; you never can tell with floats :-P). – Kevin Jan 25 '18 at 15:53
  • Computers don't use base 10 – John Coleman Jan 25 '18 at 15:53
  • It’s a float. So it’s already imprecise to begin with. You probably shouldn’t do anything with more than 3-6 of its decimal places. – poke Jan 25 '18 at 15:53
  • 1
    Compare your output with the output of `int(0.1 * (10 ** 299))`. – poke Jan 25 '18 at 15:55
  • 1
    What John Coleman said. BTW, on modern versions of Python you should use [`time.perf_counter`](https://docs.python.org/3/library/time.html#time.perf_counter) for high resolution timing info. – PM 2Ring Jan 25 '18 at 15:56
  • You should probably take a look at https://stackoverflow.com/questions/588004/is-floating-point-math-broken and the Wikipedia articles on floating point and IEEE-754, eg https://en.wikipedia.org/wiki/Double-precision_floating-point_format – PM 2Ring Jan 25 '18 at 15:59
  • FYI: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Robᵩ Jan 25 '18 at 16:53
  • @lehoo Welcome to [so]! It's better to leave comments as comments rather than at the bottom of the answer. – jpaugh Jan 25 '18 at 18:34

0 Answers0