alexaltea / ntypes Goto Github PK
View Code? Open in Web Editor NEWEmulate native integer and floating-point types in Python
Home Page: https://pypi.python.org/pypi/nativetypes
License: MIT License
Emulate native integer and floating-point types in Python
Home Page: https://pypi.python.org/pypi/nativetypes
License: MIT License
I think that the __format__
method should be implemented as well.
ntypes/nativetypes/native_int.py
Line 94 in 27cf3aa
needs a signed=True argument https://docs.python.org/3/library/stdtypes.html#int.to_bytes
Following off-by-one error occurs in negative integer divisions:
>>> int32(-7) // int32(5)
nint(-2)
The expected result would be -1
. Root cause is that in Python -7 // 5 == -2
.
Hi !
I am trying to use your module to play with a Feistel cipher and noticed that at each iteration, the code gets slower.
I figured that using inplace operators was the problem.
I came up with a minimal example showing the difference between inplace and "normal" operators:
This code is getting slower after a few iterations:
from nativetypes import *
x = uint32(1)
while True:
print(x)
x *= x
Replacing the inplace operator gets a significant speed improvement:
from nativetypes import *
x = uint32(1)
while True:
print(x)
x = x * x
From what I understand, this happens because the following code assumes that the op result is an integer, where it is in fact another nint object. This leads to nested nint objects that slow down the operations.
ntypes/nativetypes/native_int.py
Lines 102 to 105 in 27cf3aa
It is not possible to get a slice from a nint including the most significant bit:
>>> from nativetypes import uint8
>>> u8=uint8(1<<7)
>>> bin(u8)
'0b10000000'
>>> u = u8[7:8]
Traceback (most recent call last):
File "native_int.py", line 121, in __getitem__
assert 0 <= key.stop < self.b
AssertionError
IMO the assertion in __getitem__
is wrong and should be instead
assert 0 <= key.stop <= self.b
since the last element index is actually key.stop-1
in Python
This seems to work:
>>> from nativetypes import uint8
>>> u8=uint8(1<<7)
>>> bin(u8)
'0b10000000'
>>> u = u8[7:8]
>>> u
nint(1)
Would be great to implement the C-style modulo (the one that applies the sign of the dividend to the result) along with the Python-style modulo, because the vast majority of low-level languages use that implementation.
I mean to define a modulo operation like this:
def mod(a, n):
return a - int(a / n) * n
Or maybe this one, less readable you know the operator priorities in Python:
def mod(a, n):
return a - a // n * n
The thing is that I don't know how to implement that in your code so it becomes trivial to use. The __mod__
operator is already used for the python operator.mod
. I don't know if is sane to add a new method to the class or is better to replace the current one.
Off-topic: Thank you! I started a week ago to write my int32
class for a ported code, and today I noticed that this code already existed. I'll migrate all now. Surprisingly our coding style is very similar. ๐
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.