Unit Of Measures API (JSR-363) - Floating Point Errors

246 views Asked by At

We are using tec.units.unit-ri as an implementation of the JSR-363, javax.measure.unit-api.

We are using several weight-, length- and temperature- types in our application and we decided to use BigDecimal to store them. While doing some conversions, I realized floating point errors were appearing in our instances.

Unit<Length> metres = Units.METRE;
Unit<Length> centimetres = Units.METRE.divide(100);
BigDecimal valueInCentimetres = new BigDecimal("10.1");
Quantity<Length> valueInMetres = Quantities.getQuantity(valueInCentimetres, centimetres).to(metres) ;

If I run this code, the variable valueInMetres contains a Double representation with a floating point error, 0.10099999999999999 m. The implementation uses double values internally, and loses precision while converting values. We would need to round/introduce a scale to the value, which is not ideal.

Is there anything obvious I am doing wrong here? Have I missed some way of using the API which enables me to keep precision, or are all conversions routed over Double values and will lose precision, no matter what I do?

1

There are 1 answers

0
javahippie On BEST ANSWER

Searching further after asking this question, I realized that we used the wrong library for our application. unit-ri is meant for Java ME and uses double internally, uom-se is meant for Java SE and uses BigDecimal. This solves our problem.