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!news2.google.com!postnews.google.com!j72g2000cwa.googlegroups.com!not-for-mail From: "Adam Beneschan" Newsgroups: comp.lang.ada Subject: Re: Comparing Floating Point Values Date: 13 Dec 2006 08:45:25 -0800 Organization: http://groups.google.com Message-ID: <1166028325.672526.313220@j72g2000cwa.googlegroups.com> References: <1166026474.277616.267400@f1g2000cwa.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1166028331 1257 127.0.0.1 (13 Dec 2006 16:45:31 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 13 Dec 2006 16:45:31 +0000 (UTC) User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: j72g2000cwa.googlegroups.com; posting-host=66.126.103.122; posting-account=cw1zeQwAAABOY2vF_g6V_9cdsyY_wV9w Xref: g2news2.google.com comp.lang.ada:7906 Date: 2006-12-13T08:45:25-08:00 List-Id: markp wrote: > 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? > > Thank you. I don't do a lot of floating-point programming, so I don't know whether there's a standard. I would do something like if abs(x-y) <= epsilon * x then --- treat x and y as equal... where epsilon is some suitable small constant or named number. But this really isn't my area of knowledge, and if someone else has a different idea you should trust theirs rather than mine. And I wouldn't call this a "convention", which implies that this is sort of like a "custom" that everyone agrees to follow; the correct value of epsilon, and the correct way to compare, really depend on a mathematical analysis of your particular problem. -- Adam