comp.lang.ada
 help / color / mirror / Atom feed
* Ada decimal math, an example (or counterexample)
@ 1988-07-01 17:54 Norman Cohen
  0 siblings, 0 replies; 3+ messages in thread
From: Norman Cohen @ 1988-07-01 17:54 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 3+ messages in thread
* Re: Ada decimal math, an example (or counterexample)
@ 1988-07-06 18:48 Gary Morris @flash
  0 siblings, 0 replies; 3+ messages in thread
From: Gary Morris @flash @ 1988-07-06 18:48 UTC (permalink / raw)


> 
>   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

^ permalink raw reply	[flat|nested] 3+ messages in thread
* Ada decimal math, an example (or counterexample)
@ 1988-06-30 21:53 CONTR47
  0 siblings, 0 replies; 3+ messages in thread
From: CONTR47 @ 1988-06-30 21:53 UTC (permalink / raw)


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
----------------------

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~1988-07-06 18:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1988-07-01 17:54 Ada decimal math, an example (or counterexample) Norman Cohen
  -- strict thread matches above, loose matches on Subject: below --
1988-07-06 18:48 Gary Morris @flash
1988-06-30 21:53 CONTR47

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox