Skip to main content

Fixed-point data types for signal processing and blockchain applications

Project description

Welcome to Chainfix

chainfix

ci

Chainfix provides a way to represent numbers and perform simple math operations using fixed-point data types.

Chainfix supports binary fixed-point (base-2), decimal fixed-point (base-10) or even arbitrary (base-N) fixed-point types.

Fixed-point data types are used in a wide range of computing applications. Binary-fixed point types are commonly used in the design of specialized hardware, FPGAs, ASICS, and signal processing applications.

Decimal-fixed point types are commonly found in financial applications, blockchain, decentralized finance, and smart contracts (e.g. the solidity programming language).

Decimal fixed-point representations

The real-world value π can be represented with limited precision using two bytes in decimal fixed point format

>>> from chainfix import * 
>>> from math import pi
>>> pid = Fixd(pi, wordlength=16, precision=4)
Fixd(3.1416, 16, 4)

The .int property returns the stored integer value:

>>> pid.int
31416

The resolution of the decimal fixed-point data type is 10 ** -precision, which is also the value of one least significant bit:

>>> pid.lsb
0.0001

The range of numbers that can be represented with this precision using 16 bits is:

>>> (pid.lower_bound, pid.upper_bound)
(-3.2768, 3.2767)

The real-world value can also be displayed as an exact ratio of integers

>>> pid.as_integer_ratio()
(3927, 1250)

The .hex property returns the two's complement representation of the stored integer

>>> pid.hex
'0x7ab8'

When Fixd stores a negative number, the MSB of the stored integer is always 1:

>>> Fixd(-pi, 16, 4).hex
'0x8548'

Binary fixed-point representations

Likewise, π can also be represented with limited precision using two bytes in binary fixed point format

>>> pib = Fixb(pi, 16, 12)
Fixb(3.1416015625, 16, 12)

The .int property returns the stored integer value:

>>> pib.int
12868

The resolution of the decimal fixed-point data type is 2 ** -precision, which is also the value of one least significant bit:

>>> pib.lsb
0.000244140625

The range of numbers that can be represented with this precision using 16 bits is:

>>> (pib.lower_bound, pib.upper_bound)
(-8.0, 7.999755859375)

The .hex property returns the two's complement representation of the stored integer

>>> pib.hex
'0x3244'

When Fixb is used to store a negative number, the MSB of the stored integer is always 1:

>>> Fixb(-pi, 16, 4).hex
'0xffce'

Contexts

Chainfix provides a fixed-point context to control the default behavior for new fixed-point objects.

The current context can be retrieved using

>>> ctx = get_decimal_context()
>>> ctx.wordlength
256
>>> ctx.precision
18

for Fixd and Ufixd values or

>>> ctx = get_binary_context()
>>> ctx.wordlength
32
>>> ctx.precision
16

for Fixb and Ufixb values.

The context can be modified to change the behavior of newly constructed values:

>>> get_decimal_context().wordlength = 20
>>> get_decimal_context().precision = 5
>>> Fixd(pi)
Fixd(3.14159, 20, 5)

When passed through the constructor, the values for wordlength and precision always take precedence over the context.

>>> Fixd(pi, precision=10)
Fixd(3.1415926536, 256, 10)

Note that resulting data type has insufficinet range to represent the value pi.

Future Work

  • Support math operations for fixed-point types using the applicable context

Contributing

Chainfix can be installed in developer mode after cloning the repository:

$ pip install -e .

To run all tests:

$ pytest

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

chainfix-0.1.1.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

chainfix-0.1.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file chainfix-0.1.1.tar.gz.

File metadata

  • Download URL: chainfix-0.1.1.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.7

File hashes

Hashes for chainfix-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c94f30fdd5937e47834f6ff6dabc65e6b1937dd2e73d3bc487c4950a2d9d2a78
MD5 b2aae7327e3caa2f4a778913a84273ab
BLAKE2b-256 acad023472231a12ed506782eabdc737c57d7b46c27b83747f04e10c49dd7744

See more details on using hashes here.

File details

Details for the file chainfix-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: chainfix-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.7

File hashes

Hashes for chainfix-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3e7f75396e3b70355182d4ac6136ccf08517e3b0ca7e23007f323e6ff8ce095b
MD5 51f07c6fdff36110b31281949d7d641d
BLAKE2b-256 c3ad1c9b2d25868e894eb161c5cc661d8fe07994725a77125c9943c9b6a5f2ee

See more details on using hashes here.

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