unitsofmeasurement / indriya

JSR 385 - Reference Implementation
Other
122 stars 42 forks source link

Surprising parse behavior of quotient units. #249

Closed wnreynoldsWork closed 5 years ago

wnreynoldsWork commented 5 years ago

I would expect the unit string 'm/s/s' to have dimensions L/T^2, but instead it has dimension L. I understand that there is ambiguity in the order of evaluation of the '/' operators. If we evaluate the '/' right to left, then s/s = 1, and m/1 = m, with dimensions L, whereas if we evaluate the '/' left to right, then we have (m/s)/s = m/s^2, with dimensions L/T^2.

I admit that there is a formal problem with m/s/s, but I am (very) certain that every scientist and engineer who sees (or enters into software) the string 'm/s/s', would expect the resulting unit to have dimensions L/T^2. I request that the quotient operators be evaluated left to right, thus preserving the principle of least surprise.

keilw commented 5 years ago

Which UnitFormat implementation did you use?

wnreynoldsWork commented 5 years ago

SimpleUnitFormat

keilw commented 5 years ago

Did you try others like EBNFUnitFormat? As the name suggests the SimpleUnitFormat is a slightly simplified implementation while several others apply the Extended Backus-Naur Form.

wnreynoldsWork commented 5 years ago

Simple class demonstrating behavior. Output is '[L]'l instead of expected '[L]/[T]^2'

`import tech.units.indriya.format.SimpleUnitFormat;

/**

}`

wnreynoldsWork commented 5 years ago

EBNFFormat works, but it does not have the 'alias' capability of SimpleUnitFormat, which is critical for my application.

`import tech.units.indriya.format.EBNFUnitFormat;

/**

} `

wnreynoldsWork commented 5 years ago

Ok, based on your comments, I've figured out how to do aliases using EBNFFormat:

`import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.util.Locale; import java.util.ResourceBundle;

import tech.units.indriya.format.EBNFUnitFormat; import tech.units.indriya.format.SymbolMap; import tech.units.indriya.unit.Units;

/**

} `

Thank you. It turns out that this leads to a new problem, EBNFUnitParser won't accept a unit alias called 'e'. I'll post a second report on this problem.

wnreynoldsWork commented 5 years ago

see issue #250 for the problem with using the alias 'e' in the EBNFUnitParser.

keilw commented 5 years ago

If you're happy to use the mentioned demo as a basis for demos in https://github.com/unitsofmeasurement/uom-demos/tree/master/console/basic/src/main/java/tech/uom/demo/format, feel free to propose a PR or we could also do that. Can this ticket otherwise be closed? I saw the other one, similar to using "1" as a unit it may be a bit trickier because "e" is a regular part of very large or small numbers.

wnreynoldsWork commented 5 years ago

Go ahead and close the ticket. I'm not quite understanding the question about a PR - do you mean add the simple demo I posted above as an addition to the demo/format directory as a PR? If that's the question, I'd be happy to do it, otherwise let me know what you'd like.

keilw commented 5 years ago

Yes, uom-demos, but we can do that if you don't. Unlike the main RI code here everyone is free to participate in the demos and other projects. Only the parts that make up the JSR (API, RI and TCK) are a little more restricted for contribution without contributor agreements because of JCP requirements.