Re: What is the behaviour of double / float primitive == comparison?

Randall R Schulz <[email protected]>
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <[email protected]>
On Thursday 04 October 2007 15:23, Gregory A. Swarthout wrote:
>  >Carfield Yim wrote:
> >
> > I am not really clear about the definition of JLS:
> >
> > Otherwise, two distinct floating-point values are considered
> > unequal by the equality operators. In particular, there is one
> > value representing positive infinity and one value representing
> > negative infinity; each compares equal only to itself, and each
> > compares unequal to all other values.
> >
> >
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#5198
> >
> >
> > Say for following case, will this print true?
> >
> > System.out.println( 3.0/7.0 == 1.5/3.5 )
>
> Why ask us?  Why not try it and find out?

Well, that would be bad advice for someone wanting to learn how to write
portable code in the world of C and C++, since their specifications
leave so much up to the compiler.

That history aside, it is true that Java is far more definitely
specified than are C and C++ and it does specify a particular
floating-point standard (IEEE 754).

Nonetheless, I think it's reasonable to ask about the interpretation of
a specification _in principle_, apart from any particular
implementation's empirical behavior. The former is what we need to know
to write programs whose behavior is reliably what we intend. Then, if
the behavior of an implementation is at odds with the specification, it
can be reported as a bug and the compiler or interpreter fixed.

Otherwise weak specifications or implementation bugs tend to get
ensconced as "common practice" and can eventually lead to the
degradation of the specification rather than the improvement of the
implementations.

I admit that's a bit far-fetched in this case and that Java's
specification has been well tended and its implementations good, but
history provides cautionary examples of this pattern and we, as
professional programmers, should keep them in mind and heed their
lessons.


> Greg


Randall Schulz

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.