Commit cb9c9a5b authored by Stefan Behnel's avatar Stefan Behnel

also test in-place operator for power-of-2 optimisation (not currently optimised)

parent 1e0ed76a
...@@ -95,3 +95,32 @@ def optimised_pow2(n): ...@@ -95,3 +95,32 @@ def optimised_pow2(n):
assert isinstance(2.0 ** n, float), 'float %s' % n assert isinstance(2.0 ** n, float), 'float %s' % n
assert isinstance(2 ** n, (int, long)), 'int %s' % n assert isinstance(2 ** n, (int, long)), 'int %s' % n
return 2 ** n return 2 ** n
def optimised_pow2_inplace(n):
"""
>>> optimised_pow2_inplace(0)
1
>>> optimised_pow2_inplace(1)
2
>>> optimised_pow2_inplace(10)
1024
>>> optimised_pow2_inplace(30)
1073741824
>>> print(repr(optimised_pow2_inplace(32)).rstrip('L'))
4294967296
>>> print(repr(optimised_pow2_inplace(100)).rstrip('L'))
1267650600228229401496703205376
>>> optimised_pow2_inplace(30000) == 2 ** 30000
True
>>> optimised_pow2_inplace(-1)
0.5
>>> optimised_pow2_inplace(0.5) == 2 ** 0.5
True
>>> optimised_pow2_inplace('test')
Traceback (most recent call last):
TypeError: unsupported operand type(s) for ** or pow(): 'int' and 'str'
"""
x = 2
x **= n
return x
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment