Thread View: net.lang.ada
4 messages
4 total messages
Started by emery
Wed, 05 Nov 1986 14:25
Getting the integer part of a real
Author: emery
Date: Wed, 05 Nov 1986 14:25
Date: Wed, 05 Nov 1986 14:25
24 lines
1134 bytes
1134 bytes
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
Re: Getting the integer part of a real
Author: Robert Firth
Date: Fri, 07 Nov 1986 13:47
Date: Fri, 07 Nov 1986 13:47
28 lines
1209 bytes
1209 bytes
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.
Re: Getting the integer part of a real
Author: Arny B. Engelson
Date: Fri, 07 Nov 1986 14:27
Date: Fri, 07 Nov 1986 14:27
42 lines
1661 bytes
1661 bytes
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
Re: Getting the integer part of a real
Author: emery
Date: Fri, 07 Nov 1986 15:31
Date: Fri, 07 Nov 1986 15:31
18 lines
854 bytes
854 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads