----- Original Message -----
From: "David Lehman" <dlehman(a)redhat.com>
To: anaconda-patches(a)lists.fedorahosted.org
Sent: Friday, October 31, 2014 4:01:58 PM
Subject: Re: [blivet:master 3/3] Add a method that determines whether a number is an
exact power of 2.
On 10/31/2014 10:31 AM, mulhern wrote:
> Taking the log to determine whether a number is an exact power of 2 does
> not always work, because log computes w/ floats, somehow. It usually works,
> because base 2 works well w/ binary floating point representation.
>
> Decimal does not actually have a log function. You can use change of base
> to get log base 2, but you do not get an exact result.
>
> Use method in is_valid_pool_chunk_size().
>
> Signed-off-by: mulhern <amulhern(a)redhat.com>
> ---
> blivet/devicelibs/lvm.py | 3 +--
> blivet/util.py | 28 ++++++++++++++++++++++++++++
> tests/util_test.py | 22 ++++++++++++++++++++++
> 3 files changed, 51 insertions(+), 2 deletions(-)
> create mode 100644 tests/util_test.py
>
<-- SNIP -->
>
> diff --git a/blivet/util.py b/blivet/util.py
> index a69b694..e045a32 100644
> --- a/blivet/util.py
> +++ b/blivet/util.py
> @@ -524,3 +524,31 @@ def variable_copy(obj, memo, omit=None, shallow=None,
> duplicate=None):
> def get_current_entropy():
> with open("/proc/sys/kernel/random/entropy_avail", "r") as
fobj:
> return int(fobj.readline())
> +
> +def power_of_two(value):
> + """ Checks whether value is a power of 2 greater than 1.
> +
> + :param any value: a value
> + :returns: True if the value is a power of 2
> + :rtype: bool
> + """
> + try:
> + int_value = int(value)
> + except (ValueError, TypeError):
> + return False
> +
> + if int_value != value:
> + return False
> +
> + value = int_value
> +
> + if value < 2:
> + return False
> +
> + (q, r) = (value, 0)
> + while q != 0:
> + if r != 0:
> + return False
> + (q, r) = q.__divmod__(2)
Why not divmod(q, 2)?
Fixed!
<-- SNIP -->
_______________________________________________
anaconda-patches mailing list
anaconda-patches(a)lists.fedorahosted.org
https://lists.fedorahosted.org/mailman/listinfo/anaconda-patches