comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Expressing physical units (Was: Reprise: 'in out' parameters for functions)
Date: Thu, 15 Apr 2004 12:37:49 +0200
Date: 2004-04-15T12:37:49+02:00	[thread overview]
Message-ID: <c5lomm$33qke$4@ID-77047.news.uni-berlin.de> (raw)
In-Reply-To: 1081961393.799312@master.nyc.kbcfp.com

Hyman Rosen wrote:

> Dmitry A. Kazakov wrote:
>> But C++ does not allow double as a template parameter.
> 
> That's merely an implementation detail; you simply use a
> pair of integers to represent a fraction.

Ooch.

>> Why? It is no matter. Shifted units represent linear scales:
>> x = a * y + b
>> The dimension of a and b may have any powers.
> 
> No matter? What is the meaning of multiplying degrees Celsius
> by degrees Farenheit,

It is ambigous. But you can multiply Kelvin by Kelvin. For example, heat
flux has the dimension:

m**2 * kg / K**3

> or squaring degrees Celsius?

>> Because there can be a universal solution with zero-overhead in static
>> case.
> 
> Zero overhead in space as well? I assume this is another one of those
> cases which will require object tags to live in the classwide pointers
> instead of in the objetcs themselves?

Right. The idea is to have the powers as a discriminant and unit arithmetics
as pure routines. When the discriminant (unit) is statically known, it is
removed and we have a full equivalent of the template solution. When it is
unknown, we have run-time checks with space penalty. I belive it is a
doable approch, which has an additional advantage, that this mechanism
could be made available for all user types.

>> (:-))
> 
> Why is my unit output procedure funny?

It is still static and may no input counterpart.

>> Not that is the problem. One can add 1 [mm] and 1 [cm], it is legal. The
>> question is in which unit system the arguments are and the result will
>> be.
> 
> You need to guide the type of the result, since it could be either.
> So it's simpler to forbid the addition of differently scaled units,
> and require the user to convert one operand to the type of the other.

This does not solve the problem. These conversions either explicit or
implicit has to be done and thus checked. We have a geometrically exploding
number of cross conversions.
 
> Or, given that you're doing it in C++, it's easy to write traits
> templates that would represent rules for preferring one scale over
> another.

Too uncontrollable. The choice depends on the target value, so one cannot
say if lightyear is better than micrometer.

>> I do not see how you could add x in meters of SI with y in ft of
>> logarithmic scale using specialized templates. To represent scale you
>> will probably need some pointer to the scale descriptor in your template.
> 
> You represent scale and logness as template parameters. Then you just
> need to be able to convert one type to another (and if we're talking
> about addition of log-scaled quantities, we have to convert out anyway)
> for addition, and to write a specialized multiplication routine that
> takes two log-scaled units and adds their values, for example.

You have to do it for each of seven powers, representing each parameter by a
number of integers. I am afraid that the list of template arguments will be
longer than the source code. To have enough RAM to compile it, you would
need to made sand of all beaches into memory chips. And I am not sure if
the compiler will finish before the Entropic Death (:-))

> I doubt that anyone is going to build such a complicated system of
> units, but it could be done in principle.

Yes, it is Turing complete. (:-))

>> I do not see any way of making a GUI or DB interface based on templates.
>> Consider a distributed system with should have a communication protocol
>> to exchange physical measures. How would you do it using templates?
> 
> As I have already said, we are relying on knowing a fixed set of units
> within the program. Then the problem is no different than communicating
> any other fixed set of record types.

The problem is that you cannot have one piece of code dealing with all units
(though from a fixed set). You probably will be able to create a template
for this code, but the number of variants will be far greater than any
computer may hold. You need to instantiate all of them, because you do not
know which of them will be used at tun time.

And after all, who will be able to maintain this code? Finding an error in
C++ template instantiations is a work requiring a lot of experience, much
patience and great luck. And we want to give it engineers, who else needs
units so much? Even in Ada dealing with generics imposes much more
difficulties for a programmer than regular code. The price is to high.

>> Consider a large finite element library. If that has to be used for
>  > physical values, then the whole library has to be generic.
> 
> So?

Absolutely unrealistic.

-- 
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



  reply	other threads:[~2004-04-15 10:37 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <u4qrv5hwr.fsf@acm.org>
2004-04-08 17:19 ` Reprise: 'in out' parameters for functions Alexander E. Kopilovich
     [not found] ` <bRecOT0TxF@VB1162.spb.edu>
2004-04-08 23:46   ` Stephen Leake
2004-04-09  9:23     ` Florian Weimer
2004-04-09 10:04       ` Dmitry A. Kazakov
2004-04-09 11:23         ` Martin Krischik
2004-04-09 12:44           ` Dmitry A. Kazakov
2004-04-09 22:48             ` Randy Brukardt
2004-04-14 14:40               ` Robert I. Eachus
2004-04-14 21:20                 ` Randy Brukardt
2004-04-09 22:47         ` Florian Weimer
2004-04-10 10:49           ` Dmitry A. Kazakov
2004-04-10 11:11             ` Florian Weimer
2004-04-10 13:26               ` Dmitry A. Kazakov
2004-04-10 20:50                 ` Georg Bauhaus
2004-04-11 10:31                   ` Dmitry A. Kazakov
2004-04-09 11:27       ` Stephen Leake
2004-04-09 22:46       ` Randy Brukardt
2004-04-09 13:12     ` Wojtek Narczynski
2004-04-09 15:48       ` Expressing physical units (Was: Reprise: 'in out' parameters for functions) Jacob Sparre Andersen
2004-04-10 13:07         ` Wojtek Narczynski
2004-04-10 13:52           ` Jacob Sparre Andersen
2004-04-11  2:45             ` Hyman Rosen
2004-04-11 10:14               ` Expressing physical units Jacob Sparre Andersen
2004-04-11 16:05                 ` Hyman Rosen
2004-04-12  6:58               ` Expressing physical units (Was: Reprise: 'in out' parameters for functions) Russ
2004-04-12 10:29                 ` Dmitry A. Kazakov
2004-04-13  6:52                   ` Russ
2004-04-13 10:55                     ` Dmitry A. Kazakov
2004-04-14  4:50                       ` Hyman Rosen
2004-04-14  8:49                         ` Dmitry A. Kazakov
2004-04-14 16:49                           ` Hyman Rosen
2004-04-15 10:37                             ` Dmitry A. Kazakov [this message]
2004-04-14  7:10                       ` Russ
2004-04-14  8:53                         ` tmoran
2004-04-14  9:01                           ` Vinzent 'Gadget' Hoefler
2004-04-14  9:21                         ` Dmitry A. Kazakov
2004-04-13  9:53             ` Wojtek Narczynski
2004-04-15 21:27               ` Expressing physical units Jacob Sparre Andersen
2004-04-16 11:40                 ` Dmitry A. Kazakov
2004-04-09 16:17       ` Reprise: 'in out' parameters for functions Georg Bauhaus
2004-04-10  2:28         ` Wojtek Narczynski
2004-04-10  9:46           ` Georg Bauhaus
2004-04-10 10:49           ` Dmitry A. Kazakov
2004-04-10 15:35             ` Wojtek Narczynski
2004-04-10 21:01               ` Georg Bauhaus
2004-04-10 21:16               ` Georg Bauhaus
2004-04-11 13:20                 ` exception parameters Stephen Leake
2004-04-12 10:29                   ` Dmitry A. Kazakov
2004-04-13  0:58                     ` Stephen Leake
2004-04-13  1:30                       ` Randy Brukardt
2004-04-13  8:04                   ` Jean-Pierre Rosen
2004-04-11 10:31               ` Reprise: 'in out' parameters for functions Dmitry A. Kazakov
2004-04-12 22:02                 ` Randy Brukardt
2004-04-13 10:56                   ` Dmitry A. Kazakov
2004-04-14 21:12                     ` Randy Brukardt
2004-04-15 10:37                       ` Dmitry A. Kazakov
2004-04-13  9:30                 ` Wojtek Narczynski
2004-04-13 12:00                   ` Dmitry A. Kazakov
2004-04-13 22:41                     ` Wojtek Narczynski
2004-04-14  8:49                       ` Dmitry A. Kazakov
2004-04-14 15:03                         ` Wojtek Narczynski
2004-04-15 10:37                           ` Dmitry A. Kazakov
2004-04-16  0:29                             ` Wojtek Narczynski
2004-04-16 11:36                               ` Dmitry A. Kazakov
2004-04-16 19:25                                 ` Wojtek Narczynski
2004-04-14 15:57             ` Robert I. Eachus
2004-04-15  8:04               ` Dmitry A. Kazakov
2004-04-10 12:32           ` Wojtek Narczynski
2004-04-14 15:46           ` Robert I. Eachus
2004-04-16  1:52             ` Wojtek Narczynski
2004-04-16  5:40               ` Robert I. Eachus
2004-04-16 11:38                 ` Wojtek Narczynski
2004-04-16 16:30                   ` Robert I. Eachus
2004-04-16 18:38                   ` Randy Brukardt
2004-04-16 22:15                     ` Wojtek Narczynski
2004-04-17  1:20                       ` Robert I. Eachus
2004-04-17 11:42                         ` Wojtek Narczynski
2004-04-17 14:14                           ` Robert I. Eachus
2004-04-16 19:28                   ` Wojtek Narczynski
2004-04-09 17:09       ` Pascal Obry
2004-04-10  2:37         ` Wojtek Narczynski
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox