Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Types for Units of Measurement

Types for Units of Measurement

Basics of type for units of measurement.

Cranberries

April 14, 2020
Tweet

More Decks by Cranberries

Other Decks in Programming

Transcript

  1. Physical Quantity A physical quantity is a property of a

    material or system that can be quantified by measurement. A physical quantity can be expressed as the combination of a magnitude and a unit.
  2. SI The International System of Units 9th edition 2019 It

    is most widely used system of measurement.
  3. International System of Quantities Base quantity Symbol for dimension SI

    base unit SI unit symbol length L meter m mass M kilogram kg time T second s electric current I ampere A thermodynamic temperature Θ kelvin K amount of substance N mole mol luminous intensity J candela cd
  4. Vector space [1] [] [1] [] [ ∙ ] []

    [2] [] [] [] [] [] [] [−1]
  5. Dimensional Homogeneity • The most basic rule of dimensional analysis

    is dimensional homogeneity. • Only commensurable quantities (physical quantities having the same dimension) may be compared, equated, added, or subtracted. • In other words, any physically meaningful equation will have the same dimensions on its left and right sides. • This can be used to spot errors in formulae or calculations.
  6. However, the dimensions form an abelian group under multiplication, so:

    • One may take ratios of incommensurable quantities (quantities with different dimensions), and multiply or divide them. • 1 + 2 is meaningless. • However, 1 ÷ 2 is fine.
  7. An Abelian Group generated by MKS []: L:length, M:mass, T:time.

    , , ∈ ℕ. 100 = []: length 010 = []: mass 001 = []: time 10−1 = [/]: velocity
  8. In practice • This code calculates volume, but... • Correct

    code // calc volume of a box double volume = x * y; // ^~~~~~ Oh... // calc volume of a box double volume = x * y * z;
  9. Homogeneity checking at compile time • We already know type

    checking! • We need types to distinguish different quantities.
  10. Q. How to Type A. Phantom Types C++ example: •

    a is a quantity which has int value 2 with dim L. • length is a phantom type. • It allows us to distinguish for each quantities. • Type params that are used only at compile time are called phantom types. quantity<length, int> a = 2;
  11. In practice (again) • Even if you code a meaningless

    expression, you will get a compile error. • A diagnostic message will then be displayed. // calc volume of a box quantity<volume, double> volume = x * y; // ^~~~~~~~~~~~~~~ ERROR! // compiler will show you diagnostic msg // (The message will be buried in a super long type name, but...)
  12. Internal representation • Internal representation would be following form: Q.

    Is coefficient in ℕ? A. YES if only for four arithmetic operations, but NO if you want to allow power roots. quantity<force, double> quantity<((M,1),(L,1),(T,-2)), double>
  13. Conclusion • Units can be embedded in types as phantom

    types • This allows for sanity checks at compile time • If the sanity check fails, a compile error occurs • Get long error messages • These error messages are like your life • And nobody loves you
  14. References • https://www.bipm.org/utils/common/pdf/si-brochure/SI-Brochure- 9.pdf • https://en.wikipedia.org/wiki/Physical_quantity • https://en.wikipedia.org/wiki/Dimension • https://en.wikipedia.org/wiki/Dimensional_analysis

    • https://en.wikipedia.org/wiki/Homogeneity_(physics) • https://en.wikipedia.org/wiki/Finitely_generated_abelian_group • https://github.com/LoliGothick/mitama-dimensional