Re: [Boost-users] [boost] [review][constrained_value] Review of ConstrainedValueLibrary begins today

OK, now it makes a bit more sense. Moreover, I believe it is possible (though maybe not trivial) to implement something similar with the current
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2199.html Thanks for educating me on this. I find the myvar.value() syntax to be a
Robert:
As a caveat and analogy: I want to use this to represent mathematics rather
than just bounds-checking. The constrained value manages the subset within
an underlying space (represented by the underlying type int, double, etc.).
Then an instantiation of this is an element in this set. And my goal is to
be able to take an instantiation/element and access information about the
underlying set/type itself (eg managing bounds for optimizers, domain/range
for functors, etc. from only an instance of the type passed in to a generic
function). And if you use linear operators on elements of the subset, then
it always returns members of the underlying space (which is mathematically
correct. integers are a subset of reals which is a linear space with the
+,-,/,* operators on it. Divide an integer by an integer and you get an
element of the superset)
More questions:
1) Storage the same as underlying type?:::::::
library.
Cool. This would be necessary for me to use the library, so I hope it is
possible to have dynamic bounds associated with the type. If this was
completed, would the space/storage taken up by an instantiation of the
bounded type be the same as the underlying type? In particular, I would be
interested in having a vector of bounded double's have the same storage aa
vector of bounded doubles (I would be using ublas for example) so that I
could potentially pass on a pointer to a C function for some
interoperability (I know... hacky but sometimes necessary for all the C
scientific computing libraries out there). And for my numerical work,
having an extra parameter or two stored for every value in a big matrix
would eat up cache/memory too quickly for no value.
If necessary to accommodate consistency of the storage with just the
underlying type, perhaps the idea of a bounded value on an instantiation
(like it is today for the runtime) vs. a bounded type (modifiable at
runtime, but common to all instance) could have a different template...
something like:
typedef bounded_type

Hi Jesse,
From: jesseperla@gmail.com
OK, now it makes a bit more sense. Moreover, I believe it is possible (though maybe not trivial) to implement something similar with the current
Cool. This would be necessary for me to use the library, so I hope it is
First, I hope that you are aware of the issues with using floating point types
with this library. If not, please read the rationale section of the
documentation and the discussion in the related thread on the developers' list.
library.
possible to have dynamic bounds associated with the type. If this was completed,
would the space/storage taken up by an instantiation of the bounded type be the
same as the underlying type?
Yes, provided you use a zero-size constraint class and your compiler is
opimising well (see http://article.gmane.org/gmane.comp.lib.boost.devel/183612).
The constraint could look like this (written out of my head, not tested):
template
so that I could potentially pass on a pointer to a C function for some interoperability
Ouch. :P
For example, would something like the following work to ensure consistency in my own usage?: template<typname T> void myfunc1(T& t) { cout << std::max(0.0, static_cast<double>(t)); //Note a floating point here }
Yes, explicit casts will work for both constrained and underlying type values.
Does the constrained value only have a direct cast to its underlying type, or is there any way to have it cast to any types that the underlying type can cast to?
It has a cast to the underlying type, but then the underlying type may be convertible to some other type, so: constrained<double> x; static_cast<int>(x); ...will work (conversions are constrained<double> -> double -> int).
3) Bounds including infinity?:::::::: When we are working with bounds, can we use the numeric infinities with both open and closed sets? I am thinking something like: typedef bounded_type
risk_aversion; risk_aversion::change_bounds(1, std::numeric_limits<double>::infinity()); //Might want open or closed depending on if function domain is reals or extended reals.
Yes, if you really want to use floating point types (even though this is discouraged), you can do this.
4) Numeric limits traits?:::::::::::: What would be really useful is if bounded<> and other types would generate traits on their own based on the underlying type... this way, we wouldn't have to create numeric_limits traits for the types ourselves (which is unreasonable for a library user).
Why not just use numeric_limits
5) Testing set inclusion:::::::: While it is nice to have the bounds checking on the () operator, I would also want to be able to ask the type if a value is in the set. For example, in math:
if( risk_aversion::get_bounds_const().is_within(3) ) // ...
6) No debug only functionality please:::::::::::: On all of the conversations about turning off bounds checking on debug, I would definitely not want this to happen automatically. And a bounds checking failure should be an exception, not a non-recoverable error.
This is how the default error policy works.
7) Operations consistent with built in C++ numeric types::::::::::::::::::: You have successfully educated me on why you need to overload the ++, --, +, -, etc. operators yourself and can't just revert to the underlying type.
Actually, only the mutating operators (++, --, =, += etc.) are overloaded. For the rest, the underlying type's operator can be used.
But focusing entirely on intrinsic numeric types double, int, etc.: Will we have complete coverage of the operators that are defined for these types in C++?
I don't think this is needed. If you write: constrained<int> x, y, z; z = x + y; Then simply the + operator for int is used.
Will the compiler end up generating EXACTLY the same code if I do a whole bunch of read only operations on a bounded<double> vs. a double?
Quite possibly. Best regards, Robert
participants (2)
-
jesseperla@gmail.com
-
Robert Kawulak