last day (15 days later) » 

12:41 PM
1
A: User defined types - units, conversions

OutOfBoundI think you tackle your problem from the wrong direction. You do not want to transform arbitrary units, but units within a quantity. So I would start like this: struct Quantity { double value; }; // Base Quantites class Length : public Quantity {}; class Duration : public Quantity {}; // ...

 
The point of the exercise is to create units that can be used like 'normal' data types, but that don't allow implicit conversions. Using extra functions like your 'transform' take away from the natural use of types that would know how to convert without added syntax. The data types I described and implemented above work very naturally, and make it impossible to inadvertently mix meters with mm, or degrees with radians. I would just like to come up with an elegant way to do confersions so I could do math and assignments without using the likes of your transform function.
 
Why do you want to make implicit conversion impossible? Your physical background is the same, no matter if you input mm oder meter, both is a length and it is a skalar quantity. So why would you want to prevent someone to do an operation on meter and mm?
 
adding mm to meters is ok, so long as mm knows how to convert to meters. If I say mm = double, meters = double, add mm to meters, I have a bad answer. If mm = double, mm = int, adding is still possible due to implicit conversion, but the answer is not likely what I want. I want to make sure that interfaces know that the data units are directly compatible, not implicitly converted somewhere.
 
You should stop thinking about adding Millimeters and Meters and start thinking about adding two lengths. If the underlying representation of the same quantity is different over the operands of a operations (e.g. addition), than this is a whole new problem for itself and will get you into more trouble. If you are not very familiar with templates in c++, you should not atempt implementing it, because it will add much boilerplate code to your problem. And even if you implemented it correctly you run consistency problems, because your precision changes along your computation.
Assume the following operation: Millimeter<int>(Meter<double>(0.1) / 3)^2. This will result in in 9mm^2. while the calculation done in float will lead to 11,1mm^2. Your error is already 2.1mm and you have 0 significant digits.
 
I guess an important point that is missing, is that I intend to pass these types through interfaces. This means I need types, not functions that allow me to create millimeters out of meters. I want to have functions know that the units of the data being passed are correct, in an environment where I have mixed units at different interface points. Sometimes the units are mm, sometimes encoder counts that translate to mm. I want to make it clear at all interface points what the units are, and have it enforced by the compiler, not the programmer.
 
12:43 PM
You can do so even if you use Quantities over Units. Your Interface would look like this:
`calculateWhatever(Length l, Duration d, Force f, Energy e)`
And a call may look like this calcWhatever(5*Millimeter(), ....) or this calcWhatever(0.005*Meter(), ...)
Or this calcWhatever(encoderTicks * MeterPerTicks, ....)
But you always get a notion of length
 
12:58 PM
I guess the problem is that I have different units in different layers of the code. units are millimeters where the code layer needs mm. There are counts where the code needs counts. It is not feasible to change to quantities everywhere, because we would be converting back and forth all over the place. To make it even more confusing, in some places counts are doubles, because the code is trying to keep track of fractions of counts, if you can imagine.
Sometimes velocites are counts per second, sometimes mm per second. etc.
With the method I was working on, it was easy to create units of counts, mm, meters, cts per second, mm per second, etc. and use them rather easily and naturally, just like they were native types. I am very happy with that part. I just don't have a natural way to do the conversions where I need them
I can brute force it a number of ways, but I was hoping to incorporate it into the 'types'
Some of the libraries available do it, but they are very heavy looking libraries.
youtube.com/watch?v=0iWb_qi2-uI This link to Stroustrup's address in 2012 is worth watching to understand the issue I'm trying to address. Starts at about minute 20 in the talk.
I also need to contain these types in vectors. For example, I may need to pass a vector of millimeters to express the axis coordinates for the endpoint of a move. This will be converted to counts somewhere down the line. counts per millimeter varies at runtime, so this can't be a compile time conversion.
 
I watched the talk from stroustrup and he really does something different.
Dealing with different units in different parts of the code sounds very messy to me. The usual way these problems are solved by the same representation within the whole codebase and transfroming the values once on input and once on output.
Its done this way in heavy simulations and in the std library, like in std::chrono
You can take a look at std::chono::duration
Its a similar problem with seconds, minutes, hours and years and the use the same representation (by default double) across all of them
Its the only way to stay sane.
 
I'm trying to refactor the code to get rid of the differing units throughout the code. As part of that effort, I'm trying to come up with stronger typed units. Everything now is int's, floats and doubles, only enforced by the programmers.
It is quite a mess.
 
Thats the way to go
Try to keep units out of the equation at all, by using wrapper types with a common base unit
usually the SI base units are used internally.
 
1:14 PM
The human interface wants mm, but the motion controller must work in its own units, which are rather archaic. There are other layers in between to do coordinate translations, rotations, and compensations. It is a long way between saying move to x, y, z, and getting the move done.
my base units are really mm and seconds, typically. I'll drive in that direction
 

last day (15 days later) »