Skip to content

Clarify behavior of Quantity arithmetic on shifted units #95

@desruisseaux

Description

@desruisseaux

Clarify the behavior of Q₁ + Q₂ or Q * n (with n a unitless number) when the unit of measurement of Q, Q₁ or Q₂ is "shifted". Example of shifted units are:

  • Temperature in Celsius, with °C defined as K - 273.15 K.
  • Density sigma-t, with σT defined as ρ - 1000 kg/m³ (used in oceanography).
  • Other shifted units in other scientific domains?

Taking temperature in °C as an example, addition can happen in at least two different contexts:

  • T₁ + T₂ : there is some legitimate uses for such additions, but they are rare.
  • T + ΔT : a more common case.

In the T₁ + T₂ case, both quantities must be converted to K before addition. So 20°C + 10°C = 303.15°C because T₂ = 283.15 K. In the T + ΔT case, 10°C is an increment (ΔT). So in this later case 20°C + 10°C = 30°C.

In current API, there is no way to know if a number is an absolute quantity or an increment. So there is no way to know if we are in the T₁ + T₂ case or in the T + ΔT case. If we want to differentiate those quantities, we may need to:

  • Add a new method in UnitConverter: deltaConvert(double), which convert the given value without applying the offset. This is similar to deltaTransform in java.awt.geom.AffineTransform.
  • Introduce a new interface: Increment<Q extends Quantity<Q>>.
  • Defines arithmetic rules as below: in any arithmetic operation like M₁ + M₂ or M * n, implementation SHALL do the following steps at least conceptually (implementations are allowed to avoid conversions in some cases provided that the numerical result is the same):
    • All M values SHALL be converted to system units before addition or multiplication.
      • Conversions of Quantity instances shall be done with UnitConverter.convert(…).
      • Conversions of Increment instances shall be done with UnitConverter.deltaConvert(…).
    • Result type is defined by the following table:
      • Quantity + Quantity = Quantity
      • Quantity + Increment = Quantity
      • Increment + Quantity = Quantity
      • Increment + Increment = Increment
      • Quantity * n = Quantity
      • Increment * n = Increment
    • Result value is converted back to the unit of measurement of the first operand (M₁).
      • Using UnitConverter.convert(…) if the result type is Quantity.
      • Using UnitConverter.deltaConvert(…) if the result type is Increment.

Requires #98
Requires #99

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions