Re: [boost] Quantity library: any interest?

2003-04-30 Thread Terje Slettebø
From: [EMAIL PROTECTED]

  Even if angle is added as a dimension, to an implementation using an
  integer vector, it still wouldn't accommodate any other dimensions
  added later, without rewriting the library and unit definitions.

 how about having the basic SI dimensions and a couple of extra
 to-be-defined-by-the-developer dimensions. In that case, full
 SI compliance and for those who want something extra they can have
 it. I saw one of the discussed unit libraries provided something
 like a dimension to be defined by the user/developer.

I think I know a way this may be done, now: Default parameters.

There's several ways of specifying a unit; one is to use a typelist, such as
typedef mpl::vector_c1,0,0,0,0,0,0 kg (Dave Abrahams also showed this
approach at the recent ACCU conference), or use template parameters for the
dimensions. In both ways, defaults may be used (it's used internally for
mpl::vector, anyway). In fact, all could be defaulted (like mpl::vector),
giving in this case a dimensionless unit.

Named template parameters could be a possibility, as well, so you could do
typedef unitkg, m, s-2  newton.


Regards,

Terje

___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Re: [boost] Quantity library: any interest?

2003-04-27 Thread Terje Slettebø
From: David Abrahams [EMAIL PROTECTED]

 These are my (only slightly informed) opinions.  I've heard Walter
 Brown talk about angle in this context, which was a big influence.

 Terje Slettebø [EMAIL PROTECTED] writes:

  Regarding this angle dimension, should it be treated like the other
  SI-dimensions? That is, say that you represent an SI quantity/unit
  with an integer vector giving the exponents:
 
  templateint kg,int m,int s,int A,int K,int mol,int cd,int angle
  class quantity;
 
  If you multiply two quantities, you multiply the value and add the
  exponents, so quantity0,1,0,0,0,0,0,0(10) *
quantity0,1,0,0,0,0,0,0(10)
  = quantity0,2,0,0,0,0,0,0(100) (m * m = m^2)
 
  Would this hold for angle, as well?

 Yes. Angle is a dimensionless scalar (length/length).  All its
 exponents are zero.

Yes, Renej (in the posting I replied to) also pointed out that angle is a
dimensionless quantity. However, in their real-life experience, they found
it useful not to have it dimensionless, but instead having it as an
additional dimension, to be able to distinguish things like velocity and
angular velocity. It was in this context that I wondered how angle - as a
dimension - should be treated.

  That is, does it make sense to say angle * angle = angle^2?

 Probably not, but only because angle * angle doesn't make much
 sense.  Does that ever come up in real life?

If it's a dimensionless quantity, it wouldn't matter (all exponents would
still be zero). However, as a dimension, it would give angle^2. That's what
I wondered about.

  I understand that e.g. angle/s (angular velocity) makes sense, but
  should a library allow any combination with angle and the other
  dimensions?

 Not arbitrarily:

I meant - should it allow the same combinations as the other dimensions are
allowed. That is (simplified, using only one dimension):

quantityd1 * quantityd2 = quantityd1+d2
quantityd1 / quantityd2 = quantityd1-d2
quantityd + quantityd = quantityd
quantityd - quantityd = quantityd

 angle(pi/2) / mass(40);  // OK
 angle(pi/2) + mass(40);  // error

Let's again cast it in the quantity example template I gave in the last
posting, to examine it. Let's say that the angle is expressed in radians. We
then have:

templateint kg,int m,int s,int A,int K,int mol,int cd,int rad
class quantity;

typedef quantity1,0,0,0,0,0,0,0 mass;
typedef quantity0,0,0,0,0,0,0,1 angle;

angle(pi/2) * mass(40) = quantity1,0,0,0,0,0,0,1(pi/2*40) (rad * kg)
angle(pi/2) + mass(40)

The last one will be an error, as you said.

I'm guessing that the answer to my question is, yes, angle, when expressed
as a dimension, may be treated like the other dimensions.

There's also a question of what then if someone would like to add a money
dimension, to avoid having money be interchangeable with dimensionless
quantities. However, in this case, multiplying currencies with each other
really doesn't make any sense (e.g. USD(10)*USD(10) = USD^2?), which _would_
be allowed, if it used the same framework as above. Therefore, maybe money
is better treated in a different way, than with an exponent.


Regards,

Terje

___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Re: [boost] Quantity library: any interest?

2003-04-27 Thread Terje Slettebø
From: [EMAIL PROTECTED]

 we use the int-based template approach for a couple of years now in
 our AGV controller software. We actually sometimes reach the stage that
 it works when succesfully compiled and linked. Since our control software
 is physics throughout (field of robotics), the type safety is very
 important. However, besides the basic SI units we also have 'angle'
 as a dimension which allows us to distinguish 'velocity' and 'angular
 velocity' for example. Hence, from out 'real user' experience (engineering
 point of view) it would be a necessity to add 'angle' as a dimension
 without breaking already defined quantities. Most (all?) units libraries
 already define 'angle' to be dimensionless, which is true in
 scientifically spoken, but pragmatically (engineering ;-) less handy.

There's another question. If we add angle as a dimension, then what kind of
angle is it? There are several kinds of angles, such as radians (plane
angle) and steradians (solid angle). If both were represented by the same
angle dimension, then it probably wouldn't make much sense to add radians
and steradians (and what would be the resulting quantity?), yet, the library
would allow it.


Regards,

Terje

___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Re: [boost] Quantity library: any interest?

2003-04-27 Thread David Abrahams
Terje Slettebø [EMAIL PROTECTED] writes:

From: David Abrahams [EMAIL PROTECTED]

 These are my (only slightly informed) opinions.  I've heard Walter
 Brown talk about angle in this context, which was a big influence.

 Terje Slettebø [EMAIL PROTECTED] writes:

  Regarding this angle dimension, should it be treated like the other
  SI-dimensions? That is, say that you represent an SI quantity/unit
  with an integer vector giving the exponents:
 
  templateint kg,int m,int s,int A,int K,int mol,int cd,int angle
  class quantity;
 
  If you multiply two quantities, you multiply the value and add the
  exponents, so quantity0,1,0,0,0,0,0,0(10) *
 quantity0,1,0,0,0,0,0,0(10)
  = quantity0,2,0,0,0,0,0,0(100) (m * m = m^2)
 
  Would this hold for angle, as well?

 Yes. Angle is a dimensionless scalar (length/length).  All its
 exponents are zero.

 Yes, Renej (in the posting I replied to) also pointed out that angle is a
 dimensionless quantity. However, in their real-life experience, they found
 it useful not to have it dimensionless, but instead having it as an
 additional dimension, to be able to distinguish things like velocity and
 angular velocity. 

They are distinguished!

Velocity is l/t and angular velocity is 1/t - the same as frequency.
Makes perfect sense to me.

 It was in this context that I wondered how angle - as a
 dimension - should be treated.

  That is, does it make sense to say angle * angle = angle^2?

 Probably not, but only because angle * angle doesn't make much
 sense.  Does that ever come up in real life?

 If it's a dimensionless quantity, it wouldn't matter (all exponents
 would still be zero). However, as a dimension, it would give
 angle^2. That's what I wondered about.

  I understand that e.g. angle/s (angular velocity) makes sense, but
  should a library allow any combination with angle and the other
  dimensions?

 Not arbitrarily:

 I meant - should it allow the same combinations as the other dimensions are
 allowed. That is (simplified, using only one dimension):

 quantityd1 * quantityd2 = quantityd1+d2
 quantityd1 / quantityd2 = quantityd1-d2
 quantityd + quantityd = quantityd
 quantityd - quantityd = quantityd

 angle(pi/2) / mass(40);  // OK
 angle(pi/2) + mass(40);  // error

 Let's again cast it in the quantity example template I gave in the last
 posting, to examine it. Let's say that the angle is expressed in radians. We
 then have:

 templateint kg,int m,int s,int A,int K,int mol,int cd,int rad
 class quantity;

 typedef quantity1,0,0,0,0,0,0,0 mass;
 typedef quantity0,0,0,0,0,0,0,1 angle;

 angle(pi/2) * mass(40) = quantity1,0,0,0,0,0,0,1(pi/2*40) (rad * kg)
 angle(pi/2) + mass(40)

 The last one will be an error, as you said.

 I'm guessing that the answer to my question is, yes, angle, when expressed
 as a dimension, may be treated like the other dimensions.

I guess I can't play in that mind-space, because I can't get away from
what seems to me like a clear truth: angle is dimensionless.  If you
give it dimension, you'll get confusing results (like no relationship
between angular velocity and frequency).  I wonder what happens to
physics calculations when frequency is expressed as rad/t?

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


Re: [boost] Quantity library: any interest?

2003-04-27 Thread renej
From: [EMAIL PROTECTED]

 we use the int-based template approach for a couple of years now in
 our AGV controller software. We actually sometimes reach the stage
 that it works when succesfully compiled and linked. Since our control
 software is physics throughout (field of robotics), the type safety is
 very important. However, besides the basic SI units we also have
 'angle' as a dimension which allows us to distinguish 'velocity' and
 'angular velocity' for example. Hence, from out 'real user' experience
 (engineering point of view) it would be a necessity to add 'angle' as
 a dimension without breaking already defined quantities. Most (all?)
 units libraries already define 'angle' to be dimensionless, which is
 true in
 scientifically spoken, but pragmatically (engineering ;-) less handy.

 There's another question. If we add angle as a dimension, then what
 kind of angle is it? There are several kinds of angles, such as radians
 (plane angle) and steradians (solid angle). If both were represented by
 the same angle dimension, then it probably wouldn't make much sense to
 add radians and steradians (and what would be the resulting quantity?),
 yet, the library would allow it.

in our implementation (plane) angle is sufficient, so we don't bother
with solid angle for the moment. However, I think it should have it's
own dimension since steradian != radian^2 (although m^2/m^2 = (m/m)^2 ;-)

but I think a unit library would reach a much broader audience if it allows
to choose what dimensions are handled and how. For my application field
it is necessary to incorporate everything related to robotics (time,
length, angle, charge, mass), but temp, luminious intensity, amount of
substance not really. I think it would be nice to be able to select only
those you need in a unit lib and add your own dimensions if needed

renej



 Regards,

 Terje

 ___
 Unsubscribe  other changes:
 http://lists.boost.org/mailman/listinfo.cgi/boost



___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


[boost] Quantity library: any interest?

2003-04-12 Thread Pavel Vozenilek
I wrote Quantity library some time ago and wonder whether it may get added
into Conversion library. If anyone gets interested I will Boostify and
submit it.


Quantity library is used to get safe conversions between different physical
units (like between Celsius and Farhernheit degrees). Instead of bare number
you have specific type and conversion rules are applied automatically.

Example:

typedef struct { float value; } speed_km_per_hour_t;

typedef Quantity

float,   // numeric type to represent speed in meter/sec
speed_km_per_hour// alternative representation
 speed_meters_per_sec_t;

// conversion rule: define them when it makes sense
void convert_quantity(const speed_meters_per_sec_t from,
speed_km_per_hour_t to)
{
to.value = from.value * 3.6;
}

int main()
{
speed_meters_per_sec_t m_p_s(10);
speed_km_per_hour_tkm_p_h;

km_p_h = m_p_s;// OK
//   m_p_s = km_p_h;   ERROR - this conversion is not defined
km_p_h = static_castspeed_km_per_hour_t(m_p_s); // OK
}


You can manipulate Quantity value like any other value (add, multiply,
compare), you may add  e.g. 1 inch to value of 2.1 meters.  This library
helps to avoid really hard to track down conversion errors.

Some conversions may not be needed: e.g. $1.5 money may be converted to
string one dollar, fifty cents but not vice versa. Unimplemented and used
conversions result in (relatively) readable linker error.

Ability e.g. to divide length quantity by time quantity and get speed
quantity may be added into the library.


The library is simple and uses Boost.Preprocessor to emulate variable number
of template parameters.

-
Shortened snippet of implementation:

templatetypename MainType, typename OtherType1
class Quantity
{
public:
Quantity() {}
Quantity(const Quantity other) : value(other.value) {}
Quantity(const MainType val) : value(val) {}
Quantity(const OtherType1 val) { convert_quantity(val, *this); }

~Quantity() {}

Quantity operator=(const Quantity other) { value = other.value; return
*this; }
Quantity operator=(const MainType other) { value = other; return
this*; }
Quantity operator=(const OtherType1 other) { convert_quantity(other,
*this); return *this; }

operator MainType() { return value; }
operator OtherType1() { OtherType1 result; convert_quantity(*this,
result); return result; }

// operators like ==, /, * etc. not shown here

MainType value;
};
-

Name and inspiration was taken from Martin Fowler's article
http://www.martinfowler.com/ap2/quantity.html.

/Pavel




___
Unsubscribe  other changes: http://lists.boost.org/mailman/listinfo.cgi/boost