Skip to main content

A simple module for approximate floating point arithmetic.

Project description

Approximate floating point arithmetic library. This simple module can be used to compare numbers using a relative and absolute tolerance, to mitigate floating point rounding errors.

from rr.approx import Approx

x = Approx(0.1) * 3
print x == 0.3  # True

The Approx class is very simple to use as a replacement for “regular” floats – you can use Approx objects instead of floats in most (if not all) contexts: arithmetic and comparisons.

The ApproxContext class, also accessible as Approx.Context, provides a context manager to temporarily modify the module’s tolerance parameters.

print Approx.Context()  # display current context
Approx.Context(rtol=1e-4, atol=1e-2).apply()  # permanently modify tolerances
print Approx.Context()
with Approx.Context(rtol=1e-5, atol=1e-3):  # temporary modification
    print Approx.Context()
print Approx.Context()

Disclaimer

Please note that this module does not solve any problems with floating point numbers. What it does is provide a little (or big, depending on how you configure your tolerance parameters) buffer to compensate for rounding errors, but as these errors accumulate you can always get unexpected results.

Float rounding is inherently innaccurate in all computers and programming languages due to representing a possibly recurring decimal in a finite number of digits. If you really care about exact results and don’t mind paying a performance penalty, you should check out the decimal module from the standard library or some other alternative.

Installation

>From PyPI (“stable” release):

pip install rr.approx

Or from the Git repo:

git clone https://github.com/2xR/rr.approx.git
pip install ./rr.approx

Contributing

Contributions are welcome through github pull requests (tests would sure be nice to have… :P).

And if you’re using the library and would like to say “thanks!” and/or keep me working on it, why not buy me a beer?

Happy approximate comparisons!

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

rr.approx-0.1.2.zip (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rr.approx-0.1.2-py2-none-any.whl (8.3 kB view details)

Uploaded Python 2

File details

Details for the file rr.approx-0.1.2.zip.

File metadata

  • Download URL: rr.approx-0.1.2.zip
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rr.approx-0.1.2.zip
Algorithm Hash digest
SHA256 7286e8c9dce4b82515ec819862a32b82219f3785be5a6672c695f8c0820d4ef2
MD5 e21d2276c5cbd8c2693fcd1de0f3543e
BLAKE2b-256 2c79f923a3e799098cb7274f843e2cc541b8f3a28da122001840c4929e4fce80

See more details on using hashes here.

File details

Details for the file rr.approx-0.1.2-py2-none-any.whl.

File metadata

File hashes

Hashes for rr.approx-0.1.2-py2-none-any.whl
Algorithm Hash digest
SHA256 84defdd40298e11f19cbdd66279b2bddb65856190b8bbc8cc11836c70a0e278c
MD5 3918971201548a09b57069aefe7319a4
BLAKE2b-256 5385dbb69117372ac076351a92472e562a83d557f1b69bd553185d19f33ab54a

See more details on using hashes here.

Supported by

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