Skip to main content

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.

Metadata

Release files for sffloat 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sffloat 0.2.0
File Size Uploaded Provenance
sffloat-0.2.0.tar.gz 10.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sffloat 0.2.0
File Interpreter ABI Platform
sffloat-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.1 kB

Release files / sffloat-0.2.0.tar.gz

Download URL sffloat-0.2.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
66945fd4c19ee24c5584408b206064f130d4dd6346a0e6c05bb0c9328cd7eb60
BLAKE2b-256 checksum
How to use checksums
cef85f2f242935706806c4f136cd132d35d25cf3b437986f45ddf3de98326234
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.12.1.2 requests/2.32.3 setuptools/76.1.0 requests-toolbelt/1.0.0 tqdm/4.67.1 CPython/3.12.3

Release files / sffloat-0.2.0-py3-none-any.whl

Download URL sffloat-0.2.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
79256a2a2ac3cc7e930d20c2e8a7cb917e3bfbd3bf993d246fd2019f5911ab1f
BLAKE2b-256 checksum
How to use checksums
1b657b5732048feb2252ce3c11abe8dcc20f99525a9af3a1094a01c18918fadf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.12.1.2 requests/2.32.3 setuptools/76.1.0 requests-toolbelt/1.0.0 tqdm/4.67.1 CPython/3.12.3

Security for sffloat 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page