开发者

Ada: constant declaration

I'm reading Norman Cohen's Ada 95 book and on page 129 we have the constant declarations:

 Pi: constant Float := 3.1415926536

and

Pi: constant := 3.1415926536

The second declaration is said to be interpreted as, I quote: "any floating-point of fixed-point type with the appropriate range". My question is say one is working with Long_Float precision, then for example to declare a constant say 2*PI do one has to specifically declare like

Two_Pi : CONSTANT Long_Float := 2.0 * 3.1415926536

or much better still:

Two_Pi: CONSTANT Long_Float :=  2.0 * Ada.Numerics.Pi;

(the second declaration to take advantage of more digits after the decimal point) ?

Would

Two_Pi : CONSTANT := 2.0 * 3.1415926536   

or better still

Two_Pi: CONSTANT  :=  2.0 * Ada.Numerics.Pi;

be as good as the book claims so that the Ada compiler would know for instance if I'm 开发者_如何学运维using Two_Pi in a Long_Float calculation, then the compiler would supply the required number of precision digits? Since the Pi value 3.1415926536 is not of Long_Float type (as it has fewer precision digits), I guess that the last declaration i.e.

Two_Pi: CONSTANT  :=  2.0 * Ada.Numerics.Pi;

would be all that is required if I need Two_Pi in a Long_Float calculation. Am I understanding right? With a similar understanding, then

Two_Pi: CONSTANT  :=  2.0 * Ada.Numerics.Pi;

would be relevant also in a Float type calculation and the compiler would supply only the required number of precision digits.

Thanks a lot...


Number Declarations such as

Two_Pi : constant := 2.0 * Ada.Numerics.Pi;

are sometime called named numbers. Such a number is universal "in that it is acceptable where some particular type in the class is expected (see 8.6)."

Addendum: Because such numbers are universal, they can "be used as operands with the primitive subprograms of any type in the corresponding class." For example, Two_Pi can be multiplied by Float, Long_Float or any type derived from universal_real.

On a related note, you might like this Ada binding to the GNU GMP and MPFR libraries.

Addendum: The binding allows one to use the GNU libraries from Ada, as seen in this example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜