[comp.lang.ada] Ada decimal elaboration, further experimentation.

CONTR47@NOSC-TECR.ARPA (07/04/88)

Norm Cohen writes:
Sam Harbaugh expects the addition in

  my_dime : dollar_type :=
  0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01;
--   1      2      3      4      5      6      7      8      9     10

to be performed using universal_real addition rather than dollar_type
addition.  Actually, it is performed using dollar_type addition:  Each
real literal is implicitly converted to dollar_type and the ten resulting
dollar_type values are summed.

If Sam had written

  my_dime : constant :=
    0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01;

then exact universal_real addition would have been performed.

The Ada rule is that conversion takes place only at the bottom of the
expression tree: Only numeric literals, named numbers, and attributes
with universal results are convertible.

The rule is given in RM paragraph 4.6(15).  Paragraph 4.6(20) provides
enlightening examples.

Norman Cohen
IBM Research

------------------------------
Wellll. I tried Norm's suggestion (assuming he meant
"constant dollar_type" instead of just "constant") so that
my_dime : constant dollar_type := 0.01 + 0.01 etc. for a total of ten 0.01s
Janus said my_dime is worth 0.08
Alsys said my_dime is worth 0.10
Dec said my_dime is worth  0.08
How about other people trying this in case I'm
missing something and also to see how other
compilers handle this.
regards,sam harbaugh
-------------------

dik@cwi.nl (Dik T. Winter) (07/04/88)

In article <8807031845.AA01533@ajpo.sei.cmu.edu> CONTR47@NOSC-TECR.ARPA writes:
 > Norm Cohen writes:
...
 > The Ada rule is that conversion takes place only at the bottom of the
 > expression tree: Only numeric literals, named numbers, and attributes
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 > with universal results are convertible.
 > ------------------------------
 > Wellll. I tried Norm's suggestion (assuming he meant
 > "constant dollar_type" instead of just "constant") so that
 > my_dime : constant dollar_type := 0.01 + 0.01 etc. for a total of ten 0.01s
...
 > How about other people trying this in case I'm
 > missing something and also to see how other

Yes, you are.  Norm did *not* mean constant dollar_type, but just constant.
You still are trying to get an implicit conversion from a universal real
expression to something of type dollar_type.  This is not possible, as noted
by Norm.

Oh, BTW, our system (DG) gives:
My dime is .102
Your dime is .078
which is wrong on two counts.
-- 
dik t. winter, cwi, amsterdam, nederland
INTERNET   : dik@cwi.nl
BITNET/EARN: dik@mcvax

murphy@beatnix.UUCP (Michael Murphy) (07/06/88)

The more serious problem here which is what *really* causes the bad
numbers is that most (all?) Ada compilers only support fixed point numbers
that are a power of two.  So despite the type declaration with a
delta of 0.01, the actual delta used is 0.0078125 (2e-7).  This makes
decimal arithmetic using fixed point unworkable.

-- Michael Murphy

P.S.  The VADS 5.5 compiler for the ELXSI prints out 0.10 for all
my_dime calculations (constant or not) because they are all done with 
universal arithmetic, but your_dime is 0.08 for the above reason.