* Comparing Floating Point Values
@ 2006-12-13 16:14 markp
2006-12-13 16:45 ` Adam Beneschan
` (4 more replies)
0 siblings, 5 replies; 7+ messages in thread
From: markp @ 2006-12-13 16:14 UTC (permalink / raw)
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.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Comparing Floating Point Values
2006-12-13 16:14 Comparing Floating Point Values markp
@ 2006-12-13 16:45 ` Adam Beneschan
2006-12-13 16:51 ` Ludovic Brenta
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Adam Beneschan @ 2006-12-13 16:45 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Comparing Floating Point Values
2006-12-13 16:14 Comparing Floating Point Values markp
2006-12-13 16:45 ` Adam Beneschan
@ 2006-12-13 16:51 ` Ludovic Brenta
2006-12-13 20:02 ` Jeffrey R. Carter
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Ludovic Brenta @ 2006-12-13 16:51 UTC (permalink / raw)
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.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Comparing Floating Point Values
2006-12-13 16:14 Comparing Floating Point Values markp
2006-12-13 16:45 ` Adam Beneschan
2006-12-13 16:51 ` Ludovic Brenta
@ 2006-12-13 20:02 ` Jeffrey R. Carter
2006-12-13 20:13 ` Gautier
2006-12-14 2:21 ` Steve
4 siblings, 0 replies; 7+ messages in thread
From: Jeffrey R. Carter @ 2006-12-13 20:02 UTC (permalink / raw)
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?
You might want to consider the attribute 'Model_Epsilon.
--
Jeff Carter
"English bed-wetting types."
Monty Python & the Holy Grail
15
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Comparing Floating Point Values
2006-12-13 16:14 Comparing Floating Point Values markp
` (2 preceding siblings ...)
2006-12-13 20:02 ` Jeffrey R. Carter
@ 2006-12-13 20:13 ` Gautier
2006-12-14 2:21 ` Steve
4 siblings, 0 replies; 7+ messages in thread
From: Gautier @ 2006-12-13 20:13 UTC (permalink / raw)
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.
Probably you are looking at something like abs(x-y) < 10**(-n)
where n is your amount of digits (not "x" !).
This is an absolute test, you may rather be interested by a relative one.
BTW, it's worth to take a look at...
Ada 95 Quality and Style Guide, 7.2.7:
http://www.adaic.com/docs/95style/html/sec_7/7-2-7.html
HTH
______________________________________________________________
Gautier -- http://www.mysunrise.ch/users/gdm/index.htm
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm
NB: For a direct answer, e-mail address on the Web site!
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Comparing Floating Point Values
2006-12-13 16:14 Comparing Floating Point Values markp
` (3 preceding siblings ...)
2006-12-13 20:13 ` Gautier
@ 2006-12-14 2:21 ` Steve
2006-12-14 22:22 ` Martin
4 siblings, 1 reply; 7+ messages in thread
From: Steve @ 2006-12-14 2:21 UTC (permalink / raw)
"markp" <markwork66@yahoo.com> wrote in message
news:1166026474.277616.267400@f1g2000cwa.googlegroups.com...
>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 am not aware of any standard way of doing this in Ada, but I did port some
code from C to Ada that does the job for 32 bit IEEE 757 floating point
values if you're interested.
There is a good article on the subject at:
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
Steve
(The Duck)
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Comparing Floating Point Values
2006-12-14 2:21 ` Steve
@ 2006-12-14 22:22 ` Martin
0 siblings, 0 replies; 7+ messages in thread
From: Martin @ 2006-12-14 22:22 UTC (permalink / raw)
Steve wrote:
[snip]
> http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
I'd second this - I've used the algorithms before and hightlights the
problems using the solutions provided 'above' in this thread.
Another path could be to ask... "Do I /really/ need to use
Floating-Point or would Fixed-Point be better?"...
Cheers
-- Martin
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2006-12-14 22:22 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-13 16:14 Comparing Floating Point Values markp
2006-12-13 16:45 ` Adam Beneschan
2006-12-13 16:51 ` Ludovic Brenta
2006-12-13 20:02 ` Jeffrey R. Carter
2006-12-13 20:13 ` Gautier
2006-12-14 2:21 ` Steve
2006-12-14 22:22 ` Martin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox