Skip to content

Commit

Permalink
Correction to power2 to account for float inputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ballenspectric committed Jul 16, 2024
1 parent a6869cb commit 06d9981
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 2 additions & 0 deletions tests/test_noxm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions xmextras/noxm.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
'''

from datetime import datetime
from math import frexp, ldexp

J1950_DELTA = 631152000.0

Expand Down Expand Up @@ -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)))

0 comments on commit 06d9981

Please sign in to comment.