comp.lang.ada
 help / color / mirror / Atom feed
From: jhoward@solar.sky.net (John Howard)
Subject: Re: floats to integers?
Date: 11 Feb 1995 10:06:59 GMT
Date: 1995-02-11T10:06:59+00:00	[thread overview]
Message-ID: <3hi283$a3q@solar.sky.net> (raw)
In-Reply-To: hirasuna-1002951854420001@168.143.6.213

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.



  reply	other threads:[~1995-02-11 10:06 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 [this message]
1995-02-11 14:51     ` Michael Hirasuna
     [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