comp.lang.ada
 help / color / mirror / Atom feed
From: "bubble" <bubble@bubble.d2g.com>
Subject: Re: Ada begginer's new problem.
Date: Fri, 29 Oct 2004 10:58:48 +0800
Date: 2004-10-29T10:58:48+08:00	[thread overview]
Message-ID: <clsc7a$i3l$1@netnews.hinet.net> (raw)
In-Reply-To: 2ud2i7F29ahnhU1@uni-berlin.de



>
> In the above construction, it appears execute_some_Statement_here5 can
> never be executed. Did you make a mistake?
For example, I want print all prime number between 1 to 1000;
I rewrite java version prime example to ada version.

The code is

WITH ada.Text_IO;
PROCEDURE primeNum is
   PACKAGE sout is new ada.Text_IO.Integer_IO(integer);
   test:integer:=1;
begin
   sout.put(1);
   ada.Text_IO.New_Line;
   loop
      test := test+2;
      EXIT when test >1000;
      FOR fac in 2..test-1 loop
         IF test mod fac =0 THEN
            goto continueLoop;
         END IF;
      end loop;
      sout.put(test);
      ada.Text_IO.New_Line;
      <<continueLoop>>
             null;
   end loop;
end primeNum;

now I change it to another version.
It seem better.

WITH ada.Text_IO;
PROCEDURE primeNum is
   PACKAGE sout is new ada.Text_IO.Integer_IO(integer);
   test:integer:=1;
   FUNCTION isPrime(test:integer) return boolean is
   begin
     FOR fac in 2..test-1 loop
         IF test mod fac =0 THEN
            RETURN false;
         END IF;
      end loop;
      RETURN true;
   end isPrime;
begin
   sout.put(1);
   ada.Text_IO.New_Line;
   loop
      test := test+2;
      EXIT when test >1000;
      IF isPrime(test)=true THEN
         sout.put(test);
         ada.Text_IO.New_Line;
      END IF;
   end loop;
end primeNum;

thank jacob,larry,eric,nick,
your suggestions are very useful.


> Anyway, my personal advice would be not to try to eliminate gotos that
> are difficult to eliminate. I think it is nearly always better (clearer,
> easier to understand) to leave them in.
>
> > I know there are some choices can apply to my program.
> > ( a ). Type stock_price is new float;
> > ( b ). Type stock_price is digits 5;
> > ( c ). Subtype stock_price is float;
> > ...
> > if we don't consider ( c ) style, only  consider (a) and (b).
> > which style is best?
>
> I suggest you use a decimal fixed-point type for a stock price. It has
> absolute accuracy. For example:
>
>    type Stock_Price_in_Dollars is
>       delta 0.01 digits 10 range 0.00 .. 99_999_999.99;
>
> This requests a type which is always accurate to the cent, and can
> represent values up to (but not including) $100mil.
>
> It is usually better to declare types with an explicit request of the
> properties you require of them. It may be possible for a pre-existing
> type to not have these properties.
>
> I think it is possible that you might want a more general-purpose type
> to do all your currency calculations, and to use subtypes to impose
> appropriate ranges. For example:
>
>    type Dollars is delta 0.01 digits 10;
>
>    subtype Stock_Price_in_Dollars is
>       Dollars range 0.00 .. 99_999_999.99;
>
>    subtype Price_Differential_in_Dollars is
>       Dollars range -99_999_999.99 .. 99_999_999.99;
>
> This way, you do not have the (probably inapproprate) inconvenience of
> doing a lot of conversions to do mixed arithmetic:
>
>    Old_Price, New_Price: Stock_Price_in_Dollars;
>    Price_Change: Price_Differential_in_Dollars;
>    ...
>    New_Price := Old_Price + Price_Change;
>
> > 3. possible assign a IEEE754 NaN value to float type variable?
>
> It is implementation defined how to do this sort of thing. If you look
> in the reference manual for your Ada compiler, you may find that there
> is a method (for example, an implementation-defined attribute).
>
> -- 
> HTH
> Nick Roberts





  reply	other threads:[~2004-10-29  2:58 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-28  8:11 Ada begginer's new problem bubble
2004-10-28  8:44 ` Jacob Sparre Andersen
2004-10-28 10:38 ` Larry Kilgallen
2004-10-28 11:26 ` Eric Jacoboni
2004-10-28 20:19 ` Nick Roberts
2004-10-29  2:58   ` bubble [this message]
2004-10-29  2:47 ` Steve
replies disabled

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