Skip to main content

Quantized Integer type in Python!

Project description

Quantized Integer Operations (QInt) Library

The Quantized Integer Operations (QInt) library is a Python package for performing arithmetic operations on quantized integers. It allows for exact arithmetic calculations while working with numbers that have been quantized for precision. This library is particularly useful when dealing with numerical computations that require high precision.

About

Quantized Integers (QInt) are a specialized numeric data type that represents integers with a defined level of precision. In simple terms, a quantized integer consists of two components:

  1. Value: This is the integer value you want to represent.
  2. Precision: This is the number of decimal places to which the integer value is quantized. For example, if you have a value of 123 and a precision of 2, it means that you are representing the number 123 as 1.23 with two decimal places of precision. Quantized integers allow you to work with exact fractional values while maintaining control over precision.

Why are Quantized Integers Important?

1. Precision Control

Quantized integers provide precise control over the level of precision in numeric calculations. In contrast to floating-point numbers, which have limited precision and may introduce rounding errors, quantized integers enable exact arithmetic operations. This makes them essential for applications where precision is critical, such as financial calculations, scientific simulations, and engineering designs.

2. Avoiding Floating-Point Errors

Floating-point arithmetic in traditional programming languages (e.g., Python's float) can result in unexpected errors due to the inherent limitations of floating-point representations. Quantized integers eliminate these errors by representing values as scaled integers, ensuring that calculations are performed exactly as intended.

3. Deterministic Results

Quantized integers offer deterministic results in computations. This means that the same operations performed on quantized integers will always yield the same results, regardless of the platform or environment. This determinism is crucial in applications like cryptography, where consistent results are essential.

4. Compatibility with Integer Operations

Despite their fractional representation, quantized integers can seamlessly interact with regular integer operations, making them versatile for various use cases. You can add, subtract, multiply, divide, and perform other standard mathematical operations on quantized integers just like regular integers.

When Should I Use Quantized Integers?

Quantized integers are particularly valuable in scenarios where precision matters and floating-point inaccuracies can lead to significant problems. Consider using quantized integers in the following situations:

  • Financial Calculations: Precise monetary calculations require exact representation of decimal values to avoid rounding errors.
  • Scientific Research: In scientific simulations and experiments, maintaining precision is crucial for accurate results.
  • Control Systems: Systems that rely on precise numeric values, such as robotics and automation, benefit from deterministic calculations.
  • Cryptography: Cryptographic algorithms demand exact results to ensure the security and reliability of the system.

Installation

You can install the library using pip like so:

pip install qint

Usage

To use the QInt library, you first need to import the necessary modules:

from qint import qint

Creating a Quantized Integer

You can create a quantized integer using the QInt class. Quantized integers are represented by two attributes: value and precision, where value is the quantized integer value and precision is the precision of the quantized value.

# Create a QInt with a value of 123 and precision of 2
qint = QInt(123, 2)

If you instead want to create a QInt based on an actual float or integer value at a given precision, instead call the static create method:

# Create a QInt with a value of 147 and a precision of 2
qint = QInt.create(1.47, 2)

Arithmetic Operations

QInt supports various arithmetic operations, including addition, subtraction, multiplication, division, and more. It is worth noting that addition and subtraction can be performed either with another QInt, or with an integer. If done with an integer, it is treated with the implied precision. For example:

q1 = QInt.create(4.00, 2)
q2 = QInt.create(4.00, 2)
q3 = q1 + q2 # QInt(800, 2)
q4 = q1 + 4 # QInt(800, 2)

In addition to integers and QInts, multiplication and division can also be performed with python Fraction objects (from the standard fractions library):

from fractions import Fraction

q1 = QInt.create(4.00, 2)
q2 = QInt.create(4.00, 2)
q3 = q1 + q2 # QInt(1600, 2)
q4 = q1 * 4 # QInt(1600, 2)
q5 = q1 * Fraction(1, 2) # QInt(200, 2)

Comparison Operations

You can also compare QInt instances using standard comparison operators like ==, !=, <, <=, >, and >=.

qint1 = QInt(100, 2)
qint2 = QInt(200, 2)

if qint1 < qint2:
    print("qint1 is less than qint2")

Safe Arithmetic - QInts with Different Precisions

QInts are designed to throw an error when attempting to perform operations at different precisions. This is by design to ensure careful usage and ensure uniformity. However, if you wish to perform arithmetic, there are also "safe" methods which allow you to operate on QInts with different precisions. These must be explicitly called as follows, and always return a QInt with the greater precision of the two being operated on:

q1 = QInt(400, 2)
q2 = QInt(4000, 3)
q3 = q1 + q2 # Precision error
q4 = q1.add(q2) # QInt(8000, 3)

Speed

Making efficient calculations with quanitzed integers is a top priority! Speed tests and improvements are coming soon!

Contributing

We welcome contributions to the QInt library. If you find a bug or have an idea for an enhancement, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

We would like to thank the open-source community for their contributions and support in making the QInt library possible.

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

qint-0.2.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

qint-0.2.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file qint-0.2.0.tar.gz.

File metadata

  • Download URL: qint-0.2.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for qint-0.2.0.tar.gz
Algorithm Hash digest
SHA256 baa855210c61862556b9492a6f8b063a5e8754fbb666e1d960820344adb4f157
MD5 db478a8b2150173edc4fa78be4b1959d
BLAKE2b-256 97c41a2ea6219c5b460a6dc82f8fb4b598524d1a2796acdc0b469f68dd70997b

See more details on using hashes here.

File details

Details for the file qint-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: qint-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for qint-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a6505a515f1f23fedca47ae78aff668631e035e6ff8acb8c33c0118f31af346
MD5 990a553fb04de6e80181350d8d34a4e9
BLAKE2b-256 a1c410580bbd50c94e91f11c6f01e3edbb596683ae4ef7f0d821610141052b3a

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