PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > checking for close enough floating point values.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
checking for close enough floating point values.

Réponse
 
LinkBack Outils de la discussion
Vieux 29/05/2008, 18h30   #1
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut checking for close enough floating point values.

Let's say I have two doubles:

double a, b;

a = 9.35678910034592
b = 9.35678925602334

Obviously, a < b but lets say I just want to check up to 6 places
after the decimal. I want to check if the condition a >= b is
satisfied. I have a tolerance value:

#define EPSILON 0.000001

Is this a good way to check for a >= b

if( fabs(a-b) <= EPSILON)
....

  Réponse avec citation
Vieux 29/05/2008, 18h45   #2
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: checking for close enough floating point values.

On May 29, 8:30 pm, pereges <Brol...@gmail.com> wrote:
> Let's say I have two doubles:
>
> double a, b;
>
> a = 9.35678910034592
> b = 9.35678925602334
>
> Obviously, a < b but lets say I just want to check up to 6 places
> after the decimal. I want to check if the condition a >= b is
> satisfied. I have a tolerance value:
>
> #define EPSILON 0.000001
>
> Is this a good way to check for a >= b
>
> if( fabs(a-b) <= EPSILON)
> ...

Question 14.5 of the C-FAQ.
<http://c-faq.com/>
  Réponse avec citation
Vieux 29/05/2008, 18h45   #3
Walter Roberson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: checking for close enough floating point values.

In article <20765975-78f0-499c-a276-4170712e769d@x1g2000prh.googlegroups.com>,
pereges <Broli00@gmail.com> wrote:
>Let's say I have two doubles:


>double a, b;


>a = 9.35678910034592
>b = 9.35678925602334


>Obviously, a < b but lets say I just want to check up to 6 places
>after the decimal. I want to check if the condition a >= b is
>satisfied. I have a tolerance value:


>#define EPSILON 0.000001


>Is this a good way to check for a >= b


>if( fabs(a-b) <= EPSILON)
>...


That is a typical way to check whether values are "close enough"
to being equal.

However, you should be aware that this is -not- a test for
equality up to the 6th decimal place. There is no exact representation
for 0.000001 in binary floating point arithmetic, so chances
are that instead if you use that EPSILON, what you will end up doing is
checking whether they are equal to within
9.999999999999999547481118258862586856139387236908 07819366455078125e-07

That's a number slightly less than 0.000001. This could make a difference
if your a and b values were (for example) on the order of .00009 themselves.

--
"Beauty, like all other qualities presented to human experience,
is relative; and the definition of it becomes unmeaning and
useless in proportion to its abstractness." -- Walter Pater
  Réponse avec citation
Vieux 30/05/2008, 05h52   #4
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: checking for close enough floating point values.

On Thu, 29 May 2008 10:30:49 -0700 (PDT), pereges <Broli00@gmail.com>
wrote:

>Let's say I have two doubles:
>
>double a, b;
>
>a = 9.35678910034592
>b = 9.35678925602334
>
>Obviously, a < b but lets say I just want to check up to 6 places
>after the decimal. I want to check if the condition a >= b is
>satisfied. I have a tolerance value:
>
>#define EPSILON 0.000001
>
>Is this a good way to check for a >= b
>
>if( fabs(a-b) <= EPSILON)
>...


This checks for a == b, not a >= b.

if (a + EPSILON >= b) would be the corresponding check for >=.

However, you should read 14.5 in the c-faq (www.c-faq.com) to see why
an unscaled EPSILON is a bad idea.


Remove del for email
  Réponse avec citation
Vieux 30/05/2008, 10h07   #5
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: checking for close enough floating point values.

On May 30, 9:52 am, Barry Schwarz <schwa...@dqel.com> wrote:

> This checks for a == b, not a >= b.
>
> if (a + EPSILON >= b) would be the corresponding check for >=.
>
> However, you should read 14.5 in the c-faq (www.c-faq.com) to see why
> an unscaled EPSILON is a bad idea.


So do you think it will be better to use the relative difference
method as suggested in the C faq 14.5 ?

#define Abs(x) ((x) < 0 ? -(x) : (x))
#define Max(a, b) ((a) > (b) ? (a) : (b))

double RelDif(double a, double b)
{
double c = Abs(a);
double d = Abs(b);

d = Max(c, d);

return d == 0.0 ? 0.0 : Abs(a - b) / d;
}


....
to check for a >= b,

if(RelDif(a, b) <= TOLERANCE || a > b)
...

Btw the floating point data that I'm reading has 6 places after the
decimal point so I though I would take a tolerance value of 0.000001.
  Réponse avec citation
Vieux 30/05/2008, 10h22   #6
pereges
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: checking for close enough floating point values.

I don't know why a seperate macro has been written for finding
absolute value. Was it not possible to use fabs() ?

#define Abs(x) ((x) < 0 ? -(x) : (x))
  Réponse avec citation
Vieux 30/05/2008, 11h33   #7
Bartc
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: checking for close enough floating point values.


"Barry Schwarz" <schwarzb@dqel.com> wrote in message
news:inku34935bb3u8akoohq73ffc3jnvj02e7@4ax.com...
> On Thu, 29 May 2008 10:30:49 -0700 (PDT), pereges <Broli00@gmail.com>
> wrote:
>
>>Let's say I have two doubles:
>>
>>double a, b;
>>
>>a = 9.35678910034592
>>b = 9.35678925602334
>>
>>Obviously, a < b but lets say I just want to check up to 6 places
>>after the decimal. I want to check if the condition a >= b is
>>satisfied. I have a tolerance value:
>>
>>#define EPSILON 0.000001
>>
>>Is this a good way to check for a >= b
>>
>>if( fabs(a-b) <= EPSILON)
>>...

>
> This checks for a == b, not a >= b.
>
> if (a + EPSILON >= b) would be the corresponding check for >=.


What about:

if ( a>b || (fabs(a-b)<=EPSILON) )

for a>=b ? This would be faster when a is normally expected to be greater
than b.

--
Bartc


  Réponse avec citation
Vieux 31/05/2008, 08h24   #8
Barry Schwarz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: checking for close enough floating point values.

On Fri, 30 May 2008 02:07:53 -0700 (PDT), pereges <Broli00@gmail.com>
wrote:

>On May 30, 9:52 am, Barry Schwarz <schwa...@dqel.com> wrote:
>
>> This checks for a == b, not a >= b.
>>
>> if (a + EPSILON >= b) would be the corresponding check for >=.
>>
>> However, you should read 14.5 in the c-faq (www.c-faq.com) to see why
>> an unscaled EPSILON is a bad idea.

>
>So do you think it will be better to use the relative difference
>method as suggested in the C faq 14.5 ?
>


snip

>Btw the floating point data that I'm reading has 6 places after the
>decimal point so I though I would take a tolerance value of 0.000001.


If you can guarantee that all the data will be within a certain range
then you don't need to scale based on the current value. But what
happens when one set of values is near 1e-6 and another is near 1e10?
That is the point I thought the faq was trying to make.


Remove del for email
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h04.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16795 seconds with 16 queries