JScience v4.3

Package org.jscience.physics.amount

Provides support for exact or arbitrary precision measurements.

See:
          Description

Class Summary
Amount<Q extends Quantity> This class represents a determinate or estimated amount for which operations such as addition, subtraction, multiplication and division can be performed (it implements the Field interface).
AmountFormat This class provides the interface for formatting and parsing measures instances.
Constants This class provides most accurate physical constants measurement; the more accurate the constants, the higher the precision of the calculations making use of these constants.
 

Exception Summary
AmountException Signals that an illegal measure operation has been performed.
 

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).


JScience v4.3

Copyright © 2006 JScience.