Package: sffloat
sffloatThesffloatpackage defines aSFFloatclass that allows easy floating point computations
that take precision or "significant figures" into account.featuresSeamlessly mixfloatandSFFloatobjects in arithmetic. Standard floating point objects
are automatically promoted toSFFloatwhen necessary.Print and displaySFFloatobjects as if they are standardfloatnumbers.CompareSFFloatobjects with floating point numbers to determine equivalence, taking precision
into account.UseSFFloatversions of all standard Python math functions.example useimporting SFFloatfromsffloatimportSFFloatcalculating circle area using an imprecise radius# Create a floating point radius value of 3.4 with 2 sig figsfrommathimportpiradius=SFFloat(3.4,2)# Compute the circle area, with sig figsarea=pi*radius**2print(area)36addition examplea=SFFloat(0.00123,3)b=SFFloat(0.1234,4)print(a+b)0.1246mixSFFloatwithfloata=SFFloat(0.00123,3)print(a+12.3456789)12.34691useSFFloatstandard math functionsfrommathimportpifromsffloatimportsfsinassinangle=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.7071067811865475supported functionsIf 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:fromsffloatimportsfsinorfromsffloatimportsfsinassinsfsin replaces sinsfcos replaces cossftan replaces tansflog replaces logsflog10 replaces log10sfasin replaces asinsfacos replaces acossfatan replaces atansfatan2 replaces atan2sfexp replaces expsfpow replaces powsfsqrt replaces sqrtsfdegrees replaces degreessfradians replaces radiansnotes on functionalityOperation 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 withSFFloatvalues, the floating point values are
considered to have unlimited precision.Addition or subtraction operations onSFFloatvalues 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 aSFFloatobject may be accessed with its.valueproperty.The underlying significant figures of aSFFloatobject may be accessed with its.sigfigsproperty.sffloatdoes not attempt to infer precision from initializing value (at the present time).SFFloatobjects display in rounded form using thesigfig.roundfunction.SFFloatobjects display in standard format for values between 0.001 and 1000, and for 0, otherwise
scientific notation is used.installationThe best way to installsffloatis with pip and virtualenv. Create and activate your virtual environment then
installsffloatwith:pip install sffloatrequirementssffloatdepends onsigfig, which will be installed automatically when using pip.development environmentTo begin working withsffloatin a development environment:Clone this repository andcdinto it.Create a virtual environment:python3 -m venv ./envActivate the virtual environment:source ./env/bin/activateInstall the dependencies:pip install -r requirements.txtThe./scriptsfolder includes arun_tests.shscript that will:Perform a style check using black.Perform a pylint check.Execute the test cases using nose.