Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Floating po...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 17 of 17 Topic 43307 of 48417
Post > Topic >>

Re: Floating point computations differ in different runs of same

by James Kanze <james.kanze@[EMAIL PROTECTED] > Feb 15, 2008 at 03:09 AM

On Feb 14, 10:12 pm, Tim Slattery <Slatter...@[EMAIL PROTECTED]
> wrote:
> Richard Herring <junk@[EMAIL PROTECTED]
> wrote:
> >In message
> ><7775ee7d-0bc9-4a3c-b3c9-0b015a8b2...@[EMAIL PROTECTED]
>,
> >kasthurirangan.bal...@[EMAIL PROTECTED]
 writes

> >> Do not use "=3D=3D"
> >>operator for comparing doubles.

> >Do not offer such sweeping generalisations.

> >If it weren't appropriate to use operator=3D=3D on doubles, the
language
> >wouldn't supply it.

> Since floating point numbers - single or double precision - are
> approximations, the =3D=3D operator is not appropriate for them.

That's simply false.  Floating point numbers are not, in
themselves, approximations.  Every floating point number
represents an exact value.

Of course, that value might be an approximation of what is
wanted.  But the same thing is true for int: any value
representing the population of France, be it int or double, will
be an approximation (and in this case, double is probably just
as precise as int---maybe more so, if int's ony have 16 bits).

The problem is that double is often used as an abstraction for
real numbers.  But it's not a perfect abstraction, and you have
to understand the differences, and where the abstraction fails,
if you want to use double in this way.

> >>Best way is using abs, subtraction and
> >>less than operator with the precisions defined.

> >The "best" way depends on what you are trying to do. If testing for
> >equality is what you want to do, then operator=3D=3D is the best way.

> >>fabs(d1-d2) < 0.0001

> >>where d1,d2 are two doubles.

> >This may be appropriate in some cir***stances, but it is not
> >a test for equality.

> Maybe, but it's the best you can do with floating point numbers.

If it's not appropriate, then its the wrong way.  There are
cases where the right thing to do is compare using =3D=3D, and there
are cases where its not.  And you need to understand how
floating point works, and what you are trying to do, to be able
to even begin determing whether =3D=3D is appropriate in a specific
case, and if it isn't, what you should use to replace it.
(FWIW: I don't think I've ever seen a case where your example
would be appropriate.)

--
James Kanze (GABI Software)             email:james.kanze@[EMAIL PROTECTED]
 en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
 




 17 Posts in Topic:
Floating point computations differ in different runs of same pro
Rahul <rahulsharma@[EM  2008-02-14 01:17:12 
Re: Floating point computations differ in different runs of same
Lionel B <me@[EMAIL PR  2008-02-14 09:53:08 
Re: Floating point computations differ in different runs of same
kasthurirangan.balaji@[EM  2008-02-14 04:06:05 
Re: Floating point computations differ in different runs of same
Daniel Kraft <d@[EMAIL  2008-02-14 13:28:33 
Re: Floating point computations differ in different runs of same
Lionel B <me@[EMAIL PR  2008-02-14 15:29:26 
Re: Floating point computations differ in different runs of same
"Fred Zwarts" &  2008-02-14 16:59:27 
Re: Floating point computations differ in different runs of same
dohboy <dohboy@[EMAIL   2008-02-26 11:38:36 
Re: Floating point computations differ in different runs of same
Richard Herring <junk@  2008-02-14 17:02:27 
Re: Floating point computations differ in different runs of same
Tim Slattery <Slattery  2008-02-14 16:12:24 
Re: Floating point computations differ in different runs of same
"Victor Bazarov"  2008-02-14 16:37:36 
Re: Floating point computations differ in different runs of same
Kai-Uwe Bux <jkherciue  2008-02-14 19:22:43 
Re: Floating point computations differ in different runs of same
Pete Becker <pete@[EMA  2008-02-14 21:40:28 
Re: Floating point computations differ in different runs of same
Pete Becker <pete@[EMA  2008-02-14 21:50:20 
Re: Floating point computations differ in different runs of same
Pete Becker <pete@[EMA  2008-02-14 17:52:07 
Re: Floating point computations differ in different runs of same
kasthurirangan.balaji@[EM  2008-02-15 00:05:53 
Re: Floating point computations differ in different runs of same
Richard Herring <junk@  2008-02-15 10:03:35 
Re: Floating point computations differ in different runs of same
James Kanze <james.kan  2008-02-15 03:09:37 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 10:43:48 CST 2008.