Skip to main content

SFFloat class for floating point computations that account for precision or 'sig figs'.

Project description

Python package

sffloat

The sffloat package defines a SFFloat class that allows easy floating point computations that take precision or "significant figures" into account.

features

  • Seamlessly mix float and SFFloat objects in arithmetic. Standard floating point objects are automatically promoted to SFFloat when necessary.
  • Print and display SFFloat objects as if they are standard float numbers.
  • Compare SFFloat objects with floating point numbers to determine equivalence, taking precision into account.
  • Use SFFloat versions of all standard Python math functions.

example use

importing SFFloat

from sffloat import SFFloat

calculating circle area using an imprecise radius

# Create a floating point radius value of 3.4 with 2 sig figs
from math import pi
radius = SFFloat(3.4, 2)

# Compute the circle area, with sig figs
area = pi * radius ** 2
print(area)
36

addition example

a = SFFloat(0.00123, 3)
b = SFFloat(0.1234, 4)
print(a + b)
0.1246

mix SFFloat with float

a = SFFloat(0.00123, 3)
print(a + 12.3456789)
12.34691

use SFFloat standard math functions

from math import pi
from sffloat import sfsin as sin

angle = SFFloat(pi / 4, 3)
print(f"The angle is {angle}; its raw value is {angle.value}")
print(f"The sine of {angle} is {sin(angle)}; its raw value is {sin(angle).value}")
The angle is 0.785; its raw value is 0.7853981633974483
The sine of 0.785 is 0.707; its raw value is 0.7071067811865475

supported functions

If you want to use one of the standard Python math functions while preserving precision then use any of the following replacement functions. You can either import and use as-is, or import and assign the standard function name:

from sffloat import sfsin

or

from sffloat import sfsin as sin
  • sfsin replaces sin
  • sfcos replaces cos
  • sftan replaces tan
  • sflog replaces log
  • sflog10 replaces log10
  • sfasin replaces asin
  • sfacos replaces acos
  • sfatan replaces atan
  • sfatan2 replaces atan2
  • sfexp replaces exp
  • sfpow replaces pow
  • sfsqrt replaces sqrt
  • sfdegrees replaces degrees
  • sfradians replaces radians

notes on functionality

  • Operation with standard functions is not sophisticated and follows the same rules that are used for ordinary multiplication (minimum significant figures propagate as-is).
  • When ordinary floating point numbers are used with SFFloat values, the floating point values are considered to have unlimited precision.
  • Addition or subtraction operations on SFFloat values may result in "zero" or "negative" significant figures.
  • "zero" sig fig values will continue to be used correctly for subsequent addition or subtraction operations.
  • "zero" sig fig values will produce an exception when used in a function or multiplication operation.
  • "zero" sig fig values will be displayed as '0'.
  • The underlying full-precision floating point value of a SFFloat object may be accessed with its .value property.
  • The underlying significant figures of a SFFloat object may be accessed with its .sigfigs property.
  • sffloat does not attempt to infer precision from initializing value (at the present time).
  • SFFloat objects display in rounded form using the sigfig.round function.
  • SFFloat objects display in standard format for values between 0.001 and 1000, and for 0, otherwise scientific notation is used.

installation

The best way to install sffloat is with pip and virtualenv. Create and activate your virtual environment then install sffloat with:

pip install sffloat

requirements

sffloat depends on sigfig, which will be installed automatically when using pip.

development environment

To begin working with sffloat in a development environment:

  • Clone this repository and cd into it.
  • Create a virtual environment: python3 -m venv ./env
  • Activate the virtual environment: source ./env/bin/activate
  • Install the dependencies: pip install -r requirements.txt

The ./scripts folder includes a run_tests.sh script that will:

  • Perform a style check using black.
  • Perform a pylint check.
  • Execute the test cases using nose.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sffloat-0.1.3.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

sffloat-0.1.3-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file sffloat-0.1.3.tar.gz.

File metadata

  • Download URL: sffloat-0.1.3.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.7.0

File hashes

Hashes for sffloat-0.1.3.tar.gz
Algorithm Hash digest
SHA256 70e9b03a1692ace10c7c1f5edc50581bf49b63007d147525bfdf2c6df9d4d16c
MD5 8aea797174423922cfbc41ebe6f6084d
BLAKE2b-256 2571d130fa0afd285fffc5f81e9d044e34fae51399062214db804f4a4a05f542

See more details on using hashes here.

File details

Details for the file sffloat-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: sffloat-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.7.0

File hashes

Hashes for sffloat-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cb24b1052b8e35d8e5105913a8c6a92bfa7cf311971c777f251da2ed00cf02f4
MD5 3655ed5809d1c195904908f0c73571de
BLAKE2b-256 de84088bb41af2e5913347121bd4a4e9a8750df0b9f3e58814ac77ca76869901

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