Mathematical rounding in Python
This package has one purpose: fast mathematical rounding of numbers.
Installation
Use the following command to install the package:
pip install math-rounding
The package can use numba to increase speed of the calculations.
If you want to install it with numba, you can use the following command:
pip install math-rounding[numba]
or just install numba separately.
Usage
The main function of the package math_rounding has the following signature:
def math_rounding(n: Number, p: int = 0) -> float:
...
where:
nis the number to round;pis integer number that specifies the position to which rounding will be performed.
p could be a positive number or a negative number. It represents the position in the number from the dot:
- when it's equal to
0(default), the rounding will be performed to integer; - when it's positive, it represents the position in fractional part of the number;
- when it's negative, it represents the position in integer part of the number.
Example
from math_rounding import math_rounding
print(math_rounding(0.5)) # 1.0
print(math_rounding(-0.5)) # -1.0
print(math_rounding(1.1)) # 1.0
print(math_rounding(0.05, 1)) # 0.1
print(math_rounding(5, -1)) # 10.0
print(math_rounding(15, -1)) # 20.0
Tests
pip install -e ".[numba]"
python -m unittest discover -s tests -v
Limitations
The function is optimized for typical float values and precision values. It uses IEEE-754 float arithmetic and (with numba installed) JIT-compiled code. Outside the ranges below, results may be wrong or an exception may be raised.
Precision p
p is the exponent in the scale factor 10**p. For a 64-bit float:
| Range | What happens |
|---|---|
Recommended: about -15 … 15 |
Stable for most numbers; matches the examples above. |
p ≥ 309 |
10**p overflows in Python; behavior is undefined. |
p around 307–308 |
10**p is finite, but abs(n) * 10**p can overflow to inf for ordinary n, which may yield nan. |
p ≤ -324 |
10**p underflows to 0.0; dividing by s raises ZeroDivisionError. |
p around -308 … -323 |
10**p is tiny; for many n the value abs(n) * 10**p + 0.5 is dominated by 0.5, so the result can be far from the expected rounded value. |
If you need extreme p, check the result or use another tool (for example decimal.Decimal).
Value n
| Case | Behavior |
|---|---|
nan, ±inf |
Returned unchanged. |
Very large |n| (roughly above 10**18 with default p=0) |
Safe with the current implementation; much larger values are still subject to float precision limits. |
Negative zero (-0.0) |
Sign is preserved when the rounded result is zero. |
| Binary floating-point | Values such as 2.675 at p=2 may not match pen-and-paper decimal rounding because n is not stored exactly in base 10. |
Metadata
Release files for math-rounding 1.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| math_rounding-1.0.2.tar.gz | 6.1 kB | Without provenance | Details for math_rounding-1.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| math_rounding-1.0.2-py3-none-any.whl | Python 3 | none | any | Details for math_rounding-1.0.2-py3-none-any.whl |
Total release size: 10.4 kB
Release files / math_rounding-1.0.2.tar.gz
| Download URL | math_rounding-1.0.2.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9c65a72b5e01c7d2687f570ff53480dd314e75e24bd9d6b0c382ef7aeefa8741
|
|
BLAKE2b-256 checksum How to use checksums |
b81853da3fdd6c37699504eb52f9a853a5c602c7586e6b3ffbf6f4555b7d63d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Release files / math_rounding-1.0.2-py3-none-any.whl
| Download URL | math_rounding-1.0.2-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0f281c1e262ae422921d018552b34f267ad0a50d047f1ee6df459a978e448fd6
|
|
BLAKE2b-256 checksum How to use checksums |
ac17a77c051d173daf63987429b2b62c9705972c6da0a98dd09a6eca1b6eb19e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.11
|
Security for math-rounding 1.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.