Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cli uncertainty package import #2032

Merged
2 changes: 1 addition & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Pint Changelog
0.25 (unreleased)
-----------------

- Nothing added yet.
- Fix the default behaviour for pint-convert (cli) for importing uncertainties package


0.24.1 (2024-06-24)
Expand Down
27 changes: 15 additions & 12 deletions docs/dev/pint-convert.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,39 @@ With the `uncertainties` package, the experimental uncertainty in the physical
constants is considered, and the result is given in compact notation, with the
uncertainty in the last figures in parentheses:

The uncertainty can be enabled with `-U` (by default it is not enabled):

.. code-block:: console

$ pint-convert Eh eV
$ pint-convert -p 20 -U Eh eV
1 hartree = 27.211386245988471444 eV

.. code-block:: console

$ pint-convert -U Eh eV
1 hartree = 27.21138624599(5) eV

The precision is limited by both the maximum number of significant digits (`-p`)
and the maximum number of uncertainty digits (`-u`, 2 by default)::

$ pint-convert -p 20 Eh eV
$ pint-convert -U -p 20 Eh eV
1 hartree = 27.211386245988(52) eV

$ pint-convert -p 20 -u 4 Eh eV
$ pint-convert -U -p 20 -u 4 Eh eV
1 hartree = 27.21138624598847(5207) eV

The uncertainty can be disabled with `-U`):

.. code-block:: console

$ pint-convert -p 20 -U Eh eV
1 hartree = 27.211386245988471444 eV

Correlations between experimental constants are also known, and taken into
account. Use `-C` to disable it:
account if uncertainties `-U` is enabled. Use `-C` to disable it:

.. code-block:: console

$ pint-convert --sys atomic m_p
1 proton_mass = 1836.15267344 m_e

$ pint-convert -U --sys atomic m_p
1 proton_mass = 1836.15267344(11) m_e

$ pint-convert --sys atomic -C m_p
$ pint-convert -U --sys atomic -C m_p
1 proton_mass = 1836.15267344(79) m_e

Again, note that results may differ slightly, usually in the last figure, from
Expand Down
10 changes: 6 additions & 4 deletions pint/pint_convert.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
)
parser.add_argument(
"-U",
"--no-unc",
"--with-unc",
dest="unc",
action="store_false",
help="ignore uncertainties in constants",
action="store_true",
help="consider uncertainties in constants",
)
parser.add_argument(
"-C",
Expand Down Expand Up @@ -160,6 +160,7 @@ def _set(key: str, value):


def convert(u_from, u_to=None, unc=None, factor=None):
prec_unc = 0
q = ureg.Quantity(u_from)
fmt = f".{args.prec}g"
if unc:
Expand All @@ -171,7 +172,8 @@ def convert(u_from, u_to=None, unc=None, factor=None):
if factor:
q *= ureg.Quantity(factor)
nq *= ureg.Quantity(factor).to_base_units()
prec_unc = use_unc(nq.magnitude, fmt, args.prec_unc)
if args.unc:
prec_unc = use_unc(nq.magnitude, fmt, args.prec_unc)
if prec_unc > 0:
fmt = f".{prec_unc}uS"
else:
Expand Down
Loading