unit-system
General Information
unit-system is a simple library to use physical units in code bases. At the moment the library exists for C++17 and Arduino C++.
The main project contains the code to generate the units for several programming languages. For better importing, each export target has its own repository with the generated sources. The following export targets exist:
- C++17: https://github.com/noah1510/unit-system (For compatibility reasons the c++ repo just has the library name)
- Arduino C++: https://github.com/noah1510/unit-system-adruino
If you want to port the library for a new programming language fell free to contribute to the main generator repository. Just open an issue with an example implementation for a few units to show how you expect them to work and show how the syntax works.
Available types:
All units are based in the SI system and all potential prefix/suffix implementations should only exist for SI-compatible types. The following units are implemented at the moment
- time_si -> time with seconds as base unit
- length -> length with meter as base unit
- mass -> mass with kg as base unit
- temperature -> temperature with K as base unit
- amount -> amount of substance with mole as base unit
- electric_current -> electric current with Ampere as base unit
- luminous_intensity -> luminous intensity with candela as base unit
- area -> area with m² as base unit
- speed -> speed with
m/s
as base unit - acceleration -> acceleration with
m/s²
as base unit - momentum -> momentum with
kg*m/s
as base unit - force -> force with
kg*m/s
² (Newton) as base unit - energy -> energy with
kg*m²/s²
(Joules) as base unit - power -> power with
kg*m²/s³
(Watts) as base unit
New units and their combinations can be added to the json files in the generator repository. Running the generator will then produce the source files for the new units. For units that are created by squaring another unit (like area) there exits a square function for the base unit (eg. myArea = square(myLength)). At the moment all combinations of units have to be manually added if there is a missing combinations feel free to open an issue or pr in the generator repository.
Additional types and functions
The following list contains functions that should be implemented if the language supports them to make using the units close to how you would use them on paper as possible.
- units_cast: All types have a function to cast the multiplier and offset to a desired SI prefix. This allows one part of the code to work in kmph and another one in mps without causing any problem with passing a value with the wrong multiplier.
- unit_cast (time integration): The unit_cast function should also allow casting SI time units to the time format of the programming language. In C++17 this allows creating a time_si object from std::chrono::duration to work with the now functions of the standard library.
- clamp, max, min, and comparison: If the language supports it the clamp, min and max functions should be overloaded in a way that they provide accurate comparison of two units. In addition to that the comparison operators should be overloaded if that is supported by the programming language.
- math operator overloads: If the language supports it the * and / should be overloaded to allow multiplying the unit with a scalar number. The + and – operators should be overloaded to allow subtracting or adding the same unit. Also, the / operator should be overloaded with the same unit to allow producing a scalar. Operator overloads between different units should only exist to create a defined unit combination (eg. length/time = speed). All other cases of operator overloading should not exist. This is to prevent accidental errors from happening as much as possible.