Documentation of 'org.jscience.physics.amount.package-summary' Java class
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 €/L
If the expected quantity result for a measure is known but has been lost due to the calculations performed; better than using <?>, the measure can be stated in a known unit for this quantity. It ensures dynamic check of the 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 µm") and Amount.valueOf("1.2 ft") are Length amounts (both "µm" and "ft" units are derived from SI.METRE). Multiple physical models are supported (e.g. Standard, Relativistic, High-Energy, Quantum and 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 µs")).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 ± 5.1E-13) m
        > (2.1391940763025056E-26 ± 4.3E-42) kg

Physical Constants

Finally, this package holds the latest physical constants measurements with the best known accuracy (the more accurate the constant, the higher the precision of the calculations making use of these constants).

DMelt 3.0 © DataMelt by jWork.ORG

You see the box below because you did not login.