From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,b5627b980414da76 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-03-15 07:50:33 PST Newsgroups: comp.lang.ada Path: bga.com!news.sprintlink.net!EU.net!chsun!mlma11.matrix.ch!user From: Mats.Weber@matrix.ch (Mats Weber) Subject: Re: Higher precision and generics Message-ID: Sender: usenet@eunet.ch Organization: ELCA Matrix SA References: <3jsnbf$ido@nef.ens.fr> Date: Wed, 15 Mar 1995 15:36:43 GMT Date: 1995-03-15T15:36:43+00:00 List-Id: In article <3jsnbf$ido@nef.ens.fr>, sands@clipper.ens.fr (Duncan Sands) wrote: > I am writing a package of matrix routines (in Ada!) in which some intermediate results > should be calculated in higher precision than normal. The type for normal precision is > called Real and is a generic parameter of my package: > generic > type Real is digits <>; > package Matrix_Stuff is > ... > end; > > In the body I would like to be able to declare Double to be a higher precision > floating point type, something like: > package body Matrix_Stuff is > type Double is digits 2*Real'Digits; > ... > end; > > Unfortunately, this does not compile: apparently formal types like Real are not > considered static, so Real'Digits is not considered static, and only static > expressions are allowed in a "type X is digits expression" declaration. (If > I write "type Double is digits 2*Float'Digits" then it compiles fine). > > What can I do to get type Double to be of higher precision than Real? > Can anyone please help? And does anyone know why formal types like Real are not > considered static? They are not considered static because the design of Ada's generics leaves the choice to the implementor as to how generics are implemented. Some implement generics as templates that are expanded at each instantiation, and others generate code when the generic itself is compiled. The latter approach implies that attributes of generic formal parameters cannot be static. The work-around in your case is to pass the higher precision type as a generic parameter, as follows: generic type Real is digits <>; type Double_Real is digits <>; package Matrix_Stuff is ... end; and then do the precision calculation when you instantiate: type My_Real is digits ...; type My_Double_Real is digits 2 * My_Real'Digits; -- 'Digits is static here -- unless My_Real is a -- generic formal. package My_Matrix_Stuff is new Matrix_Stuff(My_Real, My_Double_Real); In the body of the generic, you can, if you wish, make a check that the precision of the actual type is sufficient: package body Matrix_Stuff is ... begin if Double_Real'Digits < 2 * Real'Digits then raise Insufficient_Precision; end if; end Matrix_Stuff; Hope this help... Mats