Skip to main content

The package contains mathematical rounding function

Project description

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:

  • n is the number to round;
  • p is 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 -1515 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

math_rounding-1.0.2.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

math_rounding-1.0.2-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file math_rounding-1.0.2.tar.gz.

File metadata

  • Download URL: math_rounding-1.0.2.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for math_rounding-1.0.2.tar.gz
Algorithm Hash digest
SHA256 9c65a72b5e01c7d2687f570ff53480dd314e75e24bd9d6b0c382ef7aeefa8741
MD5 ca44a0186f3fbfe7e6a1676ef212701c
BLAKE2b-256 b81853da3fdd6c37699504eb52f9a853a5c602c7586e6b3ffbf6f4555b7d63d2

See more details on using hashes here.

File details

Details for the file math_rounding-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: math_rounding-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for math_rounding-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0f281c1e262ae422921d018552b34f267ad0a50d047f1ee6df459a978e448fd6
MD5 269eb7737b90d6f5a86828b04eabccab
BLAKE2b-256 ac17a77c051d173daf63987429b2b62c9705972c6da0a98dd09a6eca1b6eb19e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page