|
| | Unit ()=default |
| | Construct the same as a std::int32_t; expect the value to be uninitalized. More...
|
| |
| | Unit (const Unit &)=default |
| | Copy construct the same as a std::int32_t. More...
|
| |
| constexpr | Unit (std::int32_t U) |
| | Make a new Unit set to a specific unit value; intended for making common unit constants with DUDS_UNIT_VALUE and initializing to zero for no unit or abstract unit. More...
|
| |
| | Unit (int A, int cd, int K, int kg, int m, int mol, int s, int rad=0, int sr=0) |
| | Make a new Unit and set all the exponents to the given values with range checking. More...
|
| |
| int | A () const |
| | Returns the exponent for the ampere (current) dimension. More...
|
| |
| void | A (int e) |
| | Sets the exponent for the ampere (current) dimension without range checking. More...
|
| |
| int | amp () const |
| | Returns the exponent for the ampere (current) dimension. More...
|
| |
| void | amp (int e) |
| | Sets the exponent for the ampere (current) dimension without range checking. More...
|
| |
| int | ampere () const |
| | Returns the exponent for the ampere (current) dimension. More...
|
| |
| void | ampere (int e) |
| | Sets the exponent for the ampere (current) dimension without range checking. More...
|
| |
| int | candela () const |
| | Returns the exponent for the candela (luminous intensity) dimension. More...
|
| |
| void | candela (int e) |
| | Sets the exponent for the candela (luminous intensity) dimension without range checking. More...
|
| |
| int | cd () const |
| | Returns the exponent for the candela (luminous intensity) dimension. More...
|
| |
| void | cd (int e) |
| | Sets the exponent for the candela (luminous intensity) dimension without range checking. More...
|
| |
| void | clear () |
| | Makes the Unit unitless. More...
|
| |
| int | K () const |
| | Returns the exponent for the kelvin (thermodynamic temperature) dimension. More...
|
| |
| void | K (int e) |
| | Sets the exponent for the kelvin (thermodynamic temperature) dimension without range checking. More...
|
| |
| int | kelvin () const |
| | Returns the exponent for the kelvin (thermodynamic temperature) dimension. More...
|
| |
| void | kelvin (int e) |
| | Sets the exponent for the kelvin (thermodynamic temperature) dimension without range checking. More...
|
| |
| int | kg () const |
| | Returns the exponent for the kilogram (mass) dimension. More...
|
| |
| void | kg (int e) |
| | Sets the exponent for the kilogram (mass) dimension without range checking. More...
|
| |
| int | kilogram () const |
| | Returns the exponent for the kilogram (mass) dimension. More...
|
| |
| void | kilogram (int e) |
| | Sets the exponent for the ampere (current)kilogram (mass) dimension without range checking. More...
|
| |
| int | m () const |
| | Returns the exponent for the meter (distance) dimension. More...
|
| |
| void | m (int e) |
| | Sets the exponent for the meter (distance) dimension without range checking. More...
|
| |
| int | meter () const |
| | Returns the exponent for the meter (distance) dimension. More...
|
| |
| void | meter (int e) |
| | Sets the exponent for the meter (distance) dimension without range checking. More...
|
| |
| int | metre () const |
| | Returns the exponent for the meter (distance) dimension. More...
|
| |
| void | metre (int e) |
| | Sets the exponent for the meter (distance) dimension without range checking. More...
|
| |
| int | mol () const |
| | Returns the exponent for the mole (amount of substance) dimension. More...
|
| |
| void | mol (int e) |
| | Sets the exponent for the mole (amount of substance) dimension without range checking. More...
|
| |
| int | mole () const |
| | Returns the exponent for the mole (amount of substance) dimension. More...
|
| |
| void | mole (int e) |
| | Sets the exponent for the mole (amount of substance) dimension without range checking. More...
|
| |
| bool | operator!= (const Unit &U) const |
| | Unit objects are compared using the u member. More...
|
| |
| const Unit | operator* (const Unit &U) const |
| | Combines two units into a new unit. More...
|
| |
| Unit & | operator*= (const Unit &U) |
| | Combines two units into a new unit. More...
|
| |
| const Unit | operator/ (const Unit &U) const |
| | Combines two units into a new unit. More...
|
| |
| Unit & | operator/= (const Unit &U) |
| | Combines two units into a new unit. More...
|
| |
| bool | operator< (const Unit &U) const |
| | Unit objects are compared using the u member. More...
|
| |
| bool | operator<= (const Unit &U) const |
| | Unit objects are compared using the u member. More...
|
| |
| bool | operator== (const Unit &U) const |
| | Unit objects are compared using the u member. More...
|
| |
| bool | operator> (const Unit &U) const |
| | Unit objects are compared using the u member. More...
|
| |
| bool | operator>= (const Unit &U) const |
| | Unit objects are compared using the u member. More...
|
| |
| int | rad () const |
| | Returns the exponent for radians (angle). More...
|
| |
| void | rad (int e) |
| | Sets the exponent for radians (angle) without range checking. More...
|
| |
| int | radian () const |
| | Returns the exponent for radians (angle). More...
|
| |
| void | radian (int e) |
| | Sets the exponent for radians (angle) without range checking. More...
|
| |
| int | s () const |
| | Returns the exponent for the second (time) dimension. More...
|
| |
| void | s (int e) |
| | Sets the exponent for the second (time) dimension without range checking. More...
|
| |
| int | second () const |
| | Returns the exponent for the second (time) dimension. More...
|
| |
| void | second (int e) |
| | Sets the exponent for the second (time) dimension without range checking. More...
|
| |
| void | setAmp (int e) |
| | Sets the exponent for the ampere (current) dimension. More...
|
| |
| void | setAmpere (int e) |
| | Sets the exponent for the ampere (current) dimension. More...
|
| |
| void | setCandela (int e) |
| | Sets the exponent for the candela (luminous intensity) dimension. More...
|
| |
| void | setKelvin (int e) |
| | Sets the exponent for the kelvin (thermodynamic temperature) dimension. More...
|
| |
| void | setKilogram (int e) |
| | Sets the exponent for the kilogram (mass) dimension. More...
|
| |
| void | setMeter (int e) |
| | Sets the exponent for the meter (distance) dimension. More...
|
| |
| void | setMetre (int e) |
| | Sets the exponent for the meter (distance) dimension. More...
|
| |
| void | setMole (int e) |
| | Sets the exponent for the mole (amount of substance) dimension. More...
|
| |
| void | setRadian (int e) |
| | Sets the exponent for radians (angle). More...
|
| |
| void | setSecond (int e) |
| | Sets the exponent for the second (time) dimension. More...
|
| |
| void | setSteradian (int e) |
| | Sets the exponent for steradians (solid angle). More...
|
| |
| int | sr () const |
| | Returns the exponent for steradians (solid angle). More...
|
| |
| void | sr (int e) |
| | Sets the exponent for steradians (solid angle) without range checking. More...
|
| |
| int | steradian () const |
| | Returns the exponent for steradians (solid angle). More...
|
| |
| void | steradian (int e) |
| | Sets the exponent for steradians (solid angle) without range checking. More...
|
| |
| bool | unitless () const |
| | Returns true if the Unit represents no units. More...
|
| |
| std::int32_t | value () const |
| | Returns the internal exponent fields. More...
|
| |
Represents an SI unit, either base or derived.
- Todo:
- Rename to SiUnit? Not sure.
Each base SI unit has a nibble sized field inside a 32-bit integer. There are an additional two bits each for radians and steradians. Each of these fields is a signed integer that is the exponent for the unit. All the units are multiplied, with their exponent applied, to produce the final unit.
Functions named for the units are available to get the exponent for the unit and to change the exponent. The functions that begin with "set" will perform range checking, while the other functions will not. Without range checking, the exponent to change will be incorrect if it is outside of the allowable range (see Bit fields below), but all other exponents will always remain unchanged.
Multiply and divide operators are supplied for combining Unit objects. These operators perform range checking and will throw if any exponent goes beyond the allowable range.
- Inconsistency with kilogram
- In order to avoid having a bunch of unusual derived SI units, mass is in kilograms instead of grams. As a result, an exponent of 1 for mass denotes kilograms. This makes for a special case to avoid a user interface from showing something like "millikilograms" when the scale is adjusted.
- Time
- Time is typically stored in types defined by C++ in the std::chrono namespace. These types explicitly define the unit in use. If a Unit is associated with one of these types, the Unit should indicate seconds and the software should use the type for the actual units. While this is a special case, time is already a big and annoying special case.
- Bit fields
- Access to the fields are coded explicitly to improve portability. The 32-bit integer containing all the fields is stored in native endianness.
| Base unit | Byte | Bit range | Exponent range |
| Ampere | 0 | 0-3 | -8 to 7 |
| Candela | 0 | 4-7 | -8 to 7 |
| Kelvin | 1 | 8-11, 0-3 | -8 to 7 |
| Kilogram | 1 | 12-15, 4-7 | -8 to 7 |
| Meter | 2 | 16-19. 0-3 | -8 to 7 |
| Mole | 2 | 20-23, 4-7 | -8 to 7 |
| Second | 3 | 24-27, 0-3 | -8 to 7 |
| Radian | 3 | 28-29, 4-5 | -2 to 1 |
| Steradian | 3 | 30-31, 6-7 | -2 to 1 |
Possible change to 64-bit integer (bad):
| Unit | Byte | Bit range | Exponent range | Notes |
| Ampere | 0 | 0-4 | -16 to 15 | SI base unit |
| Candela | 0-1 | 5-9 | -16 to 15 | SI base unit |
| Kelvin | 1 | 10-14 | -16 to 15 | SI base unit |
| Kilogram | 1-2 | 15-19 | -16 to 15 | SI base unit |
| Meter | 2-3 | 20-24 | -16 to 15 | SI base unit |
| Mole | 3 | 25-29 | -16 to 15 | SI base unit |
| Second | 3-4 | 30-34 | -16 to 15 | SI base unit |
| Radian | 4 | 35-38 | -8 to 7 | Needed for torque |
| Steradian | 4-5 | 39-42 | -8 to 7 | Needed for lumen |
| Becquerel | 5 | 43-46 | -8 to 7 | Not the same as hertz |
| Gray | 5-6 | 47-50 | -8 to 7 | Not the same as sievert |
| Sievert | 6 | 51-54 | -8 to 7 | Not the same as gray |
| Rel humid | 6-7 | 55-58 | -8 to 7 | Unitless if not included |
Another possible change (better):
| Unit | Byte | Bit range | Exponent range | Notes |
| Ampere | 0 | 0-4 | -16 to 15 | SI base unit |
| Candela | 0-1 | 5-9 | -16 to 15 | SI base unit |
| Kelvin | 1 | 10-14 | -16 to 15 | SI base unit |
| Kilogram | 1-2 | 15-19 | -16 to 15 | SI base unit |
| Meter | 2-3 | 20-24 | -16 to 15 | SI base unit |
| Mole | 3 | 25-29 | -16 to 15 | SI base unit |
| Second | 3-4 | 30-34 | -16 to 15 | SI base unit |
| Radian | 4 | 35-38 | -8 to 7 | Needed for torque |
| Steradian | 4-5 | 39-42 | -8 to 7 | Needed for lumen |
| Property | 5 | 43-46 | NA (4 bits) | Arbitrary physical propery value |
| Scale/off | 6-7 | 47-63 | NA (17 bits) | Scale or offset? |
The propery value is an arbitrary value that, when taken with the context of the other values, indicates the physical property being measured. The above suggestion allows for 16 different properties for each possible combination of the base SI units.
Yet another possible change (best?):
| Unit | Byte | Bit range | Exponent range | Notes |
| Ampere | 0 | 0-4 | -16 to 15 | SI base unit |
| Candela | 0-1 | 5-9 | -16 to 15 | SI base unit |
| Kelvin | 1 | 10-14 | -16 to 15 | SI base unit |
| Kilogram | 1-2 | 15-19 | -16 to 15 | SI base unit |
| Meter | 2-3 | 20-24 | -16 to 15 | SI base unit |
| Mole | 3 | 25-29 | -16 to 15 | SI base unit |
| Second | 3-4 | 30-34 | -16 to 15 | SI base unit |
| Radian | 4 | 35-39 | -16 to 15 | Needed for torque |
| Steradian | 5 | 40-44 | -16 to 15 | Needed for lumen |
| Property | 5-7 | 45-63 | NA (19 bits) | Arbitrary physical propery value |
Property value meanings:
- Ratio (non-zero to differentiate from unknown/unset)
- Calibrated instrument
- Raw unconverted data; meaning/units are device specific
- Value from local (device) axis (?)
- Something to help with ambiguity:
- Hertz versus Becquerel
- Sievert versus Gray
- Bug:
- Some units, like Watts cubed, cannot be represented. Consider larger fields. Maybe use larger fields on another type used for intermediate results and unusual units and allow implicit conversions.
- Author
- Jeff Jackowski
- Examples:
- am2320test.cpp, apds9301test.cpp, and tsl2591test.cpp.
Definition at line 178 of file Unit.hpp.