From: | "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> |
Newsgroups: | comp.compilers |
Date: | 28 Apr 2005 14:32:01 -0400 |
Organization: | cbb software GmbH |
References: | 05-04-067 |
Keywords: | theory |
Posted-Date: | 28 Apr 2005 14:32:01 EDT |
On 26 Apr 2005 20:43:21 -0400, Ron Foster wrote:
> I am working on a small project to evaluate and execute unit conversion
> expressions. I started wondering whether it was possible to determine what
> the reverse conversion rules might blook like. ...
> The problem appears to lie in the domain of ( one to one ) function
> inversion or maybe equation solving.
It all depends on the scales. F and C are linear scales of measurements. So
to convert a value from one scale to another you need a linear function.
You can have logarithmic scales or more complex ones.
> I presume that such an algorithm has been implemented a hundred or so times,
> but my research is coming up dry.
> I'd appreciate it if someone could point me to some relevant material.
http://www.dmitry-kazakov.de/ada/units.htm
(it also contains a unit converter sample code)
Basically the approach is to use one scale where possible. That makes life
a lot easier, especially because it allows unit expressions evaluation.
(The pitfall is evaluation accuracy. As long as you are using
floating-point, that's is hardly an issue, but should you switch to fixed
point that would become a problem.)
Basically each scale entails a whole unit system. It is difficult to live
with that. One could say that was the reason why SI was invented. Foot,
lightyear and everything else can be represented in SI. So you just write;
1.5 * foot + 10.3 * meter -- This is fine
and the result is evaluated in SI units. You can get its numeric equivalent
in yards or furlongs if you want to. Also the factor is not an issue.
With C and F the picture becomes more complex. As I said above they
comprise scales of shifted units. Shifted units form a group for each value
of shift. Note a group, not a field, so they cannot be multiplied to
anything but a dimensionless value:
C / s -- Meaningless
If temperature delta was meant then it should have been
K / s
Units of different shifts cannot be added:
C + F -- Illegal, because ambiguous, what's the result's shift?
Logarithmic scales could be handled in a similar way.
For parsing infix expressions (with or without units) see:
http://www.dmitry-kazakov.de/ada/components.htm
--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Return to the
comp.compilers page.
Search the
comp.compilers archives again.