From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,9a4dadd62f24335b X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-02-11 02:06:59 PST Path: nntp.gmd.de!newsserver.jvnc.net!howland.reston.ans.net!news.sprintlink.net!solar.sky.net!solar.sky.net!jhoward From: jhoward@solar.sky.net (John Howard) Newsgroups: comp.lang.ada Subject: Re: floats to integers? Date: 11 Feb 1995 10:06:59 GMT Organization: SkyNET Corporation Message-ID: <3hi283$a3q@solar.sky.net> References: <3hgfv9$468@mother.usf.edu> NNTP-Posting-Host: solar.sky.net X-Newsreader: TIN [version 1.2 PL2] Date: 1995-02-11T10:06:59+00:00 List-Id: 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.