1

I'm computing log2 for very large numbers

For example:

math.ceil(math.log2(pow(2,64)+1))

This should return 65, however, it returns 64, since math.log2(pow(2,64)+1) returns 64.0 with low precision

Any thoughts how to work around?

4
  • 1
    You will need a multi-precision number type if you need more than 53 bits in a floating point mantissa. Commented Jan 11, 2019 at 8:53
  • I think, mpmath library may be what you're looking for. Also, you can try decimal module of standart Python library. Commented Jan 11, 2019 at 9:31
  • at least from your example the easiest way is to call bit_length() on the integer Commented Jan 11, 2019 at 12:48
  • Thanks James, I also found out yesterday that bit_length is the easiest.
    – Hyoceansun
    Commented Jan 12, 2019 at 2:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.