Fixed-point arithmetic and type that mirrors popular Solidity implementations
Project description
fixedpointmath
fixedpointmath is a package that solves the issue of noisy floating point in Python.
Floating point rounding errors can be deteremental when precision matters, such as in the case of blockchain simulation. To solve this issue, we built a fixed-point class to use integers to represent real numbers. Internally, the FixedPoint
class within fixedpointmath conducts all operations using 18-decimal fixed-point precision integers and arithmetic.
To avoid confusion, the FixedPoint
class abstracts the internal integer representation, and provides a suite of operations that act upon the class, including mixed-type operations. 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 classic float
and FixedPoint
.
We also provide support for accessing and initializing FixedPoint
with the integer internal representation, which can be useful for communicating with Solidity contracts. For example,
>>> from fixedpointmath import FixedPoint
>>> FixedPoint(8.52).scaled_value
8520000000000000000
>>> FixedPoint(scaled_value=int(8e18))
FixedPoint("8.0")
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
Hashes for fixedpointmath-0.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cc8fbb913f0cbfa3fc9cfe6bfb738fb4ed3b6790244de39d243bc1e785983e4 |
|
MD5 | 85e81d2a69aec571b746331263502997 |
|
BLAKE2b-256 | 392b6a462cd193431e7f09386c8a4d02246a973b38485d700c09a5fe3ecf196d |