Handling Physical Dimensions in Ada


There has always been a demand to be able to compute with physical items where dimensional correctness is checked. So I searched the web for such methods and presented a page where four such methods were compared. This finally gave rise to a paper by Dmitry A. Kazakov, Fraser Wilson and me presented at the Ada Europe Conference 2003 in Toulouse:

Ada Europe Conference 2003

Download here as a zipped file holding the paper in PDF and the MS PowerPoint presentation:

Christoph Grein, Dmitry A. Kazakov, Fraser Wilson
A Survey of Physical Unit Handling Techniques in Ada

14.05.2004  Erratum added

The conference proceedings have appeared at Springer-Verlag:

© Springer-Verlag
Jean-Pierre Rosen, Alfred Strohmeier (Eds)
Reliable Software Technologies - Ada-Europe 2003
8th Ada-Europe International Conference on Reliable Software Technologies
Toulouse, France, June 16-20, 2003
Lecture Notes in Computer Science, Vol. 2655, Springer-Verlag, 2003

The proceedings are also available at Springer-Verlag's web site, but you need to be a subscriber in order to read the full texts:

Ada 95 Issue 324

There exists also the Ada Issue 324, an Ada0Y proposal by the Ada Rapporteur Group for handling physical dimensions. Unfortunately I could not include my view on it in the conference proceedings because the final submission date for papers had just expired when the ARG proposal was published. So a very short outline thereof is included only in the PowerPoint presentation. The proposal exhibits a very clever use of generics and generic package parameters, so it is worth reading just for the expressive power of Ada.

However because of the tremendous number of instantiations needed, it is yet another demonstration that Ada is not suited to doing unit checking during compile time. It is therefore no longer pursued by the ARG.

You can find details directly in the Ada Issues Database of the Ada Conformity Assessment Authority.

A C++ solution, which is very similar to the Ada one presented here - it does however not include fractional powers - was presented at http://www.fnal.gov/docs/working-groups/fpcltf/. Currently it's only mentioned at the end of the page, but the link is broken. The big difference is that C++ templates allow type checking during compile-time, so that no overhead neither in memory space nor in runtime is incurred. In this respect, C++ templates are more powerful than Ada generics. However when the code was still there (11 February 2004), not all C++ compilers, although capable of compiling the code, were able to actually perform this type checking (as stated in the documentation of the method).

(Mind you, I do not say that Ada should have this feature of implicit instantiations - nor do I say she should not.)

Two of the methods presented in Toulouse can also be found here.

From the Big Bang to the Universe

This paper presents a method which allows dealing with physical quantities without operator overloading as far as possible while keeping the advantages of strong typing in critical cases. Basic mathematical functions like the square root and the sine in degrees and radians are included as predefined operations of the numeric base types.

The Deutsch German version (last editorial update 23rd November 2004, a ready to use version was added) of this paper has originally been published in Ada Aktuell 1.1 (March 1993), the deceased periodical of Ada Germany.

The English English version (last editorial update 23rd November 2004) is the translation of a still older German version and hence not as elaborate.

The method presented has successfully been in use for many years in several avionics projects under hard real-time conditions.

SI Units Checked and Unchecked

This paper (last functional update of the software 28th April 2008) presents a method which handles physical dimensions completely checked in full generality by allowing arbitrary rational powers of items. It is based on the seven SI units and includes any standard units derived from them as well as all prefixes like kilo and milli. Other non-coherent units like foot or nautical miles can easily be added. Basic mathematical functions like exp and sine are also available.

A further feature is dimensional Text_IO, e.g. 1.0*km/h.

The method comes in two variants: Dimensions as simple components or as discriminants, the latter constraining variables to a fixed dimension.

By changing only a few lines of code, dimension checking can be switched off in the released code (however big it may be), thus reducing the overhead of dimension checking to null.

This work has originally been published in Softwaretechnik-Trends Vol. 22.4 (November 2002), the periodical of Ada-Deutschland, special interest group 2.1.5 Ada within Gesellschaft für Informatik, the German Informatics Society.

Ada 2012: Physical units with GNAT (GPL 2013)

The new Ada 2012 aspect clauses have been appropriated by AdaCore to enhance their GNAT compiler with the ability to deal with physical units during compile-time. The notation is very natural and much resembles the one of the paper above, SI Units Checked and Unchecked.

See here a paper titled Physical units with GNAT (in PDF format), which presents a critical review of the achievements and shortcomings of this method as of GNAT GPL 2013, which has appeared in the Ada User Journal 35.1, March 2014.

This ingenious method could evolve to finally be included in the next Ada standard, whenever this might be.

English Home Contents
Deutsch Inhaltsverzeichnis
welcome homepage

Valid XHTML 1.0 Transitional!