Skip to content

Arithmetic Rules for Difference vs Absolute Quantities (DRAFT)

Andi Huber edited this page Dec 1, 2018 · 37 revisions

Note: This draft was superseded by this Wiki Page.

The content of this page can be safely removed.


Exemplified with Temperature but applies more generally.

ArithmeticCharacteristic

Assuming ArithmeticCharacteristic(UNIFIED, ABSOLUTE, DIFFERENCE) being a property of Quantity.

public enum ArithmeticCharacteristic {

  /** No arithmetic distinction is made between absolute or difference on quantities 
   * having UNIFIED characteristic, because the physical interpretation is agnostic 
   * to such a distinction. Eg. any quantity of unit KELVIN
   */
  UNIFIED,
  
  /** Special arithmetic rules apply to quantities having ABSOLUTE characteristic, 
   * because the physical interpretation does require to distinguish between 
   * ABSOLUTE vs. DIFFERENCE. <p>
   * eg. an absolute quantity of unit CELSIUS, where 0°C is equivalent 273.15 K
   */
  ABSOLUTE,
  
  /** Special arithmetic rules apply to quantities having DIFFERENCE characteristic, 
   * because the physical interpretation does require to distinguish between 
   * ABSOLUTE vs. DIFFERENCE. <p>
   * eg. a difference quantity of unit CELSIUS, where 0°C is equivalent 0 K
   */
  DIFFERENCE
}

ArithmeticPolicy

Assuming ArithmeticPolicy(UNIFIED_ONLY, ABSOLUTE_OR_DIFFERENCE) being a property of Unit.

public enum ArithmeticPolicy{

  /** Quantities of Units with policy UNIFIED_ONLY can only have UNIFIED 
   * characteristic */
  UNIFIED_ONLY,

  /** Quantities of Units with policy ABSOLUTE_OR_DIFFERENCE can either have 
   * ABSOLUTE or DIFFERENCE characteristic */
  ABSOLUTE_OR_DIFFERENCE
}

Let

  • scalar ... a number or dimensionless quantity
  • A ... absolute Quantity type, exemplified with CELSIUS, unit °C_abs
  • D ... difference Quantity type, exemplified with CELSIUS, unit °C_dif
  • U ... Quantity type with 'unified' arithmetic characteristic exemplified with KELVIN

Quantity Conversion (pseudo-code)

  • toUnified(A) -> U ... toUnified( 0°C_abs ) = 273.15 K
  • toUnified(D) -> U ... toUnified( 1°C_dif ) = 1 K

Quantity Operations

Assuming commutativity of addition and multiplication, and let X - Y === X + (-1 * Y)

A ± A -> A ... 0°C_abs + 0°C_abs = 273.15°C_abs
A ± D -> A ... 2°C_abs + 3°C_dif = 5°C_abs
D ± D -> D ... 2°C_dif + 3°C_dif = 5°C_dif

U ± U -> U ... 1K + 2K = 3K
A ± U -> U ... 0°C_abs + 0K = 273.15K
     Note: breaks with the convention, that first operand gives the result's unit
D ± U -> U ... 0°C_dif + 0K = 0K
     Note: breaks with the convention, that first operand gives the result's unit

A * scalar -> A ... 0°C_abs * 2 = 273.15°C_abs
D * scalar -> D ... 5°C_dif * 2 = 10°C_dif
U * scalar -> U ... 5K * 2 = 10K

A * A -> yields unit K² ... by converting operands to Unified = Kelvin
D * D -> yields unit K² ... by converting operands to Unified = Kelvin
A * D -> yields unit K² ... by converting operands to Unified = Kelvin

A * U -> yields unit K² ... by converting operands to Unified = Kelvin
D * U -> yields unit K² ... by converting operands to Unified = Kelvin
U * U -> yields unit K² ... 5K * 2K = 10K²

D / D -> scalar ... 10°C_dif / 2°C_dif = 5
     Note: same result as if by converting operands to Unified
U / U -> scalar ... 10K / 2K = 5

A / A -> scalar ... by converting operands to Unified = Kelvin
A / D -> scalar ... by converting operands to Unified = Kelvin
D / A -> scalar ... by converting operands to Unified = Kelvin
A / U -> scalar ... by converting operands to Unified = Kelvin
D / U -> scalar ... by converting operands to Unified = Kelvin
U / D -> scalar ... by converting operands to Unified = Kelvin
U / A -> scalar ... by converting operands to Unified = Kelvin

1 / A -> yields unit 1/K ... by converting operand to Unified = Kelvin
     Note: is consistent with A/A, D/A and U/A
1 / D -> yields unit 1/K ... by converting operand to Unified = Kelvin
     Note: needs to be consistent with A/D, D/D and U/D (which it is, I guess)
1 / U -> yields unit 1/K

API Extension: Difference Operator

For convenience and usability a new difference operator (other than Quantity#subtract) is required to allow for the intended use-case of generating a DIFFERENCE quantity from 2 ABSOLUTE quantities:
A - A -> D ... 5°C_abs - 4°C_abs = 1°C_dif
     Note: new API !

Units and Characteristics

Class Unit System Unit Arithmetic-Policy
Dimensionless AbstractUnit.ONE YES UNIFIED_ONLY
ElectricCurrent AMPERE YES UNIFIED_ONLY
LuminousIntensity CANDELA YES ???
Temperature KELVIN YES UNIFIED_ONLY
Temperature CELSIUS NO ABSOLUTE_OR_DIFFERENCE
Mass KILOGRAM YES UNIFIED_ONLY
Length METRE YES UNIFIED_ONLY
AmountOfSubstance MOLE YES UNIFIED_ONLY
Time SECOND YES UNIFIED_ONLY
Angle RADIAN YES UNIFIED_ONLY
SolidAngle STERADIAN YES UNIFIED_ONLY
Frequency HERTZ YES UNIFIED_ONLY
Force NEWTON YES UNIFIED_ONLY
Pressure PASCAL YES UNIFIED_ONLY
Energy JOULE YES UNIFIED_ONLY
Power WATT YES UNIFIED_ONLY
ElectricCharge COULOMB YES UNIFIED_ONLY
ElectricPotential VOLT YES UNIFIED_ONLY
ElectricCapacitance FARAD YES UNIFIED_ONLY
ElectricResistance OHM YES UNIFIED_ONLY
ElectricConductance SIEMENS YES UNIFIED_ONLY
MagneticFlux WEBER YES UNIFIED_ONLY
MagneticFluxDensity TESLA YES UNIFIED_ONLY
ElectricInductance HENRY YES UNIFIED_ONLY
LuminousFlux LUMEN YES ???
Illuminance LUX YES ???
Radioactivity BECQUEREL YES ???
RadiationDoseAbsorbed GRAY YES ???
RadiationDoseEffective SIEVERT YES ???
CatalyticActivity KATAL YES ???
Speed METRE_PER_SECOND YES UNIFIED_ONLY
Acceleration METRE_PER_SQUARE_SECOND YES UNIFIED_ONLY
Area SQUARE_METRE YES UNIFIED_ONLY
Volume CUBIC_METRE YES UNIFIED_ONLY

API Extension: Quantity Creation 1

It would be desirable to extend the programming model, such that we can enforce this policy at compile-time, but this example does not:

  1. For Unit with policy UNIFIED_ONLY use:
    Quantities.getQuantity(Number, Unit) ... a Quantity w/ characteristic UNIFIED

  2. For Unit with policy ABSOLUTE_OR_DIFFERENCE use:
    Quantities.getQuantity(Number, Unit, ArithmeticCharacteristic) ... Quantity w/ either ABS. or DIF. characteristic

Examples utilizing checks at run-time

Quantities.getQuantity(5, KELVIN) ... OK
Quantities.getQuantity(5, KELVIN, ABSOLUTE) ... IllegalArgumentException or OK ???
Quantities.getQuantity(5, KELVIN, DIFFERENCE) ... IllegalArgumentException or OK ???

Quantities.getQuantity(5, CELSIUS) ... IllegalArgumentException
Quantities.getQuantity(5, CELSIUS, ABSOLUTE) ... OK
Quantities.getQuantity(5, CELSIUS, DIFFERENCE) ... OK

API Extension: Quantity Creation 2

Variant to 'Quantity Creation 1' w/o exposing ArithmeticCharacteristic to the public API

  1. For Unit with policy UNIFIED_ONLY use:
    Quantities.getQuantity(Number, Unit) ... a Quantity w/ characteristic UNIFIED

  2. For Unit with policy ABSOLUTE_OR_DIFFERENCE use:
    Quantities.getAbsoluteQuantity(Number, Unit) ... Quantity w/ ABSOLUTE characteristic Quantities.getDifferenceQuantity(Number, Unit) ... Quantity w/ DIFFERENCE characteristic

Examples utilizing checks at run-time

Quantities.getQuantity(5, KELVIN) ... OK
Quantities.getAbsoluteQuantity(5, KELVIN) ... IllegalArgumentException or OK ??? Quantities.getDifferenceQuantity(5, KELVIN) ... IllegalArgumentException or OK ???

Quantities.getQuantity(5, CELSIUS) ... IllegalArgumentException
Quantities.getAbsoluteQuantity(5, CELSIUS) ... OK
Quantities.getDifferenceQuantity(5, CELSIUS) ... OK