comp.lang.ada
 help / color / mirror / Atom feed
* floats to integers?
@ 1995-02-10 19:48 Ghada 
  1995-02-10 23:54 ` Michael Hirasuna
  0 siblings, 1 reply; 5+ messages in thread
From: Ghada  @ 1995-02-10 19:48 UTC (permalink / raw)


I'm new with Ada and I need to finish this assignment where I have to
turn a float into an integer (i.e. 2.89 to 2) any answers from you
gurus out there?



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

* Re: floats to integers?
  1995-02-10 19:48 Ghada 
@ 1995-02-10 23:54 ` Michael Hirasuna
  1995-02-11 10:06   ` John Howard
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Hirasuna @ 1995-02-10 23:54 UTC (permalink / raw)


In article <3hgfv9$468@mother.usf.edu>, gahda@wusffm.wusf.usf.edu (Ghada )
wrote:

> I'm new with Ada and I need to finish this assignment where I have to
> turn a float into an integer (i.e. 2.89 to 2) any answers from you
> gurus out there?

Since you are new to Ada, I assume that this is not a trick question and
give you the simple answer. Any numeric type can be explicitly type
converted to any other numeric type. If A is a floating point variable,
Integer (A) will yield an integer value. When converting a real type to an
integer type Ada will round. Therefore, for your example, the type
converting 2.89 will yield 3.

Here is a question that you might ask a real Ada "guru" (we prefer the
term "language lawyer"):

Is there a portable way to define a truncation function in Ada 83, which
rounds a floating point number down to the nearest integer?

-- 
Michael Hirasuna    |  hirasuna@acm.org



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

* Re: floats to integers?
  1995-02-10 23:54 ` Michael Hirasuna
@ 1995-02-11 10:06   ` John Howard
  1995-02-11 14:51     ` Michael Hirasuna
  0 siblings, 1 reply; 5+ messages in thread
From: John Howard @ 1995-02-11 10:06 UTC (permalink / raw)


Michael Hirasuna (hirasuna@acm.org) wrote:
: In article <3hgfv9$468@mother.usf.edu>, gahda@wusffm.wusf.usf.edu (Ghada )
: wrote:

: > I'm new with Ada and I need to finish this assignment where I have to
: > turn a float into an integer (i.e. 2.89 to 2) any answers from you
: > gurus out there?

: Since you are new to Ada, I assume that this is not a trick question and
: give you the simple answer. Any numeric type can be explicitly type
: converted to any other numeric type. If A is a floating point variable,
: Integer (A) will yield an integer value. When converting a real type to an
: integer type Ada will round. Therefore, for your example, the type
: converting 2.89 will yield 3.

: Here is a question that you might ask a real Ada "guru" (we prefer the
: term "language lawyer"):

: Is there a portable way to define a truncation function in Ada 83, which
: rounds a floating point number down to the nearest integer?
: Michael Hirasuna    |  hirasuna@acm.org

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.

On the other hand, if any value over 2.0 and less than 3.0 causes a 
Ceiling round to 3 then first subtract 1.0 instead of 0.5 and then type cast.

I don't remember whether the Ceiling round of a negative floating point 
number tends to be less than or greater than the original float.  But the 
negative floats would have to be accounted for accordingly.

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

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.



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

* Re: floats to integers?
  1995-02-11 10:06   ` John Howard
@ 1995-02-11 14:51     ` Michael Hirasuna
  0 siblings, 0 replies; 5+ messages in thread
From: Michael Hirasuna @ 1995-02-11 14:51 UTC (permalink / raw)


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



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

* Re: floats to integers?
       [not found] ` <3ie7je$dd9@newsbf02.news.aol.com>
@ 1995-02-25 14:21   ` Michael Hirasuna
  0 siblings, 0 replies; 5+ messages in thread
From: Michael Hirasuna @ 1995-02-25 14:21 UTC (permalink / raw)


In article <EACHUS.95Feb14104008@spectre.mitre.org>,
eachus@spectre.mitre.org (Robert I. Eachus) wrote:

>    Hmmm... If you want to always return a valid answer when possible,
> you need to do:
> 
>     function Floor (X: in Float) return Integer is
>       Temp: Integer;
>     begin
>       Temp := Integer(X);
>       if Float(Temp) > X then return Temp - 1; else return Temp; end if;
>     exception
>       when others =>
>         if X < Float(Integer'LAST) + 1.0 and X >= Float(Integer'LAST)
>         then return Integer'LAST;
>         else raise Constraint_Error;
>         end if;
>     end Floor;

I receive another solution from Angel Alvarez (aalvarez@goya.eunet.es)

if Float(Integer(X)) = X 
  then return Integer(X);
  else return Integer(X-0.5);end if;

At first glance this seems a more brute force approach, but is does avoid
the problems of the possible numeric error that may arise from integer
substraction.

-- 
Michael Hirasuna    |  hirasuna@acm.org



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

end of thread, other threads:[~1995-02-25 14:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <3i2gbu$56r@sanson.dit.upm.es>
     [not found] ` <3ie7je$dd9@newsbf02.news.aol.com>
1995-02-25 14:21   ` floats to integers? Michael Hirasuna
1995-02-10 19:48 Ghada 
1995-02-10 23:54 ` Michael Hirasuna
1995-02-11 10:06   ` John Howard
1995-02-11 14:51     ` Michael Hirasuna

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