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.3 required=5.0 tests=BAYES_00,FREEMAIL_FROM, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,cb3f9900b9cf2da6 X-Google-Attributes: gid103376,public From: johnherro@aol.com (John Herro) Subject: Re: Truncation of FLOAT values Date: 1996/04/19 Message-ID: <4l8a6h$274@newsbf02.news.aol.com>#1/1 X-Deja-AN: 148393057 sender: root@newsbf02.news.aol.com references: <4l6bjn$b96@cliffy.lfwc.lockheed.com> organization: America Online, Inc. (1-800-827-6364) newsgroups: comp.lang.ada Date: 1996-04-19T00:00:00+00:00 List-Id: Franz.Kruse@erno.de (Franz Kruse) writes: > Is there a simple way in Ada 83 to determine > the integer part or the fraction part of a FLOAT > value? A conversion to INTEGER cannot be > used, because it rounds. Here are two functions from my Ada Tutor program that will work in Ada 83. The two functions behave the same for positive Floats but differently for negative Floats. This first function Floor computes the largest Integer less than or equal to a given Float, always truncating toward negative infinity: function Floor(X : in Float) return Integer is Answer : Integer := Integer(X); begin if Float(Answer) > X then Answer := Answer - 1; end if; return Answer; end Floor; This second function always truncates toward zero: function Truncate(X : in Float) return Integer is Answer : Integer := Integer(X); begin if Answer > 0 and Float(Answer) > X then Answer := Answer - 1; elsif Answer < 0 and Float(Answer) < X then Answer := Answer + 1; end if; return Answer; end Truncate; Needless to say, getting the fractional part of a Float after you have the Integer part is trivial - just convert the Integer back to Float and subtract from the original number. I hope this helps. - John Herro Software Innovations Technology http://members.aol.com/AdaTutor ftp://members.aol.com/AdaTutor