comp.lang.ada
 help / color / mirror / Atom feed
From: hirasuna@acm.org (Michael Hirasuna)
Subject: Re: floats to integers?
Date: Sat, 11 Feb 1995 09:51:05 -0500
Date: 1995-02-11T09:51:05-05:00	[thread overview]
Message-ID: <hirasuna-1102950951050001@0.0.0.0> (raw)
In-Reply-To: 3hi283$a3q@solar.sky.net

In article <3hi283$a3q@solar.sky.net>, jhoward@solar.sky.net (John Howard)
wrote:
> : Is there a portable way to define a truncation function in Ada 83, which
> : rounds a floating point number down to the nearest integer?

> I am not a "guru" on Ada.  But to get a Floor instead of a Ceiling round for 
> a portable truncation function would require a subtraction of 0.5 from the 
> positive input float and then a default Ceiling round from Integer 
> conversion would be correct.  e.g. Integer(2.89 - 0.5) should round down 
> to 2.

That would work for 2.89, but what about 3.0? Does Integer (3.0 - 0.5)
yield 3 or does it yield 2?

> If Integer conversion is not consistent across compilers (I don't know if 
> this is the case but I doubt it with Ada), then a simple IF-THEN-ELSE inside 
> the two rounding functions could condition on the result of a constant 
> expression such as Integer(2.89 - 0.5) to check if it is 2.  That would 
> determine the direction Integer rounds.

Integer(2.89 - 0.5) is consistant across all Ada compilers, but
Integer(2.5) is not. And I don't think it is required to be consistant
even in the same compiler. So testing it for a know case, e.g. Integer
(2.5), will not necessarily predict how Integer (A) will round if a is
exactly halfway between integers. (Note that in a binary representation,
"exactly halfway" is very possible.)

> For portability we need two rounding functions: Floor and Ceiling.

I think this problem has been addressed in Ada 95. That is why I explicity
said Ada 83 in the problem statement. Your solution would work for most
cases, but this was not a request for a practical solution. This was an
exercise for a would-be Ada language lawyer preparing for the 1815A bar
exams :-).

-- 
Michael Hirasuna    |  hirasuna@acm.org



  reply	other threads:[~1995-02-11 14:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1995-02-10 19:48 floats to integers? Ghada 
1995-02-10 23:54 ` Michael Hirasuna
1995-02-11 10:06   ` John Howard
1995-02-11 14:51     ` Michael Hirasuna [this message]
     [not found] <3i2gbu$56r@sanson.dit.upm.es>
     [not found] ` <3ie7je$dd9@newsbf02.news.aol.com>
1995-02-25 14:21   ` Michael Hirasuna
replies disabled

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