Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As @romainbrette noticed, the string representation of some units were not correct in the sense that evaluating them in Python did not result in the same unit – this is obviously bad, in particular when storing units/equations in text and importing them later (as Romain did). The problem concerned units raised to powers more than once, e.g.:
Due to Python's operator precedence, this will be interpreted as
second ** (2 ** -1)
, i.e.second ** 0.5
instead ofsecond ** -2
as it should be. The simple fix in this PR is to consider units raised to powers as "compound units", which will lead to the use of parentheses:Note that this changes the output also in situations that do not need correction, e.g.
amp / cm ** 2
will now be represented asamp / (cm ** 2)
. I'd rather keep the fix as simple as it is though, the new output might even be helpful to avoid confusion.While fixing this I came across another minor issue which is now fixed as well: the function
is_scalar_type
was supposed to returnTrue
for a scalar that "can be interpreted as a dimensionlessQuantity
", but it actually only checked if it was a scalar, not whether it was dimensionless. Since this function was used to check whether an exponent of a unit was useable, it did give a somewhat confusing error message for nonsensical operations likesecond**second
:With the fix, the error message is more meaningful