[net.lang.ada] Numeric Conversion

anton@idec.stc.co.uk (Anton Gibbs) (06/11/86)

We are relatively new to using Ada and, whilst learning the  language,
have  used  as a guideline the principle that there always seems to be
an Ada way of doing things.  Whenever code we have written  turns  out
to be messy in Ada, subsequent analysis has revealed that a different,
better approach leads to a neater solution.  Usually it  is  the  data
modelling that is wrong.

Now we have encountered a problem, trivial by  nature,  which  has  us
baffled.   The problem is simply that of obtaining the integer part of
a positive fixed-point value.  We can find no simple way of  achieving
this  in Ada: use of numeric conversion leads to uncertainty regarding
the direction of rounding (ARM 4.6 p7).

No doubt we are going wrong again somewhere, but we cannot see  where.
Maybe  just wanting to examine the integer part of a fixed-point value
is against the principles of Ada real numbers.

Any ideas ?

Thanks in advance.
--

Regards,

	  Anton Gibbs    <..seismo!mcvax!ukc!stc!idec!anton>
			 +44 438 726161 x8283

eyal@wisdom.BITNET (Eyal mozes) (06/12/86)

> Now we have encountered a problem, trivial by  nature,  which  has  us
> baffled.   The problem is simply that of obtaining the integer part of
> a positive fixed-point value.  We can find no simple way of  achieving
> this  in Ada: use of numeric conversion leads to uncertainty regarding
> the direction of rounding (ARM 4.6 p7).

Well, one way is to substract 0.5 from the number before converting
it; this should always give you the correct result.

Another way is to use the following function (which I write when I
had to do the same thing a few months ago:

function integer_part(value: real) return integer is
   temp: integer := integer(value);
begin
   if real(temp) > value then
      return temp-1;
   else
      return temp;
   end if;
end;

        Eyal Mozes

        BITNET:                         eyal@wisdom
        CSNET and ARPA:                 eyal%wisdom.bitnet@wiscvm.ARPA
        UUCP:                           ..!ucbvax!eyal%wisdom.bitnet