Re: Double comparison problem

From: chris (caj_at_cs.york.ac.uk)
Date: 01/19/05


Date: Wed, 19 Jan 2005 14:48:13 +0000

Dietmar Kuehl wrote:
> chris wrote:
>
>>There are two groups of people in this world.
>>
>>1) Those who don't fully understand all of the details about
>
> performing
>
>>== and != on floating points values. They should never use == or !=
>
> on
>
>>floating point values (not being mean or anything, they just
>
> shouldn't).
>
>>2) Those who do understand all the details about performing == and !=
>
> on
>
>>floating point values. They don't use == and !=, so don't have to be
>>told not to :)
>
>
> I'd say you fall into the first group! After all, comparing floating
> point values representing reasonably sized integers is perfectly
> valid even using '==' or '!=': you can use floating point values as a
> form of bigger integer. You just have to take care of the values
> staying in a certain range. Actually, you can also do exact
> computations
> for other values, i.e. those which can represented exactly. The only
> thing to note is that the set of useful values is not closed over the
> operations and you need to realize when the result of the operation is
> of different kind (e.g. when addition or multiplication overflows,
> substraction cancels leading bits, and division is only valid for
> certain value). Sure enough, for most of my uses I don't play tricks
> with floating point values but it is still possible and if you e.g.
> need a 64 bit integer on a platform not supporting some form of
> "long long" it can be an appropriate choice to use a 'long double'.
OK, I apologise, I was perhaps a little too strong :) There are cases
where == and != can be used safely as long as you stay within some
reasonably tight bounds, as long as are careful :) But like you say you
have to be careful, so I find it easier to scare people off using == and
!= altogether, because I've seen far, far more cases where they were
being used incorrectly, and only a handful where they were being used
correctly, and in those cases the people using them knew they were using
them correctly :)

The idea of using a long double to replace a long long wasn't one I'd
seen before, thats quite a neat idea (although once again requires being
reasonably careful).

Chris



Relevant Pages

  • Re: Question for established T2s
    ... Metforming xr 750mg twice a day ... >>Chris J. wrote: ... >>Hopefully, but be careful. ... paranoia can be a useful thing... ...
    (alt.support.diabetes)
  • Re: Osc having a breakdown?
    ... >>> Chris, and this is the last time I'm going to bother wasting effort ... >>> Given your record, I strongly suspect 2. ... >You'd best be careful and moderate your behaviour. ...
    (uk.media.tv.misc)
  • Re: why are there so many world readable files?
    ... On February 18, 2004 04:41 pm, Chris W. Parker wrote: ... a script may check for valid user by greping the /etc/passwd ... storing passwords for scripts or programs to use is ... you could carefully remove that access, being careful not to break anything, ...
    (RedHat)
  • Re: terrible bug in Mathematica (//N), but why????
    ... the first expression and look at the numbers you get. ... A careful ... study of the results of floating point arithmetic may reveal why ... ask why does Mathematica not make this mistake for 44 or 42 but for 43? ...
    (sci.math.symbolic)
  • Re: Error in precision with number operations
    ... for really in-depth info on floating point arithmetic and rounding issues. ... Chris. ... I´ve a problem with some number operations in VBScript:: ...
    (microsoft.public.scripting.vbscript)