Skip to main content

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

fixedpointmath-0.1.1.tar.gz (22.7 kB view hashes)

Uploaded Source

Built Distribution

fixedpointmath-0.1.1-py3-none-any.whl (14.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page