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

[question] making a unitless Quantity #1333

Closed
teauxfu opened this issue Jun 16, 2021 · 3 comments
Closed

[question] making a unitless Quantity #1333

teauxfu opened this issue Jun 16, 2021 · 3 comments

Comments

@teauxfu
Copy link
Contributor

teauxfu commented Jun 16, 2021

how does one define or use a unitless instance of Quantity?
as distinct from a Quantity of unit 'dimensionless' which is returned when no unit is specified

assert Q_('1').units == Q_(1, None).units == ureg.dimensionless

the docs don't seem to mention their existence

I see that Quantity objects have a unitless property

pint/pint/quantity.py

Lines 461 to 464 in 9fc752c

@property
def unitless(self):
""" """
return not bool(self.to_root_units()._units)

but it seems only to be used within Quantity here in to_compact

pint/pint/quantity.py

Lines 760 to 766 in 9fc752c

if (
self.unitless
or self.magnitude == 0
or math.isnan(self.magnitude)
or math.isinf(self.magnitude)
):
return self

and here

pint/pint/quantity.py

Lines 1789 to 1791 in 9fc752c

def __ito_if_needed(self, to_units):
if self.unitless and to_units == "radian":
return

but after glancing about it's not apparent (to me) how one would even instantiate a unitless quantity, or otherwise what they'd be used for internally

I haven't yet fully inspected the UnitsContainer and its usage/references;
is the unitless property used when working with those?

possibly related #1277 regarding adding unitless dimensions

@keewis
Copy link
Contributor

keewis commented Jun 16, 2021

Could you explain what exactly you're seeing? ureg("1") returns a standard python int of value 1 for me, causing your assert line to fail with an AttributeError.

@teauxfu
Copy link
Contributor Author

teauxfu commented Jun 16, 2021

Could you explain what exactly you're seeing? ureg("1") returns a standard python int of value 1 for me, causing your assert line to fail with an AttributeError.

you're right! my bad -- I'm getting used to the differences between using Q_ vs ureg when making Quantitys

what I meant to comment was

assert Q_('1').units == ureg.dimensionless

to indicate how lack-of-unit is parsed as 'dimensionless' and not 'unitless'
similarly

assert Q_(1, None).units == ureg.dimensionless

@hgrecco
Copy link
Owner

hgrecco commented Jul 8, 2021

I do not follow. Q_('1').units == Q_(1, None).units. If you want somehing without a units fields, then you want a numeric object not a Quantity.

@teauxfu teauxfu closed this as completed Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants