Fixed-point arithmetic and type that mirrors popular Solidity implementations
Project description
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
.
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
Built Distribution
File details
Details for the file fixedpointmath-0.2.1.tar.gz
.
File metadata
- Download URL: fixedpointmath-0.2.1.tar.gz
- Upload date:
- Size: 15.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dced4f0053ee01ea98438a6d10a71b22e976d66d5f859029620703eea3126a6a |
|
MD5 | fd5868bd415c55625f7eb03ede943dba |
|
BLAKE2b-256 | 6c8a334f4b86d2de0a06c1ea7aa9baa0cac5d90c42f243cd2779b329eed5fc27 |
File details
Details for the file fixedpointmath-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: fixedpointmath-0.2.1-py3-none-any.whl
- Upload date:
- Size: 15.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1ff38663ae43dab38a5cb2e1ca2f309e430f32c17339f11351ef5cd8cd063f8 |
|
MD5 | e0ec45168406c7a4f4407cd9e9e0b0fb |
|
BLAKE2b-256 | 9b51381bda68ec144620596fdcbad29873a5dca00e35e1df6d07e1f7e6e173e2 |