Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
cb9c9a5b
Commit
cb9c9a5b
authored
May 03, 2014
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
also test in-place operator for power-of-2 optimisation (not currently optimised)
parent
1e0ed76a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
0 deletions
+29
-0
tests/run/powop.pyx
tests/run/powop.pyx
+29
-0
No files found.
tests/run/powop.pyx
View file @
cb9c9a5b
...
@@ -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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment