2 hours ago · Tech · hide · 0 comments

Last week I wrote a post on hiding cryptographic keys in decks of cards. I wrote some code for that post that shouldn’t work, but before fixing I noticed that it in fact did work. The code computes logarithms for integers larger than the largest representable float. For example, the largest float is on the order of 10308, and yet the following code works. >>> import math >>> math.log10(10**400) 400.0 The log, log2, and log10 functions have some code inside that handles large integers specially. It doesn’t simply convert the integers to floats before taking the logarithm. If it did, it would overflow. While playing around with this I also noticed that you can define floats larger than the largest float without warnings. >>> math.log(1e308) 709.1962086421661 >>> math.log(1e309) inf This isn’t a feature of math.log but of how Python handles scientific notation. The expression 1e308 is the floating point representation of 10308. It is a float, not an int. >>> type(1e308) <class 'float'>…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.