diff --git a/tests/test_noxm.py b/tests/test_noxm.py index fe3edaf..fc64a2d 100644 --- a/tests/test_noxm.py +++ b/tests/test_noxm.py @@ -19,5 +19,7 @@ def test_power2(): assert xmextras.noxm.power2(0) == 1 assert xmextras.noxm.power2(1) == 1 assert xmextras.noxm.power2(2) == 2 + assert xmextras.noxm.power2(2.5) == 4 assert xmextras.noxm.power2(3) == 4 + assert xmextras.noxm.power2(3.5) == 4 assert xmextras.noxm.power2(65537) == 131072 diff --git a/xmextras/noxm.py b/xmextras/noxm.py index 6d73427..28a89ac 100644 --- a/xmextras/noxm.py +++ b/xmextras/noxm.py @@ -3,6 +3,7 @@ ''' from datetime import datetime +from math import frexp, ldexp J1950_DELTA = 631152000.0 @@ -36,7 +37,8 @@ def power2(x): Returns the next power of two greater than or equal to x. ''' - if x < 1: + if x <= 1: return 1 - return 1 << (x-1).bit_length() + fraction, exponent = frexp(x) + return int(ldexp(1, exponent - (fraction == 0.5)))