Skip to main content

A high precision floating-point arithmetic module

Project description

High Precision Float (HPF) Platform

A pure Python implementation of a high-precision floating-point arithmetic class, designed to handle mathematical operations with enhanced precision beyond standard floating-point limitations.

Features

  • High Precision: Arbitrary precision arithmetic for both integer and fractional parts.
  • Multiple Initialization: Supports initialization from str, int, and float types.
  • Operator Overloading: Full support for +, -, *, /, //, and comparisons.
  • Negative Values: Handles negative numbers with proper sign propagation.
  • Custom Precision: Optional precision setting for division operations.
  • String Representation: Clean string output with automatic trailing zero removal.

Installation

From PyPI

Using pip:

pip install hpf

From Source

  1. Use Source:

Simply include the hpf.py file in your project and import the class:

from hpf import HighPrecisionFloat  # or 'from hpf import hpf, HPF(both OK)'
  1. Compile Yourself:

There are 2 methods:

  • Use setup.py
git clone https://github.com/zprolab/hpf
cd hpf 
pip install setuptools
setup.py install # Auto Install!
  • Use build (recommend)
git clone https://github.com/zprolab/hpf
cd hpf 
rm -rf ./dist
pip install setuptools build
python -m build # Auto Build!
pip install dist/*.whl

Usage

Test

python -m hpf

Initialization

from hpf import HighPrecisionFloat

Abbreviations

from hpf import HighPrecisionFloat as hpf

or

from hpf import HighPrecisionFloat as HPF

or

from hpf import hpf

or

from hpf import HPF
a = HighPrecisionFloat("3.14159265358979323846", precision=25)
b = HighPrecisionFloat(-42.75)
c = HighPrecisionFloat(1000)

Basic Arithmetic

x = HighPrecisionFloat("10.5")
y = HighPrecisionFloat("3.2")

print(x + y)  # 13.7
print(x - y)  # 7.3
print(x * y)  # 33.6
print(x / y)  # 3.28125
print(x // y) # 3

Comparison Operations

a = HighPrecisionFloat("100.001")
b = HighPrecisionFloat("100.002")

print(a < b)   # True
print(a == b)  # False
print(a >= b)  # False

Sign Manipulation

num = HighPrecisionFloat("-123.45")
num = -num  # Convert to positive
print(str(num)) # 123.45

num = +num  # Pos marking (no-op)
print(str(num))

Precision Control

# Set precision during initialization
div1 = HighPrecisionFloat("22", precision=50)
div2 = HighPrecisionFloat("7", precision=50)
print(str(div1/div2))

Method Overview

Core Methods

  • __init__: Constructor with value parsing
  • _add_abs/_sub_abs: Internal absolute addition/subtraction
  • _mul_abs/_div_abs: Internal absolute multiplication/division
  • _abs_greater: Absolute value comparison

(TODO) Operator Overloads

  • +, -, *, /, //
  • ==, !=, (TODO)<, (TODO)<=, (TODO)>, (TODO)>=

Utility Methods

  • __str__/__repr__: String representation
  • neg()/pos(): Sign manipulation

Considerations

  1. Performance: Operations on very large numbers or high precision settings may impact performance.
  2. Division Precision: The precision parameter in division defaults to 10 decimal places. Increase this for more precise results.
  3. Zero Handling: Trailing fractional zeros are automatically removed in string representation.

License

MIT License - See LICENSE file for details.

Author

Chenyun Z. Created: Oct 27 2024
Last Updated: Feb 18 2025

Something...

PyPI is a great invention to make package-managing easier!

GitHub Action is also a great invention to let we needn't to write python -m build again and again!

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

hpf-0.3.14.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

hpf-0.3.14.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file hpf-0.3.14.0.tar.gz.

File metadata

  • Download URL: hpf-0.3.14.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for hpf-0.3.14.0.tar.gz
Algorithm Hash digest
SHA256 68b39bec2bfb35c6f934b0e751cef8e3470e1baa3d02b50db31a9f738013fe4c
MD5 f5c855139cdb8ea1b9d75e19245c35e2
BLAKE2b-256 f49917508b0ba1b0452e75803fc49621214512f743a8a51438fed4cceaee544b

See more details on using hashes here.

File details

Details for the file hpf-0.3.14.0-py3-none-any.whl.

File metadata

  • Download URL: hpf-0.3.14.0-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for hpf-0.3.14.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d766d6d2d01428a24de9f0d24b2746ea68a6e41df20c7d79e436e52f8626ef44
MD5 80f25003676b6457c64e6dc886d279c2
BLAKE2b-256 d724b3049ae085a7f6f8ab595a210f0f602195a8f46813742f814529ba292f92

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