Article View: net.lang.ada
Article #1053Re: Getting the integer part of a real
From: 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
Technical Details
Message-ID:
<996@wayback.UUCP>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!wayback!arnyReferences:
<38000034@gypsy.UUCP>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!wayback!arny From: arny@wayback.UUCP (Arny B. Engelson) Newsgroups: net.lang.ada Subject: Re: Getting the integer part of a real Message-ID: <996@wayback.UUCP> Date: Fri, 7-Nov-86 09:27:50 EST Article-I.D.: wayback.996 Posted: Fri Nov 7 09:27:50 1986 Date-Received: Sat, 8-Nov-86 06:42:59 EST References: <38000034@gypsy.UUCP> Organization: AT&T Bell Labs, Whippany, NJ Lines: 42 Summary: This only works for POSITIVE numbers!