[comp.lang.ada] Ada decimal math, an example

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

with text_io; use text_io;
procedure decimal is
  type dollar_type is delta 0.01 range 0.00..1000.00;
  package decimal_io is new fixed_io(dollar_type); use decimal_io;
  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
  your_dime : dollar_type := 0.0;
begin
  for i in 1..10 loop
    your_dime := your_dime + 0.01;
  end loop;
  put("--  my_dime is worth "); put (	my_dime ); new_line;
  put("--your_dime is worth "); put ( your_dime ); new_line;
end decimal;

-- Janus 2.0.2 prints:
--  my_dime is worth     0.08   (Janus apparently isn't doing universal math)
--your_dime is worth	 0.08

-- Alsys v3.2 prints:
--   my_dime is worth	 0.10
-- your_dime is worth	 0.08

-- Verdix (VADS Version 5.41 ) prints:
--  my_dime is worth 0.10
-- your_dime is worth        0.07 (apparently it truncates on I/O)
--Dec version 1.3 prints:
--  my_dime is worth    0.08  (apparently isn't doing universal math)
-- your_dime is worth   0.08

--Since Dec says my_dime is worth 0.08 I now question whether
--Universal math is required for the initialization. Maybe
--only Alsys is doing the my_dime computation at compile time
--and therefore using Universal math and the others are doing
--it at run time using model numbers. What do you experts say?
--I formally withdraw my suggestion to perform computation
--in the declarative region in order to have it done by
--universal math. Sigh.
--regards,sam harbaugh
----------------------

NCOHEN@IBM.COM (Norman Cohen) (07/02/88)

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

garym@telesoft.UUCP (Gary Morris @flash) (07/07/88)

> 
>   type dollar_type is delta 0.01 range 0.00..1000.00;
> ...
>   my_dime : dollar_type :=
>   0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01 + 0.01;
> ...
These calculations are done using the dollar_type which has a 'small of
0.0078125.  For the use you are making of this type, you should use a rep
spec to make 'small be the same as the delta.  The TeleSoft TeleGen2
compiler supports this:

  type dollar_type is delta 0.01 range 0.00..1000.00;
  for dollar_type'small use 0.01;

With the rep spec our Sun Ada 1.1 compiler produces the "expected" results:

--  my_dime is worth     0.10
--your_dime is worth     0.10

Without the rep spec our Sun Ada 1.1 compiler does the computation using
a 'small of 0.0078125 yielding 0.078125, which is rounded to 0.08 for
output:
--  my_dime is worth     0.08
--your_dime is worth     0.08

Gary Morris		UUCP:	ucbvax!ucsd!telesoft!garym
TeleSoft, San Diego		telesoft!garym@ucsd.edu
(619) 457-2700		ARPA:	ucsd!telesoft!garym@ucbvax.ARPA