Package org.jscience.physics.amount

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:[code] Amount carMileage = Amount.valueOf(20, MILE.divide(GALLON_LIQUID_US)); Amount gazPrice = Amount.valueOf(1.2, EUR.divide(LITER)); // 1.2 €/L [/code] 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:[code] // No problem here as the measure type is infered from the unit. Amount tripDistance = Amount.valueOf(400, KILO(SI.METRE)); // Warning as the measure type is lost during calculation. Amount tripCost = tripDistance.divide(carMileage).times(gazPrice); // Better: Dimension check and no warning (USD is a Currency (Unit)) Amount tripCost = tripDistance.divide(carMileage).times(gazPrice).to(USD); [/code] 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:[code] import static org.jscience.physics.units.SI.*; ... Amount x = Amount.valueOf(1.0, METRE); Amount v = Amount.valueOf(0.01, METRE_PER_SECOND); Amount 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.[/code]

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:[code] RelativisticModel.select(); // Selects a relativistic model. Amount x = Amount.valueOf(100, NonSI.INCH); x = x.plus(Amount.valueOf("2.3 µs")).to(METRE); // Length and Duration can be added. Amount 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[/code]

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).
  • Class Summary 
    Class Description
    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 
    Exception Description
    AmountException
    Signals that an illegal measure operation has been performed.