> double a = 1.0;
>
> if (a == 1.0)
> System.out.println("Hello");
> why Hello is printed out? since ?i am comparing two double values?
1.0 literal is of type double
variable a is of type double, simple assignment of 1.0D to a
is double a equal to literal double 1.0 ?
yes of course
--
Mike W
> hi,
>
[quoted text clipped - 21 lines]
>
> Bo
You probably read somewhere that you shouldn't rely on the == operator to
compare doubles. This is very true. An example, with arbitrary double
values:
double d = 64.9963;
double d1 = Math.pow(d, 34.591);
double d2 = Math.pow(d1, 1.0/34.591);
if(d2 == d)
System.out.println(d + " == " + d2);
else
System.out.println(d + " != " + d2);
Mathematically speaking, this should print
64.9963 == 64.9963
However, on my test case, it printed:
64.9963 != 64.99630000000002
This is because of rounding errors when comparing floating point numbers to
binary. If you want more information about this, google for "floating
point comparison".
In your case however, there is no rounding error. You are comparing the
exact value 1.0 with the exact value 1.0 - and these are of course equal.
In general when comparing floating point numbers you should use an error
margin instead of comparing them directly - but this does not mean all
pairs of floating point numbers are unequal by definition.
Googmeister - 13 Nov 2005 02:31 GMT
> > hi,
> >
[quoted text clipped - 41 lines]
> binary. If you want more information about this, google for "floating
> point comparison".
There's no rounding error when comparing floating point values. The
roundoff error occurs in several places prior to the comparison. First
the numbers 64.9963, 34.591, 1.0/34.591 are not representable using
double in Java, so they are approximated by the nearest IEEE floating
point value. Second, using Math.pow leads to more roundoff error, again
because the result here is not representable using double.
zero - 13 Nov 2005 12:14 GMT
>> > hi,
>> >
[quoted text clipped - 48 lines]
> point value. Second, using Math.pow leads to more roundoff error,
> again because the result here is not representable using double.
Right, I meant "rounding errors when converting floating point numbers
to binary."
Sorry for the confusion
>why Hello is printed out? since ?i am comparing two double values?
Some double values are exact. Some are not. See
http://mindprod.com/jgloss/floatingpoint.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
P.Hill - 17 Nov 2005 04:57 GMT
>>why Hello is printed out? since ?i am comparing two double values?
>
> Some double values are exact. Some are not. See
Curiously enough, it doesn't matter if the double of this particular
number is exact, because in this trivial case, the same exact or inexact
conversion to a binary double will occur, so the result will be the
same. Many if not most real uses of results from some a series of
calculations will exhibit the problem inexact representation.
I guess this shows a danger of testing with top simplistic a case.
-Paul