Skip to main content

A Python package that detects and rounds numbers to significant digits.

Project description

esoteric-sigdigs

🐍 A Python package that detects and rounds numbers to significant digits.

This package aims to provide easy-to-use functions to work with significant digits (also known as significant figures).

⚠ Please note that this package is almost certainly laced with bugs, so use it at your own risk! Additionally, the significant digit rules used may not be exactly what you are looking for; I have designed this package to fit the likely-esoteric way I have been taught to use significant digits. Sorry! If you encounter any issues, please create an issue on GitHub (https://github.com/ericli3690/esoteric-sigdigs) or email me at ericli3690@gmail.com.

🛠 Installation and Use

>> pip install esoteric-sigdigs

from sigdigs import function_name_here
function_name_here(arguments_here)

🧪 What is Scientific Notation?

Scientific notation is often used by this package, a string formatting of numbers which takes the following form:

a.bcdEf

And which can be written in standard formatting as

a.bcd * 10^f

It can be seen that f represents the exponent.

Here are some more examples, shown in scientific notation while pretending that trailing zeros are NOT significant (this is not how functions like standard_to_scientific in this package actually behave!):

Standard Formatting Scientific Notation
123456 1.23456E5
120000 1.2E5
-0.0000056 -5.6E-6
10 1E1
8 8E0
100000000000 1E11

⚙ Functionality

🟥 sigdigs(*vals:float) -> int

Returns the number of significant digits of a provided float.

Given a list of numbers, it returns the lowest significant digits number provided by any of the floats in the list.

Trailing zeros are considered significant.

Zeros are considered to have one significant digit.

Examples:

Input Output
123.456 6
100.0 4
0.00056 2
-34 2
4000 4
1 1
0 1
0.000 1
123.456, 100.0, 0.00056, -34, 4000 2

🟨 round_to_sigdigs(val:float, sig:int) -> str

Rounds a float to an integer number of significant digits.

Always returns answers in scientific notation.

Examples:

Value Sigdigs Output
3.255 5 3.2550E0
-8.33 1 -8E0
0.0056 1 6E-3
1234.5678 2 1.2E3
0 4 0E0
0.000 9 0E0

🟩 scientific_to_standard(val:str) -> str:

Converts a number in scientific notation to normal formatting, IF POSSIBLE.

Sometimes, converting to normal formatting would involve adding extra significant digits (ex. 1.2E3 cannot be rewritten as 1200, as this would involve adding two new significant digits).

In these cases, this function will simply return the input.

Examples:

Scientific Notation Standard Formatting
1.23456E5 123456
1.2E5 1.2E5
-5.6E-6 -0.0000056
1.0E1 10
8E0 8
1E11 1E11
0E0 0

🟩 standard_to_scientific(val:float) -> str:

The inverse of the above operation!

Converts a float into scientific form.

Examples:

Standard Formatting Scientific Notation
123456 1.23456E5
120000 1.20000E5
-0.0000056 -5.6E-6
10 1.0E1
8 8E0
100000000000 1.00000000000E11
0 0E0

🟦 round_to_sigdigs_and_standard_form_given_source(val:float, sigsource:list) -> str:

This is a common routine that I personally find useful.

Imagine a calculation being performed...

V = lwh

l = 50.0
w = 0.02345
h = 10

V = 11.725... but to how many significant digits?

This routine uses scientific_to_standard, round_to_sigdigs, and sigdigs to solve this efficiently.

For example, for this question, to obtain V with proper significant digits:

round_to_sigdigs_and_standard_form_given_source(50.0 * 0.02345 * 10, [50.0, 0.02345, 10])

Which outputs 12, as expected.

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

esoteric-sigdigs-0.1.5.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

esoteric_sigdigs-0.1.5-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file esoteric-sigdigs-0.1.5.tar.gz.

File metadata

  • Download URL: esoteric-sigdigs-0.1.5.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.1

File hashes

Hashes for esoteric-sigdigs-0.1.5.tar.gz
Algorithm Hash digest
SHA256 0426eba08c2fff2d4f3aff4876da341c3b270fe621abe2376e7cf66227e61ed1
MD5 59e1bf448ac434940a4ffa377ff03fe9
BLAKE2b-256 e21e1897805acf354fd920f81aeb7d1df7e980d65782353f8fdf530d8c700f38

See more details on using hashes here.

File details

Details for the file esoteric_sigdigs-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for esoteric_sigdigs-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 af7ec94aaa079d25c9f6a242255432767ec4f7713b890d47b36308e2ab277782
MD5 eb5b0fe1e00797400edd99585191da68
BLAKE2b-256 c9e195f3da1b4c5786b1bf3a0282dc7a460e5de28fb2414b8268aa34da2b5099

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