I'm using the reference implementation of JSR363. I've tried many variations of this but I'll give this code as an example.
ServiceProvider provider = ServiceProvider.current();
QuantityFactory<Length> lengthFactory = provider.getQuantityFactory(Length.class);
Quantity<Length> first = lengthFactory.create(5, Units.METRE.divide(100.0));
Quantity<Length> second = lengthFactory.create(3, Units.METRE);
System.out.println(second.add(first));
This prints 503.0 m. Clearly something is very wrong and this should be 3.05 m. I find it very hard to believe that this is actually a bug with the library and I'm hoping someone can tell me what I'm missing.
After looking into this a bit I've been able to reproduce the oddities in question. It appears that using the
multiply()
or thedivide()
methods when passing aUnit
into a QuantityFactory has strange effects. In example:outputs the following:
20.5 dm
. Even when usingMetricPrefix
, which seems to be the default method of setting non base SI units, it seems to generate extremely inaccurateUnits
. Using the following:outputs
10020.0 km
which is nowhere near accurate. However, the following:outputs
12.0 m
, which is obviously the correct answer.In honesty the best solution is to simply not use those operations in the creation of the
Quantity
and to convert to other units of measurement using the built ingetConverter()
ofMetricPrefix
enums.The better way to create
Quantities
is to useQuantities.getQuantities()