DELV FixedPoint Python repo
The FixedPoint arithmetic package provides a Python endpoint for simulationg FixedPoint arithmetic that follows standards established by the Ethereum Solidity community.
Install
We are on PyPI:
pip install fixedpointmath
Testing
Testing is achieved with py.test.
Contributions
Please refer to CONTRIBUTING.md.
Documentation
This numeric representation is a noiseless alternative to floating point arithmetic.
Floating point rounding errors can be deteremental when precision matters, such as in the case of blockchain simulation.
The FixedPoint conducts all operations using 18-decimal fixed-point precision integers and arithmetic.
Number format
This package supports 18-decimal fixed-point precision numbers for arithmetic.
Briefly, this means our representation for unity, "one", is 1 * 10 ** 18, which would be 1.0 when cast to a float.
Unlike typical floats, a FixedPoint numeric always supports 18 decimal digits of precision, regardless of the scale of the number.
If you want the integer scaled representation, which can be useful for communicating with Solidity contracts, you must ask for it explicitly, e.g. FixedPoint("8.52").scaled_value == 8520000000000000000.
Conversely, if you want to initialize a FixedPoint variable using the scaled integer representation, then you need to instantiate the variable using the scaled_value argument, e.g. FixedPoint(scaled_value=8).
In that example, the internal representation is 8, so casting it to a float would produce a small value: float(FixedPoint(scaled_value=8)) == 8e-18.
If you cast FixedPoint numbers to ints or floats you will get an "unscaled" representation, e.g. float(FixedPoint("8.0")) == 8.0 and int(FixedPoint("8.528")) == 8.
We have purposefully constrained support for mixed-type operations that include the FixedPoint type.
Due to a lack of known precision, operations against Python floats are not allowed (e.g. float * FixedPoint will raise an error).
However, operations against int are allowed.
In this case, the int argument is assumed to be "unscaled", for example int(8) * FixedPoint(8) == FixedPoint(int(8)) * FixedPoint(8) == FixedPoint(64).
The internal "scaled" representation would be FixedPoint(64).scaled_value == 64 * 10**18.
Warning! Using floating point as a constructor to FixedPoint can cause loss of precision. For example,
>>> FixedPoint(1e18)
FixedPoint("1000000000000000042.420637374017961984")
Allowing floating point in the constructor of FixedPoint will be removed in a future version of fixedpointmath.
Computing with FixedPoint
The FixedPoint class abstracts away an internal integer representation and provides a suite of operations that act upon the class.
For example,
>>> from fixedpointmath import FixedPoint
>>> float(FixedPoint(8.0))
8.0
>>> int(FixedPoint(8.528))
8
>>> int(8) * FixedPoint(8)
FixedPoint("64.0")
>>> 8.0 * FixedPoint(8)
TypeError: unsupported operand type(s): <class 'float'>
The last example throws a TypeError due to the lack of known precision between Python float and FixedPoint.
Metadata
Release files for fixedpointmath 0.2.1
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 |
|---|---|---|---|---|
| fixedpointmath-0.2.1.tar.gz | 15.6 kB | Without provenance | Details for fixedpointmath-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fixedpointmath-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for fixedpointmath-0.2.1-py3-none-any.whl |
Total release size: 31.4 kB
Release files / fixedpointmath-0.2.1.tar.gz
| Download URL | fixedpointmath-0.2.1.tar.gz |
|---|---|
| Size | 15.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dced4f0053ee01ea98438a6d10a71b22e976d66d5f859029620703eea3126a6a
|
|
BLAKE2b-256 checksum How to use checksums |
6c8a334f4b86d2de0a06c1ea7aa9baa0cac5d90c42f243cd2779b329eed5fc27
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.11
|
Release files / fixedpointmath-0.2.1-py3-none-any.whl
| Download URL | fixedpointmath-0.2.1-py3-none-any.whl |
|---|---|
| Size | 15.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d1ff38663ae43dab38a5cb2e1ca2f309e430f32c17339f11351ef5cd8cd063f8
|
|
BLAKE2b-256 checksum How to use checksums |
9b51381bda68ec144620596fdcbad29873a5dca00e35e1df6d07e1f7e6e173e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.11
|
Security for fixedpointmath 0.2.1
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.