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,19805810c471a6af X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-11-06 18:44:40 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newsfeed.news2me.com!newsfeed2.earthlink.net!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!stamper.news.atl.earthlink.net!harp.news.atl.earthlink.net!beastie.ix.netcom.com!nobody From: Dennis Lee Bieber Newsgroups: comp.lang.ada Subject: Re: Decimals after the point Date: Wed, 06 Nov 2002 18:06:00 -0800 Organization: >> Leaf-Eating Penguins? << Message-ID: <9qhcqa.bp3.ln@beastie.ix.netcom.com> References: <4bhy9.52855$T_3.627411@wagner.videotron.net> NNTP-Posting-Host: a5.f7.db.67 Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Server-Date: 7 Nov 2002 02:44:40 GMT User-Agent: KNode/0.6.1 X-noarchive: yes Xref: archiver1.google.com comp.lang.ada:30500 Date: 2002-11-07T02:44:40+00:00 List-Id: Genevieve B. fed this fish to the penguins on Wednesday 06 November 2002 03:01 pm: > Hi! > > For a project at university, there is a bonus that is to check for > only 2 decimals after the point in a float. > does anybody have an idea? > > I have an idea of putting my float into a string instead and then > check for the point...and then check for only 2 numbers after it... > Probably not valid -- after all, you could code the float->string write operation to only write two decimal places... Hence you'd always see just two decimals However, the assignment is rather error-prone, due to the problems of decimal <> binary conversions. What was input as 1.40 may be output as 1.3999999. The assignment should properly specify what behavior is to be taken in such a case. {The following is from a Python interpreter -- float is double } >>> a = 1.40 >>> a 1.3999999999999999 >>> print "%4f" % a 1.400000 >>> print "%7f" % a 1.400000 >>> print "%7.7f" % a 1.4000000 >>> print "%10.9f" % a 1.400000000 >>> print "%10.10f" % a 1.4000000000 >>> print "%16.15f" % a 1.400000000000000 >>> print "%16.16f" % a 1.3999999999999999 >>> print "%20.16f" % a 1.3999999999999999 So what is the correct output for your assignment? The internal (and 16 decimal place representation) turned 1.40 -> 1.399999999999, so effectively, 1.40 does NOT have only two decimal places. How close do two floating point numbers have to be to be accepted as "the same". -- > ============================================================== < > wlfraed@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG < > wulfraed@dm.net | Bestiaria Support Staff < > ============================================================== < > Bestiaria Home Page: http://www.beastie.dm.net/ < > Home Page: http://www.dm.net/~wulfraed/ <