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.
- High Precision: Arbitrary precision arithmetic for both integer and fractional parts.
- Multiple Initialization: Supports initialization from
str
,int
, andfloat
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.
Using pip:
pip install hpf
- 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)'
- 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
python -m hpf
from hpf import HighPrecisionFloat
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)
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
a = HighPrecisionFloat("100.001")
b = HighPrecisionFloat("100.002")
print(a < b) # True
print(a == b) # False
print(a >= b) # False
num = HighPrecisionFloat("-123.45")
num = -num # Convert to positive
print(str(num)) # 123.45
num = +num # Pos marking (no-op)
print(str(num))
# Set precision during initialization
div1 = HighPrecisionFloat("22", precision=50)
div2 = HighPrecisionFloat("7", precision=50)
print(str(div1/div2))
__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)<
, (TODO)<=
, (TODO)>
, (TODO)>=
__str__/__repr__
: String representationneg()/pos()
: Sign manipulation
- Performance: Operations on very large numbers or high precision settings may impact performance.
- Division Precision: The
precision
parameter in division defaults to 10 decimal places. Increase this for more precise results. - Zero Handling: Trailing fractional zeros are automatically removed in string representation.
MIT License - See LICENSE file for details.
Chenyun Z.
Created: Oct 27 2024
Last Updated: Feb 18 2025
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!