Re: Meaning of Double.Epsilon

From: Niki Estner (niki.estner_at_cube.net)
Date: 09/10/04


Date: Fri, 10 Sep 2004 21:37:12 +0200


"cody" <please_dont.spam.deutronium@gmx.de> wrote in
news:e0VDug2lEHA.1652@TK2MSFTNGP09.phx.gbl...
>> That's the point I've already tried to say: I *do* use tests like "if (a
>> == -1) a = DoLazyCalculation();" and the like; I'd also guess the
>> equality
>> operator is implicitly used when you use doubles as keys in a hashtable,
> or
>> sort an array of doubles (at least with generics, things like this should
>> trigger your warning, too). I don't think it would be a good idea to have
> a
>> warning *every* time the == operator is used on fp values;
>
> using -1 is imho a bad idea. what if you actually need -1 in your app?
> Negative numbers are very common in floating point (in contrast to
> integers). What if you inadvertantly use the float variable with its -1
> value?
> NaN is just for that purpose: If a value is NaN it stays NaN even if used
> in
> calculations or converted from double to float and vice versa and this
> way,
> you know that something gone wrong if you see NaN in the output.
> The following code displays 4 times NaN:

I think this is going slightly off-topic now, but of course "special values"
like these have to be values that cannot be result of a calculation.
Depending on the meaning of the value this could be a negative value (e.g
distances), NaN, a value above 100 (percentages), or whatever. Although NaN
is a good "special value", too, it does have disadvantages, sometimes: AFAIK
Double.Parse can't parse it, converting it to an int will throw an
exception, you only have one single special value, and sometimes NaN can be
result of a calculation...

Niki



Relevant Pages

  • Re: test for nan
    ... John Hunter wrote: ... > I have a C extension module that is returning some doubles. ... > doubles get out of range, the numbers print as 'nan'. ... Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. ...
    (comp.lang.python)
  • Re: equality and null pointers
    ... fundamental meaning of NaN? ... at least when you want to use Doubles as hash table keys. ...
    (comp.lang.java.programmer)
  • test for nan
    ... I have a C extension module that is returning some doubles. ... the numbers print as 'nan'. ... John Hunter ...
    (comp.lang.python)
  • Re: std::max and NAN
    ... collection of doubles of which at least one is NaN causes undefined ... as an infinite loop. ... algorithmical reasons. ...
    (microsoft.public.vc.stl)
  • Re: Division by zero: float vs. int
    ... they simply cannot be represented as reals. ... prefer to invoke the complex plane -- "this calculation has two values neither ... So the NaN result is a meta-level assertion shoe-horned into IEEE FP ...
    (comp.lang.java.programmer)