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.
Contributions
You're welcome to contribute to the source code by opening issue or pull request ;)
A basic test setup have been implemented, here are the steps to run the unit tests on a linux environment:
cd modulos
python3 -m venv venv
. venv/bin/activate
pip install --upgrade pip
pip install -e .
pip install -r requirements_dev.txt
pytest
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.2.0.tar.gz
.
File metadata
- Download URL: modulos-0.2.0.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea59bc69ced75ebe65851602d6d10c396dcc5b3f4ef1bb83c1dc7382180c4fdd |
|
MD5 | 02ca859950d8d9696c36ce91f7f61eb9 |
|
BLAKE2b-256 | 315a9250cebbcdc63278d3de886bb991e15df2fecc8234c2688f747f5bc4cd03 |
File details
Details for the file modulos-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: modulos-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.4 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 | aa7bdd3bdf6ed77db1f0c50b586893b71b83a71e16f3bf56ce42431cee7beda9 |
|
MD5 | 035d6de5984759b212c1a5a1bf559ff5 |
|
BLAKE2b-256 | c25c57b2370a6db3b318e2080c224d03ed8cfe8f08c9f34ab19ff1b1c3254355 |