Hacker News new | ask | show | jobs
by Tainnor 1816 days ago

  sum = 0
  for i in range(0, 10000000):
    sum += 0.1
  print(round(sum*1000, 2))
what should this code print? what does it print?

I mean, sure, this is a contrived example. But can you guarantee that your code doesn't do anything similarly bad? Maybe the chance is tiny, but still: wouldn't you like to know for sure?

1 comments

We agree, on additions, floats are tricky. But still, on division, multiplications, they're not any worse. Dividing something by 3 will end up in an infinite number of decimals that you'll have to round at some point (except if we use what you proposed : fractions; in that case that's a completely different story).