[Units] Variant runtime analysis

Hello all, My use case is the following : I have a function that returns quantities of different units depending on its input parameters (different derivatives). I could split the function in two parts that returns one unit only, but that would move the problem to an other place in my code. Is there any way to handle this, without breaking the dimensional analysis ? I have been playing with things like : quantity<variant<force, length, energy> > but it doesn't seems to go anywhere... Samuel

On Feb 25, 8:21 am, Samuel Debionne <debio...@hydrowide.com> wrote:
I have been playing with things like :
quantity<variant<force, length, energy> >
That won't work because variant<...> is not a unit type. Can you post a small example of what you want to achieve? Alfredo

I have been playing with things like :
quantity<variant<force, length, energy> >
That won't work because variant<...> is not a unit type. Can you post a small example of what you want to achieve?
Of course it won't work as is. But I did a bit of hacking in this direction. My aim is to be able to write something like this : any_quantity<force, length, energy> any_q = myfunction(...); ... quantity<force> = any_q; //runtime check Is there any plan to have a runtime layer above Boost.Units (like boost GIL have dynamic any_images, any_view...) ? It would check the maximum at compile time (basically check if one the variant type could match the target quantity unit) and delegates the real check at runtime (throwing an exception if the conversion fails) ? IMHO it would be a lovely addition to the current library.

On Feb 28, 2:06 am, Samuel Debionne <debio...@hydrowide.com> wrote:
I have been playing with things like :
quantity<variant<force, length, energy> >
That won't work because variant<...> is not a unit type. Can you post a small example of what you want to achieve?
Of course it won't work as is. But I did a bit of hacking in this direction.
the reason I asked for an example is because I was working in something similar (not the same though).
My aim is to be able to write something like this :
any_quantity<force, length, energy> any_q = myfunction(...);
...
quantity<force> = any_q; //runtime check
I think those quantities are so unrelated with each other from a "type" point of view that you would be better of using templates. Actually I do this a lot: template<class Unit> myfunction(quantity<Unit> q){ ... } and also (if possible) auto q = myfunction( ...);
Is there any plan to have a runtime layer above Boost.Units (like boost GIL have dynamic any_images, any_view...) ?
I think this goes against the library design which is to be zero overhead. (But it may be useful some times although my pattern above is a good competition against your requested feature) What I was working on is something like: any_quantity<force_dimension> that can hold any quantity with force dimension, this makes more sense to me because at least I know the all force quantities are potentially convertible to one another, regardless of the unit system. In my proposal there is no runtime checking at all, but only runtime conversions. Good Luck, Alfredo
It would check the maximum at compile time (basically check if one the variant type could match the target quantity unit) and delegates the real check at runtime (throwing an exception if the conversion fails) ?
IMHO it would be a lovely addition to the current library.
debionne.vcf < 1KViewDownload
_______________________________________________ Boost-users mailing list Boost-us...@lists.boost.orghttp://lists.boost.org/mailman/listinfo.cgi/boost-users

On Feb 28, 10:34 am, alfC <alfredo.cor...@gmail.com> wrote:
On Feb 28, 2:06 am, Samuel Debionne <debio...@hydrowide.com> wrote:
I have been playing with things like :
quantity<variant<force, length, energy> >
That won't work because variant<...> is not a unit type. Can you post a small example of what you want to achieve?
Of course it won't work as is. But I did a bit of hacking in this direction.
the reason I asked for an example is because I was working in something similar (not the same though).
My aim is to be able to write something like this :
any_quantity<force, length, energy> any_q = myfunction(...);
...
quantity<force> = any_q; //runtime check
I think those quantities are so unrelated with each other from a "type" point of view that you would be better of using templates. Actually I do this a lot:
template<class Unit> myfunction(quantity<Unit> q){ ...
}
Also I forgot to mention that with the strategy above you have to deduce the return type of the function, that depends on the logic of function. typeof_helpers help in this case. template<class Unit> quantity< power_typeof_helper< multiply_typeof_helper < ... Unit ... , ... >::type, ... >::type > myfunction(quantity<Unit> q){ ... } I know it can be a drag. I guess auto typed return types in C++0x can help but I never tried. Sorry for the splitted answer. In any case I would like to know of any solution you can come up with. Alfredo

Alfredo, Thank you for your answers. To be more pragmatic here is an archetype of function that I have in my problem : any_quantity<force, length> myfunction(energy_q arg1) { if (arg1 > 1.0 * jouls) return 2.2 * newtons; else return 3.3 * meters; } That may sound weird but it's a real case. I'm solving a problem with two quantities that have unrelated units and one quantiy or the other is choosen at runtime depending on some state variable values. Ultimately those quantities are stored in a single matrix. For now I break the dimensionnal analysis at this step using plain doubles.
I think this goes against the library design which is to be zero overhead. (But it may be useful some times although my pattern above is a good competition against your requested feature)
What I was working on is something like:
any_quantity<force_dimension>
that can hold any quantity with force dimension, this makes more sense to me because at least I know the all force quantities are potentially convertible to one another, regardless of the unit system. In my proposal there is no runtime checking at all, but only runtime conversions.
Runtime conversion are also something I am interrested. Are you following the lead of : http://www.boost.org/doc/libs/1_46_0/doc/html/boost_units/Examples.html#boos... I think that we could layer things this way : LAYER 3: any_quantity<heterogeneous_units> runtime dimensional analysis and conversion LAYER 2: any_quantity<homogeneous_dimension_units> compile time dimensional analysis and runtime conversion LAYER 1: quantity<unit> the actual library Each extension layers could be build on top of the others. For performance reason, the runtime dimensional analysis could be switched off for release build.
In any case I would like to know of any solution you can come up with.
Of course.

Samuel Debionne <debionne <at> hydrowide.com> writes:
Runtime conversion are also something I am interrested. Are you following the lead of :
http://www.boost.org/doc/libs/1_46_0/doc/html/boost_units/Examples.html#boos... nits.Examples.RuntimeUnits
I think that we could layer things this way :
LAYER 3: any_quantity<heterogeneous_units> runtime dimensional analysis and conversion
LAYER 2: any_quantity<homogeneous_dimension_units> compile time dimensional analysis and runtime conversion
LAYER 1: quantity<unit> the actual library
Fwiw: this runtime variant would be really welcome. In our application we calculate some data with various units (e.g. m/s, m) which gets plotted. The plotter just talks to the base class of the data, like: struct Data { Unit GetUnit () const; double GetValue(size_t nIndex) const; }; Also the user is able to specify his display units (e.g. cm instead of m), and we use the runtime unit for scaling the physical quantities. (snipped some quoted stuff, gmane reports: 'There's much more quoted text in your article than new. Prune quoted stuff.')
participants (3)
-
alfC
-
gast128
-
Samuel Debionne