unit-api icon indicating copy to clipboard operation
unit-api copied to clipboard

Units can have non-integer Dimension

Open SebHess opened this issue 1 year ago • 4 comments

Actual physical units can have dimensions that are non-integer (most of the time square-root). This often happens with spectral densities (square root of Hertz), but also in many other domains.

This cannot be defined with the current Unit API because (1) Unit.getBaseDimensions() returns a Map<Unit, Integer>; (2) Dimsension.getBaseDimensions() returns a Map<Unit,Integer>; (3) Dimension.pow() and Unit.pow() accept only int as argument.

SebHess avatar Oct 27 '23 14:10 SebHess

Dimension.getBaseDimensions() returns a Map<Unit,Integer>; (3) Dimension.pow() and Unit.pow() accept only int as argument.

This is not really a problem because either Integer.intValue() or a cast between Integer and int should both work. @SebHess Was this the only concern, or are there significant examples for e.g. floating-point dimensions?

keilw avatar Nov 01 '23 20:11 keilw

This is not really a problem because either Integer.intValue() or a cast between Integer and int should both work. Was this the only concern, or are there significant examples for e.g. floating-point dimensions?

In my understanding, he wished that Map<Unit,Integer> would have been something like Map<Unit,Fraction> instead (Fraction does not exist in the JDK, I'm using that only for expressing the intend). The example given is spectral densities with units of s^−½.

Map<Unit,Integer> cannot be changed without compatibility break. Furthermore there is no clear alternative to Integer in the standard JDK: Fraction does not exist, and Float or Double may be too flexible. While this issue is not impossible to resolve, it is not an easy one neither.

desruisseaux avatar Nov 01 '23 21:11 desruisseaux

@desruisseaux Yes, that is exactly what I had in mind. Note that there are empirical laws in physics and chemisty that are expressed as an arbitrary (real) power of a quantity, thus implying real power of units.

An example is the electron emission from a metal by electric and thermal effects: if the metal is cold, the current is given by the Fowler-Nordheim law and implies fractional powers of the field, while for warmer metal, the empirical thermo-field law applies, with real powers of the temperature and field.

Thus for completeness, Integer should be replaced by either Float or Double.

It is true that this issue breaks the existing API, but one solution could be replacing Map<Unit, Integer> by Map<Unit, ? extends Number> ? This would be OK API-wise and should not break existing implementation, although it would allow them to evolve toward non-integer powers of unit.

SebHess avatar Nov 02 '23 07:11 SebHess