S
Stephan Rose
Hi everyone,
I have a little problem I'm not sure what to do about or if anything can
even be done about it.
Situation is as follows:
I had previous implemented a class called Scalar that is essentially a
wrapper around double that contains an additional field to store what
unit type is represented by this double.
So this allowed me to add 2 values together regardless of their unit
type. For example, 1 inch + 1 millimeter. The way I had implemented this
was just using one class, with a small 2 dimensional table that contained
the conversion factors. Then the various arithmetic operators would do a
lookup in this conversion table using the unit type as index into the
table.
The result works very well, but has a significant performance overhead
compared to just plain double. It pretty much cuts the speed in half.
Also, another problem with this table is that I have the factor "1" going
across the diagonal. So everytime I am performing arithmetic with units
of the same type I have a needless multiply by 1 in there.
This was before I started using STL. =)
Using STL however I came up with a completely different concept that
eliminates the conversion table and the need to store the unit type.
I defined a template class called "Scalar", and then for each unit type,
I also defined an additional class. All classes implement the appropriate
arithmetic operators and the unit type classes contain function that
allow them to convert to other types of units. The Scalar template then
contains conversion operators that will then call the appropriate
conversion function of it's type when necessary.
The benefit of this is that if I add 1 mil to 1 mil, there no longer is a
needless multiply by 1. It is now reduced to 1 double + 1 double and
that's it. Zero performance overhead for adding units of the same type.
If I add units of different type, the conversion operator in the Scalar
template just calls the appropriate conversion function of it's type and
then performs the arithmetic. Advantage here being that the conversion is
now much simpler than before as it now is just a static multiply by a
constant and no longer involves a table lookup. Not as fast as adding
just 2 doubles, but still by magnitudes faster than my previous method.
Also, my memory footprint is significantly reduced as I no longer need to
store a unit type.
So essentially, my code went from this:
Scalar a(1, ScalarUnitMil);
Scalar b(1, ScalarUnitInch);
Scalar c = a + b;
To this:
Scalar<Mil> a(1);
Scalar<Inch> b(1);
Scalar<Mil> c = a + b;
Now, here is where my problem comes in. Scalar c in my old code is unit
independent. The unit type that it will be depends on the result of a +
b; So if I add inch to an inch then c will be an inch. If I add a
millimeter to a millimeter it'll be a millimeter.
With my STL version however, I have to explicitly state what unit I'm
going to store in it. So no matter what my math operation is, the result
will be Mil.
99% of the time, this isn't a problem. It's a problem 1% of the time when
the user goes to enter data and I then go to store that value. The user
input is going to be converted to whatever unit type I previously defined
in my code and I will loose that information. The value of course will
still be correct, but it'll be in a different unit. This isn't a major
deal-breaker, but if I could avoid it then it would be nice.
So long story short, is there any way to declare variable C in such a way
as to where I don't need to know ahead of time what unit type it's going
to be? I suspect no as I currently can't imagine a way that would be
possible...but since I'm still new to STL I figured I'd ask.
Thanks,
--
Stephan
2003 Yamaha R6
å›ã®ã“ã¨æ€ã„出ã™æ—¥ãªã‚“ã¦ãªã„ã®ã¯
å›ã®ã“ã¨å¿˜ã‚ŒãŸã¨ããŒãªã„ã‹ã‚‰
I have a little problem I'm not sure what to do about or if anything can
even be done about it.
Situation is as follows:
I had previous implemented a class called Scalar that is essentially a
wrapper around double that contains an additional field to store what
unit type is represented by this double.
So this allowed me to add 2 values together regardless of their unit
type. For example, 1 inch + 1 millimeter. The way I had implemented this
was just using one class, with a small 2 dimensional table that contained
the conversion factors. Then the various arithmetic operators would do a
lookup in this conversion table using the unit type as index into the
table.
The result works very well, but has a significant performance overhead
compared to just plain double. It pretty much cuts the speed in half.
Also, another problem with this table is that I have the factor "1" going
across the diagonal. So everytime I am performing arithmetic with units
of the same type I have a needless multiply by 1 in there.
This was before I started using STL. =)
Using STL however I came up with a completely different concept that
eliminates the conversion table and the need to store the unit type.
I defined a template class called "Scalar", and then for each unit type,
I also defined an additional class. All classes implement the appropriate
arithmetic operators and the unit type classes contain function that
allow them to convert to other types of units. The Scalar template then
contains conversion operators that will then call the appropriate
conversion function of it's type when necessary.
The benefit of this is that if I add 1 mil to 1 mil, there no longer is a
needless multiply by 1. It is now reduced to 1 double + 1 double and
that's it. Zero performance overhead for adding units of the same type.
If I add units of different type, the conversion operator in the Scalar
template just calls the appropriate conversion function of it's type and
then performs the arithmetic. Advantage here being that the conversion is
now much simpler than before as it now is just a static multiply by a
constant and no longer involves a table lookup. Not as fast as adding
just 2 doubles, but still by magnitudes faster than my previous method.
Also, my memory footprint is significantly reduced as I no longer need to
store a unit type.
So essentially, my code went from this:
Scalar a(1, ScalarUnitMil);
Scalar b(1, ScalarUnitInch);
Scalar c = a + b;
To this:
Scalar<Mil> a(1);
Scalar<Inch> b(1);
Scalar<Mil> c = a + b;
Now, here is where my problem comes in. Scalar c in my old code is unit
independent. The unit type that it will be depends on the result of a +
b; So if I add inch to an inch then c will be an inch. If I add a
millimeter to a millimeter it'll be a millimeter.
With my STL version however, I have to explicitly state what unit I'm
going to store in it. So no matter what my math operation is, the result
will be Mil.
99% of the time, this isn't a problem. It's a problem 1% of the time when
the user goes to enter data and I then go to store that value. The user
input is going to be converted to whatever unit type I previously defined
in my code and I will loose that information. The value of course will
still be correct, but it'll be in a different unit. This isn't a major
deal-breaker, but if I could avoid it then it would be nice.
So long story short, is there any way to declare variable C in such a way
as to where I don't need to know ahead of time what unit type it's going
to be? I suspect no as I currently can't imagine a way that would be
possible...but since I'm still new to STL I figured I'd ask.
Thanks,
--
Stephan
2003 Yamaha R6
å›ã®ã“ã¨æ€ã„出ã™æ—¥ãªã‚“ã¦ãªã„ã®ã¯
å›ã®ã“ã¨å¿˜ã‚ŒãŸã¨ããŒãªã„ã‹ã‚‰