Skip to content Skip to sidebar Skip to footer

Dealing With Accuracy In Python Math Operations

I was trying to write a simple program to determine if input integer is a power of two. I had following code. It will fail the test case for n=536870912 (536870912 is the 2^29). I

Solution 1:

If you want to compare floats and allow for a little floating-point inaccuracy, you would normally check if they are within a certain allowable distance of each other (if abs(x-y) < epsilon).

However, if you want to find out if an integer is a power of 2, you can do it like this:

defispoweroftwo(n):
    return (n>0and (n&-n)==n)

This works according to the rules of two's complement representation of signed numbers.

>>> ispoweroftwo(536870911)
False>>> ispoweroftwo(536870912)
True

Solution 2:

The way to go about comparing floating point numbers for equality is abs(a - b) < tolerance, where tolerance = 1e-6 or some similar small number. In your case it would just be abs(y) < 1e-6.

For more info check out Accuracy here or a popular SO question.

Solution 3:

If you need accuracy and you don't want to reinvent the accuracy-wheel yourself, you could have a look at NumPy, which is precisely designed for this kind of purpose (accurately making complex mathematical operations on big numbers of any kind).

import numpy as np

x = np.array([0, 1, 2, 2**4, 536870912])
np.log2(x)

# array([-Inf,   0.,   1.,   4., 29.])

See documentation for np.log2() or a quickstart tutorial.

Post a Comment for "Dealing With Accuracy In Python Math Operations"