Skip to main content

A small, easy-to-use wrapper around Python's Decimal with configurable precision and rounding.

Project description

bigmathlib — Arbitrary-Precision Decimal Numbers for Python

bigmathlib is a tiny wrapper around Python’s built-in decimal.Decimal type.

It gives you:

  • Easy, consistent handling of big numbers (money, crypto, prices, etc.)
  • Configurable precision (DP) and rounding mode (RM)

Perfect when float is too imprecise and you want something safer but still easy to use.


Features

  • ✅ Arbitrary-precision decimal arithmetic
  • ✅ Global defaults for decimal places (DP) and rounding mode (RM)
  • ✅ Minimal, focused API
  • ✅ Works with str, int, float, Decimal, and Big values

Installation

If you’ve published this as a package:

pip install bigmathlib

Quick Start

from bigmathlib import Big

# Basic construction
a = Big("0.1")
b = Big("0.2")

# Addition
c = a + b
print(c)           # -> 0.3

# Or using methods, if you added them:
# c = a.add(b)

# Multiplication
d = Big("1.23") * Big("4")
print(d)           # -> 4.92

Because Big uses Decimal under the hood, you don’t get the usual floating-point surprises:

from bigmathlib import Big

print(0.1 + 0.2)        # 0.30000000000000004 (float)
print(Big("0.1") + Big("0.2"))  # 0.3 (Big)

Configuration

Big exposes two important global settings:

from bigmathlib import Big

# Maximum decimal places for division, sqrt, etc.
Big.DP = 20    # default: 20

# Rounding mode (like big.js)
# 0: ROUND_DOWN
# 1: ROUND_HALF_UP      (default)
# 2: ROUND_HALF_EVEN
# 3: ROUND_UP
Big.RM = 1

Internally, these map to decimal’s rounding modes via _ROUND_MAP.

You can also use decimal.localcontext() for temporary precision overrides inside your own code, but for most use cases, setting Big.DP and Big.RM is enough.

Creating Big Numbers

from bigmathlib import Big
from decimal import Decimal

Big("1.2345")         # from string
Big(12345)            # from int
Big(1.2345)           # from float (not recommended for exact finance values)
Big(Decimal("1.2345"))# from Decimal
Big(Big("1.2345"))    # from another Big

Under the hood, the constructor converts the value to a Decimal and stores it as self._d.

Arithmetic

The exact API here depends on how you implemented Big.

Below is a typical pattern using operator overloading.

Addition

x = Big("1.1")
y = Big("2.2")
print(x + y)      # -> 3.3

Subtraction

x = Big("5")
y = Big("2.5")
print(x - y)      # -> 2.5

Multiplication

x = Big("1.5")
y = Big("3")
print(x * y)      # -> 4.5

Division

Division respects Big.DP and Big.RM:

from bigmathlib import Big

Big.DP = 10
Big.RM = 1  # ROUND_HALF_UP

x = Big("1")
y = Big("3")

print(x / y)  # -> 0.3333333333  (10 decimal places)

License

MIT License

Copyright (c) 2025 Jason Foley

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

bigmathlib-1.0.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

bigmathlib-1.0.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file bigmathlib-1.0.0.tar.gz.

File metadata

  • Download URL: bigmathlib-1.0.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for bigmathlib-1.0.0.tar.gz
Algorithm Hash digest
SHA256 abc0ea88d6309670913b8799d50bb19de9a1a0b672e0288e367e5b81973594d5
MD5 da34299e004abd36c4eca1e83e8273e8
BLAKE2b-256 2c2e6053cc555223c43dd1109e3a6850b2cc1647d89d16231cad9c35d7702d70

See more details on using hashes here.

File details

Details for the file bigmathlib-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: bigmathlib-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for bigmathlib-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a53e3b906d69b94d2fbd32f02d514909ce3485fccec8e27b5171805f421f5c1
MD5 fc803d0a253e3db1dcb50c4e36a30f84
BLAKE2b-256 141bd937c8ef5f1e069cd56c88bba1e085e99b74a6c784f6b723f08d64e61a30

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