Re:Ten Ways to Check if an Integer Is a Power Of Two in C
by eru on Tuesday July 26, 2011 @11:41AM
(#2807836)
math.log uses floating point arithmetic. That will lead to trouble on large numbers. The article addresses the issue.
[ Parent
| on HN ]
Re:Ten Ways to Check if an Integer Is a Power Of Two in C
by zxw on Tuesday July 26, 2011 @12:12PM
(#2808001)
I'm not doing power == int(power) though which is what he warns against. I haven't done a lot of testing however as far I can tell it's working.
>>> is_power_of_two(2 ** 31 - 1)
False
>>> is_power_of_two(2 ** 31)
True
>>> is_power_of_two(2 ** 31 + 1)
False
>>> is_power_of_two(2 ** 548 - 1)
False
>>> is_power_of_two(2 ** 548)
True
>>> is_power_of_two(2 ** 548 + 1)
False