org.jscience.physics.amount

Package org.jscience.physics.amount

Provides support for exact or arbitrary precision measurements.

See: Description

Package org.jscience.physics.amount Description

Provides support for exact or arbitrary precision measurements.

Amount as measurable quantity:

The Amount base class is parameterized with the quantity onto which the measure applies (Amount<Q extends Quantity>). If the quantity is not known then <?> can be used. For example:
    Amount<?> carMileage = Amount.valueOf(20, MILE.divide(GALLON_LIQUID_US));    Amount<?> gazPrice   = Amount.valueOf(1.2, EUR.divide(LITER)); // 1.2 \xe2\x82\xac/L
If the expected quantity result for a measure is known but has been lostdue to the calculations performed; better than using <?>, the measure canbe stated in a known unit for this quantity. It ensures dynamic check ofthe measure dimension/unit and avoid compiler warnings. For example:
    // No problem here as the measure type is infered from the unit.    Amount<Length> tripDistance = Amount.valueOf(400, KILO(SI.METRE));    // Warning as the measure type is lost during calculation.    Amount<Money> tripCost = tripDistance.divide(carMileage).times(gazPrice);    // Better: Dimension check and no warning (USD is a Currency (Unit<Money>))    Amount<Money> tripCost = tripDistance.divide(carMileage).times(gazPrice).to(USD);
It should be noted that this conversion is immediate if the specified unit is the actual unit for the amount (which is then returned unchanged).

Error calculations:

Amount take into account measurement and calculation errors. For example:
    import static org.jscience.physics.units.SI.*;    ...    Amount<Length> x = Amount.valueOf(1.0, METRE);    Amount<Velocity> v = Amount.valueOf(0.01, METRE_PER_SECOND);    Amount<Duration> t = Amount.valueOf(1.0, MICRO(SECOND));    for (int i = 0; i < 10000000; i++) {        x = x.plus(v.times(t));    }    AmountFormat.setInstance(AmountFormat.getExactDigitsInstance());    System.out.println(x);    > 1.10000000 m    The exact value is guaranteed to be in the range: ]1.09999999 m, 1.10000001 m[            The same calculation using primitive double type would have display:    > 1.099999999392253    with no idea on the accuracy of the result.

Dimension checking.

The unit of an amount determinates its type.For example, Amount.valueOf("1 \xc2\xb5m") and Amount.valueOf("1.2 ft")are Length amounts (both "\xc2\xb5m" and "ft" unitsare derived from SI.METRE). Multiple physical models are supported (e.g. Standard,Relativistic, High-Energy, Quantumand Natural). The physical model sets which conversions are allowed or disallowed.For example:
        RelativisticModel.select(); // Selects a relativistic model.        Amount<Length> x = Amount.valueOf(100, NonSI.INCH);        x = x.plus(Amount.valueOf("2.3 \xc2\xb5s")).to(METRE); // Length and Duration can be added.        Amount<Mass> m = Amount.valueOf("12 GeV").to(KILOGRAM); // Energy is compatible with mass (E=mc2)        System.out.println(x);         System.out.println(m);                 > (692.06265340000008 \xc2\xb1 5.1E-13) m        > (2.1391940763025056E-26 \xc2\xb1 4.3E-42) kg

Physical Constants

Finally, this package holds the latest physical constants measurementswith the best known accuracy (the more accurate the constant, the higher the precision of the calculationsmaking use of these constants).

SCaVis 1.7 © jWork.org