how to i get ratio?

13 views
Skip to first unread message

Michael Lambert

unread,
Aug 9, 2011, 10:27:57 AM8/9/11
to units-users
I am really struggling with the units of measure api. i have a
quantity object and i need to extract out
a comparative ration from the two units:

Quantity<Length> foot = getLength(1, FOOT);

Quantity<Length> inches = a.getLength(24, INCHES);

double ratio = foot.divide(inches);

assert(ratio == 2);

How can I do this?

Werner Keil

unread,
Aug 16, 2011, 8:17:33 AM8/16/11
to units-users
Michael,

Thanks a lot for the question and your interest in Unit-API.

For this case, I suggest using a UnitConverter, which the unit also
provides methods for.

double ratio = INCH.getConverterTo(FOOT).convert(24);

Unit-API implementations like EclipseUOMo provide a slightly more
elegant abstraction.
There you can use base interface on top of Quantity called IMeasure.

Allowing you to do things like:
LengthAmount inches = new LengthAmount(24, INCH);
double ratio = inches.doubleValue(FOOT);

Or vice versa.

Using other methods of IMeasure you could directly do this:
LengthAmount foot = new LengthAmount(1, FOOT);
LengthAmount inches = new LengthAmount(24, INCH);
IMeasure<Length> = foot.divide(inches);

All above constants are part of UOMo and other implementations. So
unless you want to create your own implementation for some special
purpose (we'd love to hear about that if the case) you should be fine
using the unit systems provided by these implementations.

I hope that helped answer most of your questions. If you have any
further questions, please just ask here.

Kind Regards,
Werner

On Aug 9, 10:27 am, Michael Lambert <mich...@michaellambert.com>
wrote:

Werner Keil

unread,
Aug 17, 2011, 11:28:04 AM8/17/11
to units-users
The best and most precise conversion is this:
IMeasure<Length> ratio = inches.to(FOOT);

All others are also valid, but especially the double values involve
some level of rounding or precision loss (which is why using more
precise numbers like BigDecimal are highly encouraged), so e.g.
double ratio = INCH.getConverterTo(FOOT).convert(24);
will result in 1.9999999999999996 due to this precision loss. A proper
NumberFormat would sort that out, but if you want to be on the safe
side, please use to(targetUnit) instead.

HTH,
Werner

Michael Lambert

unread,
Aug 17, 2011, 12:03:26 PM8/17/11
to units...@googlegroups.com
But IMeasure isnt part of the unit of measure api. I would be exposing an implementation detail. Is the api incomplete?

Werner Keil

unread,
Aug 19, 2011, 2:15:09 AM8/19/11
to units-users
This was removed due to the history of JSR-275.
I won't go too much into detail, but basically JCP EC members,
especially Sun or Google couldn't or didn't want to agree on wording
for the interface then called Measurable.
So we dropped it from the Spec that was later converted into Unit-API.
Some new JCP members think, it's worth considering a new Unit JSR, so
Java doesn't lose momentum to F#, Scala, Fantom or many other new
languages, most of which have good unit support built in or easy to
add.

IMeasure was the Eclipse way of naming it. I mentioned the idea of
adding a new interface named Measurement into the API, but haven't got
enough feedback from other committers to do it now. The old Measure
type exists in ICU4J, another implementation detail of UOMo only those
may wish to use or expose, who need to be compatible with other
systems that use it, like Google Finance, etc.

I'll present UOMo either at EclipseCon Europe in Stuttgart or a local
DemoCamp in November.
Your name sounds German, so if you live in Germany, I'm here near
Frankfurt for some time. I'd be happy to meet other than at those
events if you like.

Cheers,
Werner

On Aug 17, 9:03 pm, Michael Lambert <mich...@michaellambert.com>
wrote:
> But IMeasure isnt part of the unit of measure api. I would be exposing an
Reply all
Reply to author
Forward
0 new messages