Re: Comparing floating point values in Java



Daniel Pitts wrote:
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.


Since the special cases are rare, one could also describe them by having
e.g. a HashSet<Integer> containing the index values of the special cases.

Using any magic value, including NaN, has the risk that a bug elsewhere
in the code will get hidden as the special case. The risk can be reduced
by using a specific NaN value and Float.floatToRawIntBits to check for it.

Patricia
.



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
    ... Eric Sosman wrote: ... 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 ...
    (comp.lang.java.programmer)
  • Re: Problem with log function - Intel fortran compiler under Linux
    ... compiler under Linux ... an NaN (while the argument is a perfectly acceptable value of about ... Linux: Red Hat Enterprise Linux, ... I then tried with debugging on and array bound checking ...
    (comp.lang.fortran)
  • Re: How to store a number that is "n/a"?
    ... There are actually 2 flavours of NaN values defined by the ... IEEE standard: signalling and ... quiet (which simply propagate through calculations. ... store a number in the decimal array (or any other number array, ...
    (microsoft.public.vb.general.discussion)
  • [OT] Re: model implementation
    ... I want to know the very basics of model ... What I did was create a two-dimensional array of double values, ... The total length of the simulation was a value I let the user decide, ... calculating each level's new content based entirely on the values ...
    (comp.lang.cpp)