views:

61

answers:

1

Hi, 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...

+2  A: 

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.

trashgod
Thanks. If I understand correctly then I don't need to add the Long_Float. The expression that you gave is sufficient for the compiler to know what precision to use.
yCalleecharan
I use the GNAT GPL compiler from Adacore. If I understand correctly then this GNU GMP and MPFR numerical libraries are for GCC only. Am I right?
yCalleecharan
@yCalleecharan: Yes and no, respectively; see above.
trashgod
@yCalleecharan: Oops, I misunderstood the second question. I know the binding works with GNAT, but it should also work with other compilers that support linking with GCC binaries.
trashgod
Thanks. So it's yes for the first and yes/no for the second :). I shall try to check if this binding works with GNAT from Adacore.
yCalleecharan
@yCalleecharan: I know the example works with GNAT 4.3.4.
trashgod
Thanks for this information.
yCalleecharan
Named numbers: learn 'em, love 'em, use 'em.
T.E.D.