comp.lang.ada
 help / color / mirror / Atom feed
From: arny@wayback.UUCP (Arny B. Engelson)
Subject: Re: Getting the integer part of a real
Date: Fri, 7-Nov-86 09:27:50 EST	[thread overview]
Date: Fri Nov  7 09:27:50 1986
Message-ID: <996@wayback.UUCP> (raw)
In-Reply-To: 38000034@gypsy.UUCP

Dave Emery writes:
> 
> When we discovered the 'feature' in Ada that an implementor can pick how
> he rounds, we discussed various ways to get the integer part of a number.
> Here is the 'best' (meaning most portable) way:  (p.s. this 'algorithm' 
> is obviously language-independent)
> 
> 	declare
> 	    x : real;
> 	    i : integer;	-- integer part of x
> 	begin
> 	    i := integer(x);
> 	    if (i > x) then
> 		   -- machine rounded up
>                 i := i - 1;
> 	    end if;
> 	end;
> 

The above algorithm only works for POSITIVE values of X!  For example, if
X = -0.7 this will return -1 instead of 0.  The "integer part of a real number"
is a truncate function:

   function Trunc (X : Float) return Integer is
      I : Integer := Integer (X);
   begin
      if I > 0 and then
	 Float(I) > X then
	    I := I - 1;
      elsif I < 0 and then
	 Float(I) < X then
	    I := I + 1;
      end if;
      return I;
   end Trunc;

This should return whatever is before the decimal point in a floating point
number.

Arny B. Engelson
{ihnp4 | bonnie | clyde } wayback!arny
(201) 386-4816

  parent reply	other threads:[~1986-11-07 14:27 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1986-11-05 14:25 Getting the integer part of a real emery
1986-11-07 13:47 ` Robert Firth
1986-11-07 14:27 ` Arny B. Engelson [this message]
1986-11-10 22:12   ` David desJardins
1986-11-07 15:31 ` emery
  -- strict thread matches above, loose matches on Subject: below --
1986-11-12 13:34 Hazel
replies disabled

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