Re: Comparing floating point values in Java




Eric Sosman wrote:
Philipp wrote:
[...]

I was calculating an array of float values from some user input. But
sometimes (rarely) the calculation cannot be done (this is correct
behavior).
I then want to mark these values by setting them to -1 and treat this
special case later on. So I later need to test for -1 in my array.

Perhaps you can avoid the entire issue by choosing a
different "distinguished value." Float.NaN, maybe, using
the Float.isNaN(float) method instead of an == test. (NaN
seems a more natural surrogate for "does not compute," too.)

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx

Or even have a seperate boolean array to specify valid results.
Having a magic value isn't generally a good idea, although NaN would be
the way to go if you did.

.



Relevant Pages

  • Re: Comparing floating point values in Java
    ... I was calculating an array of float values from some user input. ... Having a magic value isn't generally a good idea, although NaN would be ... Using any magic value, including NaN, has the risk that a bug elsewhere ...
    (comp.lang.java.programmer)
  • Re: Comparing floating point values in Java
    ... I was calculating an array of float values from some user input. ... Having a magic value isn't generally a good idea, although NaN would be ... Using any magic value, including NaN, has the risk that a bug elsewhere ...
    (comp.lang.java.programmer)
  • Re: indirect sort
    ... Eric Sosman wrote: ... If you look at the source for Arrays.sort(double), one of the first things it does is scan the array for NaN, push them to the end, and then sort the array from 0 to length-#_of_NaNs. ...
    (comp.lang.java.programmer)
  • Re: Data Structure Speed
    ... Eric Sosman wrote: ... N - 1 (where N is the size of the array), I need to update the same ... Knuth wrote that "Premature optimization is the root of ... Oops, sorry for the top-post. ...
    (comp.lang.java.help)
  • Re: assigning array addresses to integer variables and vice versa
    ... Eric Sosman wrote: ... So without using much of my gray matter I replied stating ... >> array and &array are same. ... > implicit and does not show up in something like a core dump -- ...
    (comp.lang.c)