![]() |
Trax3 3.1.0
trax track library
|
Calculations in physics not only deal with stark naked numbers (for tranquility of mind we call them 'values'), but they are generally also connected with two other aspects: dimension and unit. The dimension is the quality that is dealt with, be it a length in space, a time span or an inertial mass or any combination of these. The units on the other hand specify the quantity we are measuring, telling us that 1000 are actually 1000_kg, not 1000_t. If you do physics calculation it is wise to check the dimensions of your result and then reason about its magnitude. For the trax library the dim library automates both.
To define a length of 2 meters, with dim we would write (the code can be found in /Test/dim/TestDimDocu.cpp):
Note that we can use the literal operator '_m' to denote a length in meters. We also can use other operators like '_cm' for centimeters or '_km' for kilometers:
We are not confined to the metric system, we can also use imperial units:
Note the check that uses the operator '<' between two Length values. With stark naked numbers we would have to take care of the units ourselves, but with dim this is done automatically and safely. You'll never need to care about what unit a value has, ever again. Also note our first arithmetic operation with dimensionated values: the addition of two Length values, even with different units.
Multiplication also works:
The dim library deals with three base dimensions: Length, Mass and Time. From these three base dimensions several other physical dimensions can be constructed:
Note that division in units we symbolize by a capital 'I' in the unit literal.
If we wanted the velocity in m/s we would write:
If g is the acceleration due to gravity, we can calculate the time to fall from s = 1/2 * g * t * t; -> t = sqrt(2*s/g):
You might have noticed that all of a sudden we get the velocity always in m/s. This is because we permanently specified this unit for the streaming target std::cout. To get km/h again we need:
The dim library is designed to prevent you from making mistakes with dimensions and units. If you try to add two values with different dimensions, e.g. a Length and a Time, the compiler will complain. The same happens if you try to assign a value with wrong dimension to a variable.
One might think that the dim library is providing conversions of units and maybe a certain clarity on method parameter definitions; but that is not its primary goal. The main purpose of the dim library is to provide a type-safe way to work with physical quantities and their units, preventing common mistakes and ensuring correct calculations. It is actually the main purpose of the dim library to produce compiler errors: if a complicated formula compiles without errors, be assured it is mathematically correct with respect to dimensions and units.
If you have a stark naked number 'lengthInCentimeters' and happen to know it to be centimeters, assign it like this:
If you want to get a value in meters from a dim::Length length, do this:
The other units of course work accordingly.
A Dimensionated Value with no dimension is still a Dimensionated Value (One ) and is to be distinguished conceptionally from a stark naked number like float, int or double. Angles are of dimension one as well. Both convert seamlessly to each other and to stark naked numbers, but are still different types:
The limits of numerical calculations. E.g. for EEP, the smallest value in length that would make a difference to the user, trax::epsilon__length, would be something about 1_cm. From this the trax::plausible_maximum_length can be estimated to be about 10_km for 32bit trax::Real values. At that distance from the origin, the calculations would start to produce inaccurate results with respect to the 1_cm difference. It is not accidential, that the biggest layouts ever build in EEP are about 20_km in diameter. For optimal results, the trax::meters_per_unit should not be 0.01 nor 1 but rather something like 15.
This section documents the output stream operators for dimensionated values.
This section documents the input stream operators for dimensionated values.
The dim library provides support for reading and writing dimensionated values in XML format, using the Boost Property Tree library.