comp.lang.ada
 help / color / mirror / Atom feed
* Getting the integer part of a real
@ 1986-11-05 14:25 emery
  1986-11-07 13:47 ` Robert Firth
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: emery @ 1986-11-05 14:25 UTC (permalink / raw)





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;

				Dave Emery
until 7 Nov:	   ...princeton!siemens!emery
		      princeton!siemens!emery@seismo.css.gov
after 10 Nov:             	linus!emery
		      		emery@mitre-bedford.arpa

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

* Re: Getting the integer part of a real
  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
  1986-11-07 15:31 ` emery
  2 siblings, 0 replies; 6+ messages in thread
From: Robert Firth @ 1986-11-07 13:47 UTC (permalink / raw)


Given

	x : float;
	i : integer;

then the line

	if (i > x) then ...

won't work, since ">" isn't defined between integers
and floats.  This is perhaps what is meant:

	if float(i) > x then ...

However, this does not really help the hard-core numerical
programmer, who most likely wants X rounded but left in
FLOATING representation - since otherwise he can use only
a very restricted subrange of float.

What we really need is the set


	function FLOOR (X : FLOAT) return FLOAT;
		 CEIL
		 ROUND
		 TRUNC

and let's make them generic in the domain type.

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

* Re: Getting the integer part of a real
  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
  1986-11-10 22:12   ` David desJardins
  1986-11-07 15:31 ` emery
  2 siblings, 1 reply; 6+ messages in thread
From: Arny B. Engelson @ 1986-11-07 14:27 UTC (permalink / raw)


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

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

* Re: Getting the integer part of a real
  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
@ 1986-11-07 15:31 ` emery
  2 siblings, 0 replies; 6+ messages in thread
From: emery @ 1986-11-07 15:31 UTC (permalink / raw)




oops, made a type mismatch mistake in the last note:

comparison should be:

	if (real(i) > x) then
	    i := i - 1;		-- machine rounded up

There is no operation ">" (L : integer, R : real)....

Sorry about that, chief...

			Dave Emery
			Mitre Corp.
			emery@mitre-bedford.arpa

	    

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

* Re: Getting the integer part of a real
  1986-11-07 14:27 ` Arny B. Engelson
@ 1986-11-10 22:12   ` David desJardins
  0 siblings, 0 replies; 6+ messages in thread
From: David desJardins @ 1986-11-10 22:12 UTC (permalink / raw)


In article <996@wayback.UUCP> arny@wayback.UUCP (Arny B. Engelson) writes:
>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:

   Not to restart the flame wars (I hope!), but when this topic has been
discussed in the past, the consensus (at least among mathematicians) has
been that "round-down" is preferable to "round-toward-zero."  At the very
least it is not clear that the latter is preferable.

   -- David desJardins

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

* Re: Getting the integer part of a real
@ 1986-11-12 13:34 Hazel
  0 siblings, 0 replies; 6+ messages in thread
From: Hazel @ 1986-11-12 13:34 UTC (permalink / raw)


Oops, you got me!  OK, first check if the number is positive or negative, and
do the 'correct' thing.  Obviously, I hadn't thought it all through when
I posted that.  The context of our discussion (at Siemens) on that was 
1.  realizing what the language said, and
2.  coming up with a strategy to get the integer part that didn't depend on
    underlying implementation.  
"The details are left to the reader"  (Meaning, I'm too lazy to work them 
out.  

Thanks, Ken.

				Dave Emery

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

end of thread, other threads:[~1986-11-12 13:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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