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 06:51:05 PST Path: nntp.gmd.de!newsserver.jvnc.net!nntpserver.pppl.gov!princeton!udel!gatech!howland.reston.ans.net!news.sprintlink.net!news.clark.net!NewsWatcher!user From: hirasuna@acm.org (Michael Hirasuna) Newsgroups: comp.lang.ada Subject: Re: floats to integers? Date: Sat, 11 Feb 1995 09:51:05 -0500 Organization: Clark Internet Services, Inc. Message-ID: References: <3hgfv9$468@mother.usf.edu> <3hi283$a3q@solar.sky.net> NNTP-Posting-Host: hirasuna.clark.net Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Date: 1995-02-11T09:51:05-05:00 List-Id: 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