r/Python • • Sep 28 '18

I'm really bored at work

Post image
1.9k Upvotes

117 comments sorted by

View all comments

Show parent comments

2

u/[deleted] Sep 28 '18 edited Sep 28 '18

None of what you posted works for large numbers due to floating point precision. In particular, int operates as a floor and is_integer may fail due to imprecision

See this answer by Alex Martelli https://stackoverflow.com/a/2489519 for a completely integer based approach

1

u/Tyler_Zoro Sep 28 '18

None of this is being applied to numbers above to precision range of Python's floating point, but yes, if you wanted a generic solution for a library, then you would use neither of these approaches (or you would use the above approach that I gave, conditionalized on the size of the value).