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-Thread: 103376,d402aef7676f64a5 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local01.nntp.dca.giganews.com!nntp.scarlet.biz!news.scarlet.biz.POSTED!not-for-mail NNTP-Posting-Date: Wed, 13 Dec 2006 10:53:06 -0600 From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Comparing Floating Point Values References: <1166026474.277616.267400@f1g2000cwa.googlegroups.com> Date: Wed, 13 Dec 2006 17:51:05 +0100 Message-ID: <87ejr368p2.fsf@ludovic-brenta.org> User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/21.4 (gnu/linux) Cancel-Lock: sha1:kfUJMe1tXKH1cw7GX7eCM8rSSqI= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: 62.235.205.116 X-Trace: sv3-idoLH43PJZPFLNVRqBZQ8gULgbS1C1bzCk5SwHGcjDD8+3NDv7z0mjLPIqW6EN+JE0WyPZWVPJg1I4x!wp6JqQ6K8jTj2/RQrlk6ODz4lPB0SxRh+ccDWiV5hBgtussbK+uThVdEv1HfDl5K+eCHbAZOj/g= X-Complaints-To: abuse@scarlet.be X-DMCA-Complaints-To: abuse@scarlet.biz X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.32 Xref: g2news2.google.com comp.lang.ada:7907 Date: 2006-12-13T17:51:05+01:00 List-Id: markp writes: > I was wondering if there was a convention for comparing 2 floats > dealing with significant digits. For example, if I had 2 variables (x > and y) and wanted to see if they were equal in an if statement, is > there a standard way to do a compare based on "x" significant digits? The way we usually do this is by defining our own floating-point types and redefine the "=" operator to deal with the precision appropriate for the type and the application. For example: type Kilograms is digits 6 range 0.0 .. 600_000.0; function "=" (L, R : Kilograms) is Epsilon : constant Kilograms := 0.1; begin if L < R then return R - L < Epsilon; else return L - R < Epsilon; end if; end "="; The above uses an absolute precision; it would be trivial to use a relative precision instead. HTH -- Ludovic Brenta.