Simple python library for modulo operations
Project description
Modulos
Simple pure Python library implementing modulo operations
Purpose
Modulo operation implement remainder of a division, based on this simple formula, with an integer quotient: $$ dividend = divisor * quotient + remainder$$
However, as quotient can be any integer, multiple definitions of modulo can outputs different result, depending of the implementation used. This can be seen with math.fmod which explicitely says that it may have different output from % symbol.
This library fixes that by adding 5 different implementations of the operation :
- Euclidean modulo (output smallest positive remainder. Probably the one you're looking for if you're coming from mathematical background)
- Rounded modulo (output closest to zero remainder)
- Floored modulo (output remainder with same sign as divisor)
- Truncated modulo (outputs remainder with same sign as dividend)
- Ceiled modulo (outputs remainder with opposite sign as divisor)
This may be useful for (but not limited to):
- Port code from another language implementing different definition of modulo
- Use specific mathematical properties of a definition (especially euclidean or rounded)
- Use same mathematical definition for integers and floats values
Example
from modulos.modulos import euclidean_modulo, rounded_modulo, floored_modulo, ceiled_modulo, truncated_modulo
print(f"{euclidean_modulo(5,3)=}")
print(f"{rounded_modulo(5,3)=}")
print(f"{floored_modulo(5,3)=}")
print(f"{truncated_modulo(5,3)=}")
print(f"{ceiled_modulo(5,3)=}")
Why would I use these library when Python math library already contains some of its functions ?
Contrary to math.fmod (for truncated modulo) and math.remainder (for rounded modulo), this library keeps same type as inputs (if both are same type). This can be particularly interesting when working with Decimal library, to avoid rounding issues.
Be aware that this means this library does not cast values from the types they were in. Generally it does not cause any issue, but if your dividend and divisor types does not support division between them, you will have to manually perform the cast. This is for example the case between Decimal built-in library and Numpy types. It does also mean it does not correct roundness issues within float.
Conributions
You're welcome to contribute to the source code by opening issue or pull request ;)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file modulos-0.1.0.tar.gz
.
File metadata
- Download URL: modulos-0.1.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c9176b1aff8d1db06f19b51050bf0e0c6a9199f963828f42e23c245961cd333 |
|
MD5 | bcba34514817e45418f6841a98bc6ee9 |
|
BLAKE2b-256 | ca360435bdb8c00ab69a97c6ed55f6f02de4b6a9c78c5556f59e578c598ee9da |
File details
Details for the file modulos-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: modulos-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b315b400cfd96e8cfdb9d54a00fb72f01816d22e2717c8ec58eba882488c9809 |
|
MD5 | 51e2e5cd5e57301625a63c33b42728ef |
|
BLAKE2b-256 | 97ae56d01611636661ea56be828d5365617dc5f515b1dc95506b2087e1559c1f |